import { describe, expect, it } from "bun:test"; import { dirname, join } from "node:path"; import { existsSync, readFileSync } from "node:fs"; /** * Guards the one dependency mistake that broke the shared schemas, because * nothing else catches it and the symptom points somewhere else entirely. * * `@continuum/ts-types` is linked from continuum-schemas and resolves its own * dependencies from that repo's node_modules. It had TypeBox 0.32.37 while * this backend had 0.33.24, and two *different* TypeBox versions mean two * structurally incompatible `TSchema` types - so Elysia rejected every schema * from there, with a message that pointed somewhere else entirely. * * The misleading part: Elysia's validator accepts * `TSchema | StandardSchemaV1Like`, so when the TSchema branch failed * TypeScript reported the *other* branch's failure, `'~standard' is missing`. * That reads like Elysia wants a Standard Schema validator and TypeBox is the * wrong tool, which is not true at all - Elysia is TypeBox-native (`t` is a * TypeBox JavaScriptTypeBuilder, and the dist `require`s @sinclair/typebox at * runtime without even declaring it as a dependency). * * Note what this does *not* check: that both resolve to the same file. A * `file:` dependency keeps its own node_modules, so there are legitimately two * copies on disk. Two copies of the *same version* are interchangeable here, * because TypeBox's types are structural and its kind markers use * `Symbol.for()`, which is global across copies. It is the version skew that * breaks, not the duplication. */ /** The installed version of `name`, resolved as `from` would resolve it. */ function installedVersion(name: string, from: string): string { let dir = dirname(Bun.resolveSync(name, from)); // Walk up from the entry file to the package root - resolveSync returns e.g. // build/esm/index.mjs, and package.json is not always an allowed export. for (let i = 0; i < 10; i++) { const candidate = join(dir, "package.json"); if (existsSync(candidate)) { const pkg = JSON.parse(readFileSync(candidate, "utf8")); if (pkg.name === name) return pkg.version; } const parent = dirname(dir); if (parent === dir) break; dir = parent; } throw new Error(`could not find the installed package.json for ${name} from ${from}`); } describe("@sinclair/typebox version is identical everywhere", () => { it("matches between this backend and @continuum/ts-types", () => { const here = installedVersion("@sinclair/typebox", import.meta.dir); const tsTypesDir = dirname(Bun.resolveSync("@continuum/ts-types", import.meta.dir)); const there = installedVersion("@sinclair/typebox", tsTypesDir); expect(there).toBe(here); }); it("is pinned exactly, not as a range", () => { // A caret range is what let the two drift apart in the first place: this // backend and ts-types each resolved their own newest match. const pkg = JSON.parse(readFileSync(`${import.meta.dir}/../package.json`, "utf8")); const pinned = pkg.dependencies["@sinclair/typebox"]; expect(pinned).not.toMatch(/^[\^~]/); expect(installedVersion("@sinclair/typebox", import.meta.dir)).toBe(pinned); }); });