Initial import of streamline

This commit is contained in:
2026-11-16 17:26:44 -08:00
commit 8e4aec1855
1221 changed files with 274256 additions and 0 deletions
+110
View File
@@ -0,0 +1,110 @@
// MUST be first: points DATABASE_URL at a closed local port before db_client is
// evaluated, so importing the module under test can't reach production.
import "./setup_env";
import { test, expect, describe } from "bun:test";
import {
sanitiseFigmaToken,
createFigmaUserTokenResolver,
figmaRejectsScope,
FIGMA_PROBE_TTL_MS,
type FigmaProbeResult,
} from "../src/figma_tokens_db";
const PAT = "figd_" + "a".repeat(40);
describe("sanitiseFigmaToken", () => {
test("trims and accepts a PAT", () => {
expect(sanitiseFigmaToken(` ${PAT}\n`)).toBe(PAT);
});
test("does not require the figd_ prefix", () => {
expect(sanitiseFigmaToken("x".repeat(20))).toBe("x".repeat(20));
});
test("rejects non-strings, wrong length and foreign characters", () => {
expect(sanitiseFigmaToken(null)).toBeNull();
expect(sanitiseFigmaToken("figd_short")).toBeNull();
expect(sanitiseFigmaToken("x".repeat(201))).toBeNull();
expect(sanitiseFigmaToken(`Bearer ${PAT}`)).toBeNull();
expect(sanitiseFigmaToken(`${PAT}!`)).toBeNull();
});
});
describe("figmaRejectsScope", () => {
test("a missing-scope 403 is told apart from a bad-token 403", () => {
expect(figmaRejectsScope(JSON.stringify({ status: 403, err: "Invalid scope(s): file_content:read. This endpoint requires the current_user:read scope" }))).toBe(true);
expect(figmaRejectsScope(JSON.stringify({ status: 403, err: "Invalid token" }))).toBe(false);
});
});
/**
* The resolver behind /internal/figma/user-token. The owner, the store and the
* Figma probe are injected, so nothing here touches the DB or the network.
*/
function harness(opts: { owner: string | null; tokens: Record<string, string>; probe: FigmaProbeResult[] }) {
const reads: string[] = [];
const probes: string[] = [];
let clock = 1_000;
const queue = [...opts.probe];
const resolve = createFigmaUserTokenResolver({
ownerFor: async () => opts.owner,
readToken: async (p) => { reads.push(p); return opts.tokens[p] ?? null; },
probe: async (t) => { probes.push(t); return queue.shift() ?? { verdict: "valid", handle: "h", email: "e" }; },
now: () => clock,
});
return { resolve, reads, probes, advance: (ms: number) => { clock += ms; }, tokens: opts.tokens };
}
const valid: FigmaProbeResult = { verdict: "valid", handle: "alice", email: "alice@x" };
describe("figma user-token resolver", () => {
test("returns the creator's own token", async () => {
const h = harness({ owner: "alice", tokens: { alice: PAT, bob: "figd_" + "b".repeat(40) }, probe: [valid] });
expect(await h.resolve("feature", "f1")).toEqual({ ok: true, token: PAT });
expect(h.reads).toEqual(["alice"]);
});
test("no creator → no token, and no other user's row is read", async () => {
const h = harness({ owner: null, tokens: { bob: PAT }, probe: [] });
expect((await h.resolve("feature", "f1")).ok).toBe(false);
expect(h.reads).toEqual([]);
});
test("creator without a token → ok:false, no probe", async () => {
const h = harness({ owner: "alice", tokens: { bob: PAT }, probe: [] });
expect(await h.resolve("bug", "t1")).toEqual({ ok: false, reason: "no token" });
expect(h.probes).toEqual([]);
});
test("Figma rejects the stored token → ok:false so the node token is used", async () => {
const h = harness({ owner: "alice", tokens: { alice: PAT }, probe: [{ verdict: "invalid", status: 403, missingScope: false }] });
expect((await h.resolve("feature", "f1")).ok).toBe(false);
});
test("network error passes the token through and is not cached", async () => {
const h = harness({ owner: "alice", tokens: { alice: PAT }, probe: [{ verdict: "error", reason: "timeout" }, valid] });
expect(await h.resolve("feature", "f1")).toEqual({ ok: true, token: PAT });
expect(await h.resolve("feature", "f1")).toEqual({ ok: true, token: PAT });
expect(h.probes.length).toBe(2);
});
test("verdict is cached per person for 10 minutes", async () => {
const h = harness({ owner: "alice", tokens: { alice: PAT }, probe: [{ verdict: "invalid", status: 403, missingScope: false }, valid] });
expect((await h.resolve("feature", "f1")).ok).toBe(false);
h.advance(FIGMA_PROBE_TTL_MS - 1);
expect((await h.resolve("feature", "f2")).ok).toBe(false);
expect(h.probes.length).toBe(1);
h.advance(2);
expect((await h.resolve("feature", "f3")).ok).toBe(true);
expect(h.probes.length).toBe(2);
});
test("saving a new token is re-probed without waiting for the cache", async () => {
const h = harness({ owner: "alice", tokens: { alice: PAT }, probe: [{ verdict: "invalid", status: 403, missingScope: false }, valid] });
expect((await h.resolve("feature", "f1")).ok).toBe(false);
const fresh = "figd_" + "c".repeat(40);
h.tokens.alice = fresh;
expect(await h.resolve("feature", "f1")).toEqual({ ok: true, token: fresh });
});
});