|
1 | 1 | import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
2 | 2 | import fs from "fs"; |
3 | 3 | import path from "path"; |
| 4 | +import { createRequire } from "module"; |
| 5 | +const _require = createRequire(import.meta.url); |
| 6 | +const { AGENT_OUTPUT_FILENAME, TMP_GH_AW_PATH } = _require("./constants.cjs"); |
4 | 7 | describe("collect_ndjson_output.cjs", () => { |
5 | 8 | let mockCore, collectScript; |
6 | 9 | (beforeEach(() => { |
@@ -146,12 +149,30 @@ describe("collect_ndjson_output.cjs", () => { |
146 | 149 | expect(mockCore.info).toHaveBeenCalledWith("GH_AW_SAFE_OUTPUTS not set, no output to collect")); |
147 | 150 | }), |
148 | 151 | it("should handle missing output file", async () => { |
149 | | - ((process.env.GH_AW_SAFE_OUTPUTS = "/tmp/gh-aw/nonexistent-file.txt"), |
| 152 | + const missingFile = `${TMP_GH_AW_PATH}/nonexistent-file.txt`; |
| 153 | + ((process.env.GH_AW_SAFE_OUTPUTS = missingFile), |
150 | 154 | await eval(`(async () => { ${collectScript}; await main(); })()`), |
151 | | - expect(mockCore.setOutput).toHaveBeenCalledWith("output", ""), |
| 155 | + expect(mockCore.setOutput).toHaveBeenCalledWith("output", '{"items":[],"errors":[]}'), |
152 | 156 | expect(mockCore.setOutput).toHaveBeenCalledWith("output_types", ""), |
153 | 157 | expect(mockCore.setOutput).toHaveBeenCalledWith("has_patch", "false"), |
154 | | - expect(mockCore.info).toHaveBeenCalledWith("Output file does not exist: /tmp/gh-aw/nonexistent-file.txt")); |
| 158 | + expect(mockCore.info).toHaveBeenCalledWith(`Output file does not exist: ${missingFile} — no safe-output items were emitted; treating as empty collection (graceful no-op)`), |
| 159 | + expect(mockCore.exportVariable).toHaveBeenCalledWith("GH_AW_AGENT_OUTPUT", path.join(TMP_GH_AW_PATH, AGENT_OUTPUT_FILENAME))); |
| 160 | + }), |
| 161 | + it("should error and still set output when artifact write fails", async () => { |
| 162 | + const writeError = new Error("disk full"); |
| 163 | + const spy = vi.spyOn(fs, "writeFileSync").mockImplementationOnce(() => { |
| 164 | + throw writeError; |
| 165 | + }); |
| 166 | + try { |
| 167 | + ((process.env.GH_AW_SAFE_OUTPUTS = `${TMP_GH_AW_PATH}/nonexistent-file.txt`), |
| 168 | + await eval(`(async () => { ${collectScript}; await main(); })()`), |
| 169 | + expect(mockCore.error).toHaveBeenCalledWith(expect.stringContaining("disk full")), |
| 170 | + expect(spy).toHaveBeenCalledWith(path.join(TMP_GH_AW_PATH, AGENT_OUTPUT_FILENAME), '{"items":[],"errors":[]}', "utf8"), |
| 171 | + expect(mockCore.setOutput).toHaveBeenCalledWith("output", '{"items":[],"errors":[]}'), |
| 172 | + expect(mockCore.exportVariable).not.toHaveBeenCalled()); |
| 173 | + } finally { |
| 174 | + spy.mockRestore(); |
| 175 | + } |
155 | 176 | }), |
156 | 177 | it("should handle empty output file", async () => { |
157 | 178 | const testFile = "/tmp/gh-aw/test-ndjson-output.txt"; |
|
0 commit comments