Files
streamline/satellite/test/figma_token.test.ts
T
2026-11-16 17:26:44 -08:00

76 lines
3.3 KiB
TypeScript

import { test, expect, describe, afterEach } from "bun:test";
import { figmaUserTokenEnv, FIGMA_API_KEY_ENV } from "../src/figma_token";
import { agentSpawnEnv } from "../src/agent_env";
const origFetch = globalThis.fetch;
const origKey = process.env[FIGMA_API_KEY_ENV];
afterEach(() => {
globalThis.fetch = origFetch;
if (origKey === undefined) delete process.env[FIGMA_API_KEY_ENV];
else process.env[FIGMA_API_KEY_ENV] = origKey;
});
/** Answers the Figma route with `figma`, and the Lark route with a miss. */
function mockControl(figma: unknown, status = 200) {
const urls: string[] = [];
globalThis.fetch = (async (input: RequestInfo | URL) => {
const url = String(input);
urls.push(url);
if (url.includes("/figma/")) return new Response(JSON.stringify(figma), { status });
return new Response(JSON.stringify({ ok: false }), { status: 200 });
}) as typeof fetch;
return urls;
}
describe("figmaUserTokenEnv", () => {
test("puts the creator's token in FIGMA_API_KEY and asks by kind + id", async () => {
const urls = mockControl({ ok: true, token: "figd_user" });
expect(await figmaUserTokenEnv({ kind: "verify", id: "v 1" })).toEqual({ FIGMA_API_KEY: "figd_user" });
expect(urls[0]).toContain("/api/internal/figma/user-token?kind=verify&id=v%201");
expect(process.env[FIGMA_API_KEY_ENV]).toBe(origKey);
});
test("a miss, an HTTP error or a fetch failure is an empty overlay", async () => {
mockControl({ ok: false, reason: "no token" });
expect(await figmaUserTokenEnv({ kind: "bug", id: "t1" })).toEqual({});
mockControl({}, 500);
expect(await figmaUserTokenEnv({ kind: "bug", id: "t1" })).toEqual({});
globalThis.fetch = (async () => { throw new Error("down"); }) as unknown as typeof fetch;
expect(await figmaUserTokenEnv({ kind: "bug", id: "t1" })).toEqual({});
});
test("the request carries a timeout, and a timeout falls back to the node token", async () => {
let signal: AbortSignal | null | undefined;
globalThis.fetch = (async (_: RequestInfo | URL, init?: RequestInit) => {
signal = init?.signal;
// What fetch throws when AbortSignal.timeout fires; not waiting 10 s for it.
throw new DOMException("The operation timed out.", "TimeoutError");
}) as typeof fetch;
expect(await figmaUserTokenEnv({ kind: "bug", id: "t1" })).toEqual({});
expect(signal).toBeInstanceOf(AbortSignal);
});
test("no item → no request", async () => {
const urls = mockControl({ ok: true, token: "figd_user" });
expect(await figmaUserTokenEnv(null)).toEqual({});
expect(urls).toEqual([]);
});
});
describe("agentSpawnEnv + Figma", () => {
test("the child gets the creator's token", async () => {
mockControl({ ok: true, token: "figd_user" });
const env = await agentSpawnEnv({ kind: "feature", id: "f1" });
expect(env.FIGMA_API_KEY).toBe("figd_user");
});
test("on a miss an inherited FIGMA_API_KEY is kept (current node behaviour)", async () => {
process.env[FIGMA_API_KEY_ENV] = "figd_from_satellite_env";
mockControl({ ok: false, reason: "no token" });
expect((await agentSpawnEnv({ kind: "feature", id: "f1" })).FIGMA_API_KEY).toBe("figd_from_satellite_env");
mockControl({ ok: true, token: "figd_user" });
expect((await agentSpawnEnv({ kind: "feature", id: "f1" })).FIGMA_API_KEY).toBe("figd_user");
});
});