|
| 1 | +import { describe, it, beforeEach, afterEach, mock } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { tmpdir } from "node:os"; |
| 6 | + |
| 7 | +mock.module("../allowance-auth.js", { |
| 8 | + namedExports: { |
| 9 | + requireAllowanceAuth: () => ({ headers: { "SIGN-IN-WITH-X": "dGVzdA==" } }), |
| 10 | + }, |
| 11 | +}); |
| 12 | + |
| 13 | +mock.module("../paid-fetch.js", { |
| 14 | + namedExports: { |
| 15 | + paidApiRequest: async (path: string, opts: any) => { |
| 16 | + const { apiRequest } = await import("../client.js"); |
| 17 | + return apiRequest(path, opts); |
| 18 | + }, |
| 19 | + }, |
| 20 | +}); |
| 21 | + |
| 22 | +const { handleBundleDeploy } = await import("./bundle-deploy.js"); |
| 23 | + |
| 24 | +const originalFetch = globalThis.fetch; |
| 25 | +let tempDir: string; |
| 26 | + |
| 27 | +beforeEach(() => { |
| 28 | + tempDir = mkdtempSync(join(tmpdir(), "run402-bundle-deploy-test-")); |
| 29 | + process.env.RUN402_CONFIG_DIR = tempDir; |
| 30 | + process.env.RUN402_API_BASE = "https://test-api.run402.com"; |
| 31 | + |
| 32 | + const store = { |
| 33 | + projects: { |
| 34 | + "proj-001": { anon_key: "ak-123", service_key: "sk-456" }, |
| 35 | + }, |
| 36 | + }; |
| 37 | + writeFileSync(join(tempDir, "projects.json"), JSON.stringify(store)); |
| 38 | +}); |
| 39 | + |
| 40 | +afterEach(() => { |
| 41 | + globalThis.fetch = originalFetch; |
| 42 | + rmSync(tempDir, { recursive: true, force: true }); |
| 43 | + delete process.env.RUN402_CONFIG_DIR; |
| 44 | + delete process.env.RUN402_API_BASE; |
| 45 | +}); |
| 46 | + |
| 47 | +describe("bundle_deploy tool", () => { |
| 48 | + it("sends inherit in body when true", async () => { |
| 49 | + let capturedBody: string | undefined; |
| 50 | + globalThis.fetch = (async (_url: string | URL | Request, init?: RequestInit) => { |
| 51 | + capturedBody = init?.body as string; |
| 52 | + return new Response( |
| 53 | + JSON.stringify({ project_id: "proj-001", site_url: "https://dpl-001.sites.run402.com" }), |
| 54 | + { status: 201, headers: { "Content-Type": "application/json" } }, |
| 55 | + ); |
| 56 | + }) as typeof fetch; |
| 57 | + |
| 58 | + await handleBundleDeploy({ |
| 59 | + project_id: "proj-001", |
| 60 | + files: [{ file: "style.css", data: "body{}" }], |
| 61 | + inherit: true, |
| 62 | + }); |
| 63 | + |
| 64 | + const parsed = JSON.parse(capturedBody!); |
| 65 | + assert.equal(parsed.inherit, true); |
| 66 | + }); |
| 67 | + |
| 68 | + it("does not send inherit when omitted", async () => { |
| 69 | + let capturedBody: string | undefined; |
| 70 | + globalThis.fetch = (async (_url: string | URL | Request, init?: RequestInit) => { |
| 71 | + capturedBody = init?.body as string; |
| 72 | + return new Response( |
| 73 | + JSON.stringify({ project_id: "proj-001", site_url: "https://dpl-002.sites.run402.com" }), |
| 74 | + { status: 201, headers: { "Content-Type": "application/json" } }, |
| 75 | + ); |
| 76 | + }) as typeof fetch; |
| 77 | + |
| 78 | + await handleBundleDeploy({ |
| 79 | + project_id: "proj-001", |
| 80 | + files: [{ file: "index.html", data: "<html></html>" }], |
| 81 | + }); |
| 82 | + |
| 83 | + const parsed = JSON.parse(capturedBody!); |
| 84 | + assert.equal(parsed.inherit, undefined); |
| 85 | + }); |
| 86 | +}); |
0 commit comments