|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { runCommand, stubFetch, type FetchStub } from "../helpers/harness"; |
| 6 | + |
| 7 | +vi.mock("../../src/lib/config", () => ({ |
| 8 | + getApiUrl: () => "http://api.test", |
| 9 | + getToken: () => "tok", |
| 10 | +})); |
| 11 | +const gitState = vi.hoisted(() => ({ inRepo: false })); |
| 12 | +vi.mock("node:child_process", async (original) => { |
| 13 | + const actual = await original<typeof import("node:child_process")>(); |
| 14 | + return { |
| 15 | + ...actual, |
| 16 | + execFileSync: (...args: Parameters<typeof actual.execFileSync>) => { |
| 17 | + if (args[0] === "git") { |
| 18 | + if (gitState.inRepo) return Buffer.from("true"); |
| 19 | + throw new Error("not a Git repository"); |
| 20 | + } |
| 21 | + return actual.execFileSync(...args); |
| 22 | + }, |
| 23 | + }; |
| 24 | +}); |
| 25 | +vi.mock("../../src/lib/project-link", () => ({ readProjectLink: () => null })); |
| 26 | + |
| 27 | +describe("deploy requires folder upload opt-in (#853)", () => { |
| 28 | + let fetchStub: FetchStub; |
| 29 | + beforeEach(() => { |
| 30 | + vi.resetModules(); |
| 31 | + gitState.inRepo = false; |
| 32 | + }); |
| 33 | + afterEach(() => { |
| 34 | + fetchStub?.restore(); |
| 35 | + vi.restoreAllMocks(); |
| 36 | + }); |
| 37 | + |
| 38 | + it.each([ |
| 39 | + { flags: ["--folder"], inRepo: true }, |
| 40 | + { flags: ["--name", "local-app"], inRepo: false }, |
| 41 | + ])("uploads source with explicit opt-in: $flags", async ({ flags, inRepo }) => { |
| 42 | + const sourceDir = mkdtempSync(join(tmpdir(), "openship-folder-command-")); |
| 43 | + writeFileSync(join(sourceDir, "index.html"), "site"); |
| 44 | + vi.spyOn(process, "cwd").mockReturnValue(sourceDir); |
| 45 | + gitState.inRepo = inRepo; |
| 46 | + fetchStub = stubFetch((req) => { |
| 47 | + if (req.url.endsWith("/projects/folder/session")) { |
| 48 | + return { |
| 49 | + json: { |
| 50 | + sessionId: "session-1", |
| 51 | + expiresAt: Date.now() + 60_000, |
| 52 | + upload: { |
| 53 | + url: "/folder-upload", |
| 54 | + absoluteUrl: "http://api.test/folder-upload", |
| 55 | + method: "POST", |
| 56 | + headers: {}, |
| 57 | + requiresAuth: true, |
| 58 | + withCredentials: true, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }; |
| 62 | + } |
| 63 | + if (req.url.endsWith("/folder-upload")) return { json: { success: true } }; |
| 64 | + if (req.url.endsWith("/projects/folder/scan/session-1")) |
| 65 | + return { |
| 66 | + json: { |
| 67 | + success: true, |
| 68 | + name: "local-app", |
| 69 | + stack: "static", |
| 70 | + projectType: "app", |
| 71 | + packageManager: "npm", |
| 72 | + installCommand: "", |
| 73 | + buildCommand: "", |
| 74 | + startCommand: "", |
| 75 | + buildImage: "", |
| 76 | + outputDirectory: "", |
| 77 | + rootDirectory: "", |
| 78 | + }, |
| 79 | + }; |
| 80 | + if (req.url.endsWith("/projects/ensure")) { |
| 81 | + expect(req.body).toMatchObject({ projectId: "p1", gitProvider: "upload" }); |
| 82 | + return { json: { success: true, project_id: "p1", created: false } }; |
| 83 | + } |
| 84 | + if (req.url.endsWith("/deployments/build/access")) { |
| 85 | + expect(req.body).toMatchObject({ |
| 86 | + projectId: "p1", |
| 87 | + uploadSessionId: "session-1", |
| 88 | + serviceIds: ["svc1"], |
| 89 | + }); |
| 90 | + return { json: { success: true, deployment_id: "d1", project_id: "p1" } }; |
| 91 | + } |
| 92 | + throw new Error(`Unexpected endpoint: ${req.url}`); |
| 93 | + }); |
| 94 | + try { |
| 95 | + const { deployCommand } = await import("../../src/commands/deploy"); |
| 96 | + const result = await runCommand(deployCommand, [ |
| 97 | + ...flags, |
| 98 | + "--project", |
| 99 | + "p1", |
| 100 | + "--service-ids", |
| 101 | + "svc1", |
| 102 | + ]); |
| 103 | + expect(result.code).toBe(0); |
| 104 | + expect(result.out + result.err).toContain("d1"); |
| 105 | + expect(fetchStub.calls).toHaveLength(5); |
| 106 | + } finally { |
| 107 | + rmSync(sourceDir, { recursive: true, force: true }); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + it("does not provision an upload without a source or project opt-in outside Git", async () => { |
| 112 | + fetchStub = stubFetch(() => { |
| 113 | + throw new Error("must not send a request"); |
| 114 | + }); |
| 115 | + const { deployCommand } = await import("../../src/commands/deploy"); |
| 116 | + const result = await runCommand(deployCommand, []); |
| 117 | + expect(result.code).toBe(1); |
| 118 | + expect(result.err).toContain("--folder"); |
| 119 | + expect(fetchStub.calls).toEqual([]); |
| 120 | + }); |
| 121 | + |
| 122 | + it("redeploys an explicit project from outside Git without uploading the working directory", async () => { |
| 123 | + fetchStub = stubFetch((req) => { |
| 124 | + expect(req.url).toBe("http://api.test/api/deployments"); |
| 125 | + expect(req.body).toMatchObject({ projectId: "p1", serviceIds: ["svc1"] }); |
| 126 | + expect(req.body).not.toHaveProperty("branch"); |
| 127 | + return { json: { data: { deployment_id: "dep1", project_id: "p1" } } }; |
| 128 | + }); |
| 129 | + const { deployCommand } = await import("../../src/commands/deploy"); |
| 130 | + expect( |
| 131 | + (await runCommand(deployCommand, ["--project", "p1", "--service-ids", "svc1"])).code, |
| 132 | + ).toBe(0); |
| 133 | + expect(fetchStub.calls).toHaveLength(1); |
| 134 | + }); |
| 135 | + |
| 136 | + it("uses the Git deployment endpoint for an explicit remote branch", async () => { |
| 137 | + fetchStub = stubFetch((req) => { |
| 138 | + expect(req.url).toBe("http://api.test/api/deployments"); |
| 139 | + expect(req.body).toMatchObject({ projectId: "p1", branch: "main", serviceIds: ["svc1"] }); |
| 140 | + return { json: { data: { deployment_id: "dep1", project_id: "p1" } } }; |
| 141 | + }); |
| 142 | + const { deployCommand } = await import("../../src/commands/deploy"); |
| 143 | + const result = await runCommand(deployCommand, [ |
| 144 | + "--project", |
| 145 | + "p1", |
| 146 | + "--branch", |
| 147 | + "main", |
| 148 | + "--service-ids", |
| 149 | + "svc1", |
| 150 | + ]); |
| 151 | + expect(result.code).toBe(0); |
| 152 | + expect(fetchStub.calls).toHaveLength(1); |
| 153 | + }); |
| 154 | + |
| 155 | + it("rejects Git-only options with --folder before making requests", async () => { |
| 156 | + fetchStub = stubFetch(() => { |
| 157 | + throw new Error("must not send a request"); |
| 158 | + }); |
| 159 | + const { deployCommand } = await import("../../src/commands/deploy"); |
| 160 | + const result = await runCommand(deployCommand, ["--folder", "--commit", "abc123"]); |
| 161 | + expect(result.code).toBe(1); |
| 162 | + expect(result.err).toContain("cannot be combined"); |
| 163 | + expect(fetchStub.calls).toEqual([]); |
| 164 | + }); |
| 165 | +}); |
0 commit comments