|
| 1 | +import { readFileSync, readdirSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | + |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | + |
| 6 | +/** |
| 7 | + * A TypeScript SDK integration fixture named for a release line — `ai-4`, |
| 8 | + * `langchain-0.3`, `mastra-0` — exists to hold its framework on that line: the |
| 9 | + * pinned release is the subject of the test (SECURITY.md, "Triaging a Dependabot |
| 10 | + * alert in an integration fixture"). A move off it still installs and still |
| 11 | + * passes, against a different major, so the matrix loses a supported floor with |
| 12 | + * every check green. Dependabot security updates have made that move twice |
| 13 | + * (#838, #931), both times to reach a fix the fixture could not take. This fails |
| 14 | + * on the manifest, before anything installs. |
| 15 | + * |
| 16 | + * #931 also moved the `runtimes` fixture's zod without the `npm:zod@…` specifier |
| 17 | + * its Deno program imports, which Deno resolves against the fixture's |
| 18 | + * `node_modules` and so could no longer find. That half is checked here too. |
| 19 | + */ |
| 20 | + |
| 21 | +const fixturesDir = join(__dirname, "..", "..", "sdk", "typescript", "integration", "fixtures"); |
| 22 | + |
| 23 | +/** The package each fixture-name prefix pins. */ |
| 24 | +const FRAMEWORK: Record<string, string> = { |
| 25 | + ai: "ai", |
| 26 | + langchain: "@langchain/core", |
| 27 | + llamaindex: "llamaindex", |
| 28 | + mastra: "@mastra/core", |
| 29 | +}; |
| 30 | + |
| 31 | +const fixtures = readdirSync(fixturesDir, { withFileTypes: true }) |
| 32 | + .filter((entry) => entry.isDirectory()) |
| 33 | + .map((entry) => entry.name); |
| 34 | + |
| 35 | +const named = fixtures |
| 36 | + .map((name) => /^([a-z]+)-(\d+(?:\.\d+)?)$/.exec(name)) |
| 37 | + .filter((match): match is RegExpExecArray => match !== null) |
| 38 | + .map(([fixture, prefix, line]) => ({ fixture: fixture!, framework: FRAMEWORK[prefix!], line: line! })); |
| 39 | + |
| 40 | +const readJson = (fixture: string, file: string) => |
| 41 | + JSON.parse(readFileSync(join(fixturesDir, fixture, file), "utf8")) as { |
| 42 | + dependencies?: Record<string, string>; |
| 43 | + packages?: Record<string, { version?: string }>; |
| 44 | + }; |
| 45 | + |
| 46 | +/** Every `npm:<name>@<version>` a fixture's top-level programs import, once each. */ |
| 47 | +const NPM_SPECIFIER = /["']npm:((?:@[\w.-]+\/)?[\w.-]+)@([^"'/]+)/g; |
| 48 | +const specifiers = fixtures.flatMap((fixture) => |
| 49 | + readdirSync(join(fixturesDir, fixture)) |
| 50 | + .filter((file) => file.endsWith(".ts")) |
| 51 | + .flatMap((file) => { |
| 52 | + const source = readFileSync(join(fixturesDir, fixture, file), "utf8"); |
| 53 | + const found = new Set([...source.matchAll(NPM_SPECIFIER)].map(([, name, version]) => `${name}@${version}`)); |
| 54 | + return [...found].map((spec) => { |
| 55 | + const at = spec.lastIndexOf("@"); |
| 56 | + return { fixture, file, name: spec.slice(0, at), version: spec.slice(at + 1) }; |
| 57 | + }); |
| 58 | + }), |
| 59 | +); |
| 60 | + |
| 61 | +describe("TS SDK integration fixtures named for a release line", () => { |
| 62 | + it("finds them, every framework's supported floor among them", () => { |
| 63 | + expect(named.map((f) => f.fixture)).toEqual( |
| 64 | + expect.arrayContaining(["ai-4", "langchain-0.3", "llamaindex-0.11", "mastra-0"]), |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it("know which framework each name pins", () => { |
| 69 | + expect(named.filter((f) => f.framework === undefined).map((f) => f.fixture)).toEqual([]); |
| 70 | + }); |
| 71 | + |
| 72 | + it.each(named)("$fixture pins $framework exactly, on $line", ({ fixture, framework, line }) => { |
| 73 | + const pinned = readJson(fixture, "package.json").dependencies?.[framework!]; |
| 74 | + expect(pinned, `${fixture}: ${framework} in package.json`).toMatch(/^\d+\.\d+\.\d+$/); |
| 75 | + expect(pinned!.startsWith(`${line}.`), `${fixture} pins ${framework}@${pinned}`).toBe(true); |
| 76 | + const locked = readJson(fixture, "package-lock.json").packages?.[`node_modules/${framework}`]?.version; |
| 77 | + expect(locked, `${fixture}: ${framework} in package-lock.json`).toBe(pinned); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +describe("TS SDK integration fixtures' npm: specifiers", () => { |
| 82 | + it("finds the Deno program's", () => { |
| 83 | + expect(specifiers.map((s) => s.name)).toEqual(expect.arrayContaining(["ai", "zod"])); |
| 84 | + }); |
| 85 | + |
| 86 | + it.each(specifiers)("$fixture/$file imports npm:$name@$version, the version it installs", (spec) => { |
| 87 | + const { fixture, name, version } = spec; |
| 88 | + const locked = readJson(fixture, "package-lock.json").packages?.[`node_modules/${name}`]?.version; |
| 89 | + expect(locked, `${fixture}: ${name} in package-lock.json`).toBe(version); |
| 90 | + }); |
| 91 | +}); |
0 commit comments