|
| 1 | +// An ejected entity/routes generator imports the owned adapter copy in codegen/runtime/ |
| 2 | +// by a relative path. In a monorepo whose output sits in a workspace package that path |
| 3 | +// leaves the package and the package's build rejects it (found upgrading a pnpm monorepo: |
| 4 | +// `TS6059 … is not under 'rootDir'`). meta gen warns, naming the package. |
| 5 | +import { describe, test, expect, afterEach } from "bun:test"; |
| 6 | +import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs"; |
| 7 | +import { tmpdir } from "node:os"; |
| 8 | +import { join } from "node:path"; |
| 9 | +import { findRuntimeBoundaryCrossings, runtimeBoundaryWarnings } from "../../src/lib/runtime-boundary-advisory.js"; |
| 10 | + |
| 11 | +const dirs: string[] = []; |
| 12 | +afterEach(() => { for (const d of dirs.splice(0)) rmSync(d, { recursive: true, force: true }); }); |
| 13 | + |
| 14 | +function project(): string { |
| 15 | + const d = mkdtempSync(join(tmpdir(), "mo-rt-boundary-")); |
| 16 | + dirs.push(d); |
| 17 | + writeFileSync(join(d, "package.json"), "{}"); |
| 18 | + mkdirSync(join(d, "codegen/runtime/drizzle-fastify"), { recursive: true }); |
| 19 | + writeFileSync(join(d, "codegen/runtime/drizzle-fastify/index.ts"), "export {};\n"); |
| 20 | + return d; |
| 21 | +} |
| 22 | + |
| 23 | +function gen(root: string, rel: string, content: string): string { |
| 24 | + const abs = join(root, rel); |
| 25 | + mkdirSync(join(abs, ".."), { recursive: true }); |
| 26 | + writeFileSync(abs, content); |
| 27 | + return abs; |
| 28 | +} |
| 29 | + |
| 30 | +describe("runtime boundary advisory", () => { |
| 31 | + test("a file in a workspace package importing ../codegen/runtime is named", () => { |
| 32 | + const root = project(); |
| 33 | + mkdirSync(join(root, "apps/api"), { recursive: true }); |
| 34 | + writeFileSync(join(root, "apps/api/package.json"), "{}"); |
| 35 | + const f = gen(root, "apps/api/src/generated/User.routes.ts", |
| 36 | + `import { mountCrudRoutes } from "../../../../codegen/runtime/drizzle-fastify/index";\n`); |
| 37 | + const found = findRuntimeBoundaryCrossings(root, [f]); |
| 38 | + expect(found).toEqual([{ packageDir: "apps/api", example: "apps/api/src/generated/User.routes.ts" }]); |
| 39 | + const [w] = runtimeBoundaryWarnings(found); |
| 40 | + expect(w).toContain(`runtimeImport: "@metaobjectsdev/runtime-ts"`); |
| 41 | + expect(w).toContain("apps/api/"); |
| 42 | + }); |
| 43 | + |
| 44 | + test("a single-package project is not flagged", () => { |
| 45 | + const root = project(); |
| 46 | + const f = gen(root, "src/generated/User.routes.ts", |
| 47 | + `import { mountCrudRoutes } from "../../codegen/runtime/drizzle-fastify/index";\n`); |
| 48 | + expect(findRuntimeBoundaryCrossings(root, [f])).toEqual([]); |
| 49 | + }); |
| 50 | + |
| 51 | + test("output that imports the published package is not flagged", () => { |
| 52 | + const root = project(); |
| 53 | + mkdirSync(join(root, "apps/api"), { recursive: true }); |
| 54 | + writeFileSync(join(root, "apps/api/package.json"), "{}"); |
| 55 | + const f = gen(root, "apps/api/src/generated/User.routes.ts", |
| 56 | + `import { mountCrudRoutes } from "@metaobjectsdev/runtime-ts/drizzle-fastify";\n`); |
| 57 | + expect(findRuntimeBoundaryCrossings(root, [f])).toEqual([]); |
| 58 | + }); |
| 59 | + |
| 60 | + test("a runtime copy moved inside the package is not flagged", () => { |
| 61 | + const root = project(); |
| 62 | + mkdirSync(join(root, "apps/api/codegen/runtime/drizzle-fastify"), { recursive: true }); |
| 63 | + writeFileSync(join(root, "apps/api/package.json"), "{}"); |
| 64 | + const f = gen(root, "apps/api/src/generated/User.routes.ts", |
| 65 | + `import { mountCrudRoutes } from "../../codegen/runtime/drizzle-fastify/index";\n`); |
| 66 | + expect(findRuntimeBoundaryCrossings(root, [f])).toEqual([]); |
| 67 | + }); |
| 68 | +}); |
0 commit comments