|
1 | | -import { describe, test, expect, beforeEach, afterEach, mock } from "bun:test"; |
2 | | -import { buildInlineUrl, buildStudioUrl } from "../src/commands/studio.ts"; |
| 1 | +import { describe, test, expect, beforeEach, afterEach, mock, spyOn } from "bun:test"; |
| 2 | +import { buildInlineUrl, buildStudioUrl, shareApiKey } from "../src/commands/studio.ts"; |
| 3 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { ApiError } from "../src/http/client.ts"; |
3 | 7 |
|
4 | 8 | const sample = { |
5 | 9 | timeline: { |
@@ -103,3 +107,125 @@ describe("buildStudioUrl", () => { |
103 | 107 | expect(fetchSpy).toHaveBeenCalledTimes(0); |
104 | 108 | }); |
105 | 109 | }); |
| 110 | + |
| 111 | +describe("share API rules", () => { |
| 112 | + // Answers each share call with the next step; an Error step rejects like a timeout or network failure. |
| 113 | + function shareSequence(...steps: (Response | Error)[]) { |
| 114 | + const fetchMock = mock(async (_input: string | URL | Request, _init?: RequestInit) => { |
| 115 | + const step = steps.shift(); |
| 116 | + if (!step) throw new Error("Unexpected extra share call"); |
| 117 | + if (step instanceof Error) throw step; |
| 118 | + return step; |
| 119 | + }); |
| 120 | + globalThis.fetch = fetchMock as unknown as typeof fetch; |
| 121 | + return fetchMock; |
| 122 | + } |
| 123 | + |
| 124 | + const shared = () => new Response(JSON.stringify({ slug: "abc12345", url: "https://shotstack.studio/s/abc12345" })); |
| 125 | + const refusal = (status: number, error: string) => new Response(JSON.stringify({ error }), { status }); |
| 126 | + const timeout = () => Object.assign(new Error("The operation was aborted due to timeout"), { name: "TimeoutError" }); |
| 127 | + |
| 128 | + let stderr: ReturnType<typeof spyOn>; |
| 129 | + beforeEach(() => { |
| 130 | + stderr = spyOn(console, "error").mockImplementation(() => {}); |
| 131 | + }); |
| 132 | + afterEach(() => { |
| 133 | + stderr.mockRestore(); |
| 134 | + }); |
| 135 | + |
| 136 | + test("sends the API key and prints no expiry note", async () => { |
| 137 | + const fetchMock = shareSequence(shared()); |
| 138 | + const result = await buildStudioUrl(sample, { apiKey: "key" }); |
| 139 | + const headers = fetchMock.mock.calls[0]![1]!.headers as Record<string, string>; |
| 140 | + expect(headers["x-api-key"]).toBe("key"); |
| 141 | + expect(fetchMock.mock.calls[0]![1]!.redirect).toBe("error"); |
| 142 | + expect(result.shortened).toBe(true); |
| 143 | + expect(stderr).not.toHaveBeenCalled(); |
| 144 | + }); |
| 145 | + |
| 146 | + test("without a key, says on stderr that the link lasts 30 days", async () => { |
| 147 | + const fetchMock = shareSequence(shared()); |
| 148 | + await buildStudioUrl(sample); |
| 149 | + const headers = fetchMock.mock.calls[0]![1]!.headers as Record<string, string>; |
| 150 | + expect(headers["x-api-key"]).toBeUndefined(); |
| 151 | + expect(String(stderr.mock.calls[0]![0])).toContain("lasts 30 days"); |
| 152 | + }); |
| 153 | + |
| 154 | + test("for dev, the note names the cause instead of asking for a key", async () => { |
| 155 | + shareSequence(shared()); |
| 156 | + await buildStudioUrl(sample, { env: "dev" }); |
| 157 | + expect(String(stderr.mock.calls[0]![0])).toContain("Dev keys"); |
| 158 | + }); |
| 159 | + |
| 160 | + test("retries once after a timeout", async () => { |
| 161 | + const fetchMock = shareSequence(timeout(), shared()); |
| 162 | + const result = await buildStudioUrl(sample, { apiKey: "key" }); |
| 163 | + expect(fetchMock).toHaveBeenCalledTimes(2); |
| 164 | + expect(result).toEqual({ url: "https://shotstack.studio/s/abc12345", shortened: true }); |
| 165 | + }); |
| 166 | + |
| 167 | + test("retries once after a 429", async () => { |
| 168 | + const fetchMock = shareSequence(new Response('{"message":"Too Many Requests"}', { status: 429 }), shared()); |
| 169 | + const result = await buildStudioUrl(sample, { apiKey: "key" }); |
| 170 | + expect(fetchMock).toHaveBeenCalledTimes(2); |
| 171 | + expect(result.shortened).toBe(true); |
| 172 | + }); |
| 173 | + |
| 174 | + test("falls back after two 5xx and names the cause", async () => { |
| 175 | + const fetchMock = shareSequence(refusal(502, "storage-failed"), new Response("", { status: 503 })); |
| 176 | + const result = await buildStudioUrl(sample, { apiKey: "key" }); |
| 177 | + expect(fetchMock).toHaveBeenCalledTimes(2); |
| 178 | + expect(result.shortened).toBe(false); |
| 179 | + expect(String(stderr.mock.calls[0]![0])).toContain("HTTP 503"); |
| 180 | + }); |
| 181 | + |
| 182 | + test("falls back after two timeouts and names the cause", async () => { |
| 183 | + shareSequence(timeout(), timeout()); |
| 184 | + const result = await buildStudioUrl(sample, { apiKey: "key" }); |
| 185 | + expect(result.shortened).toBe(false); |
| 186 | + expect(String(stderr.mock.calls[0]![0])).toContain("timeout"); |
| 187 | + }); |
| 188 | + |
| 189 | + test("a 400 is an error with the share API's reason, not an inline URL", async () => { |
| 190 | + const fetchMock = shareSequence(refusal(400, "edit-missing-timeline-or-output")); |
| 191 | + const err = await buildStudioUrl(sample, { apiKey: "key" }).catch((e: unknown) => e); |
| 192 | + expect((err as Error).message).toContain("edit-missing-timeline-or-output"); |
| 193 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 194 | + }); |
| 195 | + |
| 196 | + test("a 413 is an ApiError carrying payload-too-large", async () => { |
| 197 | + shareSequence(refusal(413, "payload-too-large")); |
| 198 | + const err = await buildStudioUrl(sample, { apiKey: "key" }).catch((e: unknown) => e); |
| 199 | + expect(err).toBeInstanceOf(ApiError); |
| 200 | + expect((err as ApiError).status).toBe(413); |
| 201 | + expect((err as ApiError).message).toContain("payload-too-large"); |
| 202 | + }); |
| 203 | +}); |
| 204 | + |
| 205 | +describe("shareApiKey", () => { |
| 206 | + const saved = process.env.SHOTSTACK_API_KEY; |
| 207 | + afterEach(() => { |
| 208 | + if (saved === undefined) delete process.env.SHOTSTACK_API_KEY; |
| 209 | + else process.env.SHOTSTACK_API_KEY = saved; |
| 210 | + }); |
| 211 | + |
| 212 | + test("uses the key a sandbox render would use", () => { |
| 213 | + process.env.SHOTSTACK_API_KEY = "stage-key"; |
| 214 | + expect(shareApiKey("stage")).toBe("stage-key"); |
| 215 | + }); |
| 216 | + |
| 217 | + test("returns no key when none is set or saved", () => { |
| 218 | + const dir = mkdtempSync(join(tmpdir(), "shotstack-creds-")); |
| 219 | + delete process.env.SHOTSTACK_API_KEY; |
| 220 | + try { |
| 221 | + expect(shareApiKey("stage", join(dir, "credentials.json"))).toBeUndefined(); |
| 222 | + } finally { |
| 223 | + rmSync(dir, { recursive: true, force: true }); |
| 224 | + } |
| 225 | + }); |
| 226 | + |
| 227 | + test("shares anonymously for dev because Studio cannot validate dev keys", () => { |
| 228 | + process.env.SHOTSTACK_API_KEY = "dev-key"; |
| 229 | + expect(shareApiKey("dev")).toBeUndefined(); |
| 230 | + }); |
| 231 | +}); |
0 commit comments