tina4-nodejs 3.13.157 → 3.13.159

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1482,6 +1482,12 @@ export function generateMigration(
1482
1482
  // Build SQL columns from fields
1483
1483
  const fields = fieldsOverride || parseFields((flags.fields as string) || "");
1484
1484
  const isCreate = name.startsWith("create_") || fieldsOverride !== undefined;
1485
+ // A `create_*_view` name is a VIEW, not a table. Emitting a runnable
1486
+ // `CREATE TABLE <name>_view` made migrate build a table and never the view,
1487
+ // silently (#297). A view needs a SELECT, so scaffold commented guidance with
1488
+ // tina4:edit markers and no executable SQL, so the runner keeps it pending
1489
+ // until it is written (#288).
1490
+ const isView = isCreate && fieldsOverride === undefined && table.endsWith("_view");
1485
1491
 
1486
1492
  const fileName = `${ts}_${name}.sql`;
1487
1493
  const path = join(dir, fileName);
@@ -1507,7 +1513,14 @@ export function generateMigration(
1507
1513
  // parsed label (the migration runner's splitStatements strips `--` line
1508
1514
  // comments, so a comma-less marker line inside a DDL body is still
1509
1515
  // syntactically inert on every engine).
1510
- if (isCreate) {
1516
+ if (isView) {
1517
+ upSql =
1518
+ `-- tina4:edit write the CREATE VIEW statement, then remove the\n` +
1519
+ `-- tina4:edit lines. For example:\n` +
1520
+ `-- CREATE VIEW ${table} AS\n` +
1521
+ `-- SELECT id, /* columns */ FROM source_table WHERE /* ... */;`;
1522
+ downSql = `DROP VIEW IF EXISTS ${table};`;
1523
+ } else if (isCreate) {
1511
1524
  const colLines = [" id INTEGER PRIMARY KEY AUTOINCREMENT"];
1512
1525
  for (const [fname, ftype] of fields) {
1513
1526
  const info = FIELD_TYPE_MAP[ftype] || FIELD_TYPE_MAP.string;
@@ -1533,11 +1546,15 @@ export function generateMigration(
1533
1546
  }
1534
1547
 
1535
1548
  const now = isoNow();
1549
+ // The main .sql holds ONLY the UP migration. The runner executes the WHOLE
1550
+ // file (comments stripped), so embedding the DOWN here made it CREATE then
1551
+ // immediately DROP the table — a generated migration silently undid itself.
1552
+ // Rollback reads the sibling .down.sql, never this file's DOWN. Keep DOWN out
1553
+ // of the main file (parity with the Python/PHP/Ruby masters).
1536
1554
  const content =
1537
1555
  `-- Migration: ${name}\n` +
1538
1556
  `-- Created: ${now}\n\n` +
1539
- `-- UP\n${upSql}\n\n` +
1540
- `-- DOWN\n${downSql}\n`;
1557
+ `-- UP\n${upSql}\n`;
1541
1558
 
1542
1559
  writeFileSafe(path, content);
1543
1560
 
@@ -1550,10 +1567,11 @@ export function generateMigration(
1550
1567
 
1551
1568
  writeFileSafe(downPath, downContent);
1552
1569
 
1553
- // Co-emit a real up/down migration test — only for CREATE migrations (a
1554
- // placeholder ALTER migration has no asserted schema yet). Suppressed when a
1555
- // model generates its matching migration (the model's own test covers schema).
1556
- if (emitTest && isCreate) emitMigrationTest(name, table);
1570
+ // Co-emit a real up/down migration test — only for CREATE TABLE migrations (a
1571
+ // placeholder ALTER, or a VIEW stub that still needs its SELECT, has no
1572
+ // asserted schema yet). Suppressed when a model generates its matching
1573
+ // migration (the model's own test covers schema).
1574
+ if (emitTest && isCreate && !isView) emitMigrationTest(name, table);
1557
1575
  }
1558
1576
 
1559
1577
  // ── Middleware ───────────────────────────────────────────────────────
@@ -2541,9 +2559,11 @@ function emitRouteStubTest(route: string): void {
2541
2559
  * Routing test for ${route} — no mocks, real Router + route discovery.
2542
2560
  *
2543
2561
  * Generated with src/routes/api/${route}/ by \`tina4nodejs generate route
2544
- * ${route}\` (no --model). The handlers are AI-FILL stubs that throw until you
2545
- * implement them, so this tests what IS live on generation: all five routes
2546
- * register on the REAL Router, and the list handler fails loud until filled.
2562
+ * ${route}\` (no --model). The handlers start as AI-FILL stubs that throw until
2563
+ * you implement them. This tests what is live on generation: all five routes
2564
+ * register on the REAL Router, and the list handler is wired and callable. It
2565
+ * stays GREEN whether the stub still throws OR you have filled it in —
2566
+ * implementing the handler never turns this test red.
2547
2567
  * Run with: npx tsx tests/${route}.test.ts
2548
2568
  */
2549
2569
  import { dirname, resolve } from "node:path";
@@ -2560,9 +2580,15 @@ for (const sig of ["GET /api/${route}", "POST /api/${route}", "GET /api/${route}
2560
2580
  assert(\`route registered: \${sig}\`, sigs.includes(sig));
2561
2581
  }
2562
2582
 
2563
- // The scaffolded list handler is a loud AI-FILL stub — it throws until filled.
2564
- await assertThrows("list handler is a live stub (throws until filled)",
2565
- () => listHandler({} as never, {} as never));`;
2583
+ // The list handler is WIRED: a loud AI-FILL stub (throws) on generation. This
2584
+ // stays green whether it still throws OR you have filled it in, so doing the
2585
+ // work never turns the suite red.
2586
+ assert("list handler is callable", typeof listHandler === "function");
2587
+ try {
2588
+ await listHandler({} as never, {} as never);
2589
+ } catch {
2590
+ // still the scaffold stub (throws until filled) — expected
2591
+ }`;
2566
2592
  writeTest(route, standaloneTest(doc, body));
2567
2593
  }
2568
2594
 
@@ -2651,8 +2677,14 @@ ServiceRunner.register(service.name, service.handler, { interval: (service as {
2651
2677
  assert("registers on a real ServiceRunner", ServiceRunner.list().some((s) => s.name === "${snake}"));
2652
2678
  ServiceRunner.remove("${snake}");
2653
2679
 
2654
- // The scaffolded task body is an AI-FILL stub — it throws until filled.
2655
- await assertThrows("task is a live stub (throws until filled)", () => ${camel}Task({} as never));`;
2680
+ // The task is WIRED: a loud AI-FILL stub (throws) on generation. This stays
2681
+ // green whether it still throws OR you have filled it in.
2682
+ assert("task is callable", typeof ${camel}Task === "function");
2683
+ try {
2684
+ await ${camel}Task({} as never);
2685
+ } catch {
2686
+ // still the scaffold stub (throws until filled) — expected
2687
+ }`;
2656
2688
  writeTest(snake, standaloneTest(doc, body));
2657
2689
  }
2658
2690
 
@@ -2676,8 +2708,14 @@ assert("the job is really on the queue", new Queue({ topic: "${topic}" }).size()
2676
2708
  assert("consumer default export is a daemon", worker.daemon === true);
2677
2709
  assert("consumer handler is wired", typeof worker.handler === "function");
2678
2710
 
2679
- // The per-job handler is an AI-FILL stub — it throws until filled.
2680
- await assertThrows("handle is a live stub (throws until filled)", () => handle${pascal}({}));`;
2711
+ // The per-job handler is WIRED: a loud AI-FILL stub (throws) on generation.
2712
+ // This stays green whether it still throws OR you have filled it in.
2713
+ assert("handle is callable", typeof handle${pascal} === "function");
2714
+ try {
2715
+ await handle${pascal}({});
2716
+ } catch {
2717
+ // still the scaffold stub (throws until filled) — expected
2718
+ }`;
2681
2719
  writeTest(slug, standaloneTest(doc, body));
2682
2720
  }
2683
2721
 
@@ -2748,9 +2786,14 @@ import { ${handler} } from "../src/routes/ws_${base}.js"; // importing register
2748
2786
  const closed = await ${handler}(null as never, "close", "");
2749
2787
  assert("close event handled cleanly", closed === undefined);
2750
2788
 
2751
- // The "message" branch is an AI-FILL stub — it throws until filled.
2752
- await assertThrows("message branch is a live stub (throws until filled)",
2753
- () => ${handler}(null as never, "message", "hi"));`;
2789
+ // The "message" branch is WIRED: a loud AI-FILL stub (throws) on generation.
2790
+ // This stays green whether it still throws OR you have filled it in.
2791
+ try {
2792
+ await ${handler}(null as never, "message", "hi");
2793
+ assert("message branch is callable", true);
2794
+ } catch {
2795
+ assert("message branch is callable", true); // still the scaffold stub
2796
+ }`;
2754
2797
  writeTest(`ws_${base}`, standaloneTest(doc, body));
2755
2798
  }
2756
2799
 
@@ -2769,9 +2812,15 @@ import { Events } from "tina4-nodejs";
2769
2812
  import "../src/listeners/${slug}.js"; // importing registers the listener via Events.on()`;
2770
2813
  const body = `assert("listener registered on the real event bus", Events.listeners("${event}").length >= 1);
2771
2814
 
2772
- // strict emit re-raises the stub error, proving the listener actually ran.
2773
- await assertThrows("emitting the event reaches the (stub) listener",
2774
- () => Events.emit("${event}", { strict: true }, { id: 1 }));`;
2815
+ // Emitting reaches the listener. It starts as a loud AI-FILL stub (a strict emit
2816
+ // re-raises); once you implement it this test STILL passes, so doing the work
2817
+ // never turns the suite red.
2818
+ try {
2819
+ await Events.emit("${event}", { strict: true }, { id: 1 });
2820
+ assert("emitting reaches the listener", true);
2821
+ } catch {
2822
+ assert("emitting reaches the listener", true); // still the scaffold stub
2823
+ }`;
2775
2824
  writeTest(slug, standaloneTest(doc, body));
2776
2825
  }
2777
2826