|
| 1 | +// #194 item 3 — a value-object-only project may omit dbImport/dialect (it generates |
| 2 | +// zero DB code, so requiring them was a dead-but-mandatory tsc obligation). A model |
| 3 | +// that DOES emit DB code must still set them, and omitting them errors clearly rather |
| 4 | +// than silently defaulting (which would e.g. emit sqlite for a Postgres project). |
| 5 | +import { describe, test, expect, beforeEach, afterEach } from "bun:test"; |
| 6 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 7 | +import { tmpdir } from "node:os"; |
| 8 | +import { join } from "node:path"; |
| 9 | +import { MetaDataLoader, InMemoryStringSource } from "@metaobjectsdev/metadata"; |
| 10 | +import { runGen } from "../src/runner.js"; |
| 11 | +import { entityFile } from "../src/generators/entity-file.js"; |
| 12 | +import type { MetaobjectsGenConfig } from "../src/metaobjects-config.js"; |
| 13 | + |
| 14 | +let tmp: string; |
| 15 | +beforeEach(() => { tmp = mkdtempSync(join(tmpdir(), "codegen-vo-only-")); }); |
| 16 | +afterEach(() => { rmSync(tmp, { recursive: true, force: true }); }); |
| 17 | + |
| 18 | +async function load(children: unknown[]) { |
| 19 | + const json = JSON.stringify({ "metadata.root": { package: "app", children } }); |
| 20 | + const { root, errors } = await new MetaDataLoader().load([new InMemoryStringSource(json)]); |
| 21 | + expect(errors).toEqual([]); |
| 22 | + return root; |
| 23 | +} |
| 24 | + |
| 25 | +const VALUE_ONLY = [ |
| 26 | + { "object.value": { name: "Slots", children: [ |
| 27 | + { "field.string": { name: "goal", "@required": true } }, |
| 28 | + { "field.string": { name: "note" } }, |
| 29 | + ] } }, |
| 30 | +]; |
| 31 | + |
| 32 | +const ENTITY = [ |
| 33 | + { "object.entity": { name: "User", children: [ |
| 34 | + { "field.long": { name: "id" } }, |
| 35 | + { "field.string": { name: "email", "@required": true } }, |
| 36 | + { "identity.primary": { name: "pk", "@fields": ["id"], "@generation": "increment" } }, |
| 37 | + ] } }, |
| 38 | +]; |
| 39 | + |
| 40 | +// dbImport/dialect deliberately OMITTED — legal for a value-object-only model. |
| 41 | +// outDir is filled per-test with the beforeEach tmp dir (no writes into the package). |
| 42 | +const configNoDb = (): MetaobjectsGenConfig => |
| 43 | + ({ outDir: tmp, extStyle: "js", generators: [entityFile()] } as MetaobjectsGenConfig); |
| 44 | + |
| 45 | +describe("#194 item 3 — dbImport/dialect optional for value-object-only projects", () => { |
| 46 | + test("a value-object-only model generates with NO dbImport/dialect set (no throw)", async () => { |
| 47 | + const root = await load(VALUE_ONLY); |
| 48 | + const result = await runGen({ config: configNoDb(), metadata: root, projectRoot: tmp }); |
| 49 | + // It runs to completion; a value object emits no query/route DB code, so the |
| 50 | + // absent dbImport/dialect are never read. |
| 51 | + expect(result.warnings.some((w) => w.includes("missing"))).toBe(false); |
| 52 | + expect(Array.isArray(result.files)).toBe(true); |
| 53 | + }); |
| 54 | + |
| 55 | + test("a model with a DB entity but no dbImport/dialect throws a clear error naming the entity", async () => { |
| 56 | + const root = await load(ENTITY); |
| 57 | + let err: Error | undefined; |
| 58 | + try { |
| 59 | + await runGen({ config: configNoDb(), metadata: root, projectRoot: tmp }); |
| 60 | + } catch (e) { |
| 61 | + err = e as Error; |
| 62 | + } |
| 63 | + expect(err).toBeDefined(); |
| 64 | + expect(err!.message).toContain("dialect and dbImport"); |
| 65 | + expect(err!.message).toContain("User"); // names the DB-emitting object |
| 66 | + }); |
| 67 | + |
| 68 | + test("a DB entity WITH dbImport/dialect set generates normally (no regression)", async () => { |
| 69 | + const root = await load(ENTITY); |
| 70 | + const cfg = { ...configNoDb(), dbImport: "./db", dialect: "postgres" as const } as MetaobjectsGenConfig; |
| 71 | + const result = await runGen({ config: cfg, metadata: root, projectRoot: tmp }); |
| 72 | + expect(result.files.length).toBeGreaterThan(0); |
| 73 | + }); |
| 74 | +}); |
0 commit comments