|
| 1 | +import { mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | + |
| 6 | +const prepareTaskArtifactUploads = vi.fn(); |
| 7 | +const finalizeTaskArtifactUploads = vi.fn(); |
| 8 | + |
| 9 | +vi.mock("../../../signed-commit-artefacts", () => ({ |
| 10 | + createSandboxPosthogClient: () => ({ |
| 11 | + prepareTaskArtifactUploads, |
| 12 | + finalizeTaskArtifactUploads, |
| 13 | + }), |
| 14 | +})); |
| 15 | + |
| 16 | +import { uploadArtifactTool } from "./upload-artifact"; |
| 17 | + |
| 18 | +describe("uploadArtifactTool", () => { |
| 19 | + let cwd: string; |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + vi.clearAllMocks(); |
| 23 | + cwd = await mkdtemp(path.join(os.tmpdir(), "upload-artifact-")); |
| 24 | + vi.stubGlobal( |
| 25 | + "fetch", |
| 26 | + vi.fn().mockResolvedValue({ ok: true, status: 204 }), |
| 27 | + ); |
| 28 | + prepareTaskArtifactUploads.mockResolvedValue([ |
| 29 | + { |
| 30 | + id: "artifact-1", |
| 31 | + name: "report.csv", |
| 32 | + type: "output", |
| 33 | + size: 7, |
| 34 | + storage_path: "tasks/artifacts/report.csv", |
| 35 | + expires_in: 300, |
| 36 | + presigned_post: { |
| 37 | + url: "https://storage.example/upload", |
| 38 | + fields: { key: "value" }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + ]); |
| 42 | + finalizeTaskArtifactUploads.mockResolvedValue([ |
| 43 | + { |
| 44 | + id: "artifact-1", |
| 45 | + name: "report.csv", |
| 46 | + type: "output", |
| 47 | + size: 7, |
| 48 | + storage_path: "tasks/artifacts/report.csv", |
| 49 | + uploaded_at: "2026-01-01T00:00:00Z", |
| 50 | + }, |
| 51 | + ]); |
| 52 | + }); |
| 53 | + |
| 54 | + afterEach(async () => { |
| 55 | + vi.restoreAllMocks(); |
| 56 | + await rm(cwd, { recursive: true, force: true }); |
| 57 | + }); |
| 58 | + |
| 59 | + it("uploads and finalizes a workspace file as an output artifact", async () => { |
| 60 | + await writeFile(path.join(cwd, "report.csv"), "a,b\n1,2"); |
| 61 | + |
| 62 | + const result = await uploadArtifactTool.handler( |
| 63 | + { cwd, taskId: "task-1", taskRunId: "run-1" }, |
| 64 | + { path: "report.csv", contentType: "text/csv" }, |
| 65 | + ); |
| 66 | + |
| 67 | + expect(result.isError).toBeUndefined(); |
| 68 | + expect(prepareTaskArtifactUploads).toHaveBeenCalledWith("task-1", "run-1", [ |
| 69 | + { name: "report.csv", type: "output", size: 7, content_type: "text/csv" }, |
| 70 | + ]); |
| 71 | + expect(fetch).toHaveBeenCalledWith( |
| 72 | + "https://storage.example/upload", |
| 73 | + expect.objectContaining({ method: "POST", body: expect.any(FormData) }), |
| 74 | + ); |
| 75 | + expect(finalizeTaskArtifactUploads).toHaveBeenCalledWith( |
| 76 | + "task-1", |
| 77 | + "run-1", |
| 78 | + [ |
| 79 | + expect.objectContaining({ |
| 80 | + id: "artifact-1", |
| 81 | + type: "output", |
| 82 | + storage_path: "tasks/artifacts/report.csv", |
| 83 | + }), |
| 84 | + ], |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it("rejects files outside the session workspace", async () => { |
| 89 | + const outside = await mkdtemp(path.join(os.tmpdir(), "outside-artifact-")); |
| 90 | + const outsideFile = path.join(outside, "secret.txt"); |
| 91 | + await writeFile(outsideFile, "secret"); |
| 92 | + |
| 93 | + try { |
| 94 | + const result = await uploadArtifactTool.handler( |
| 95 | + { cwd, taskId: "task-1", taskRunId: "run-1" }, |
| 96 | + { path: outsideFile }, |
| 97 | + ); |
| 98 | + |
| 99 | + expect(result.isError).toBe(true); |
| 100 | + expect(result.content[0]?.text).toContain("inside the session workspace"); |
| 101 | + expect(prepareTaskArtifactUploads).not.toHaveBeenCalled(); |
| 102 | + } finally { |
| 103 | + await rm(outside, { recursive: true, force: true }); |
| 104 | + } |
| 105 | + }); |
| 106 | +}); |
0 commit comments