@suluk/journeys 0.3.1 → 0.4.0

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.
@@ -0,0 +1,129 @@
1
+ import { test, expect, describe } from "bun:test";
2
+ import { parseFeature } from "../src/gherkin";
3
+ import {
4
+ extractPublicRows,
5
+ buildExampleObject,
6
+ promoteExampleIntoZod,
7
+ promoteFeatureExamples,
8
+ } from "../src/promote";
9
+ import type { JsonSchema } from "@suluk/examples";
10
+
11
+ /**
12
+ * C040-P4 — promote a tester's @public Examples row into the Zod source as .meta({ examples }). The source-write is
13
+ * MARKED, IDEMPOTENT, and NEVER CLOBBERS a hand-authored example.
14
+ */
15
+
16
+ describe("extractPublicRows — a @public Examples block's first row", () => {
17
+ const feature = parseFeature(
18
+ [
19
+ "Feature: f",
20
+ " Scenario Outline: charge",
21
+ " When I charge",
22
+ " @public",
23
+ " Examples:",
24
+ " | amountCents | currency |",
25
+ " | 100 | usd |",
26
+ " | 250 | eur |",
27
+ " Scenario Outline: private",
28
+ " When I charge",
29
+ " Examples:",
30
+ " | amountCents |",
31
+ " | 999 |",
32
+ ].join("\n"),
33
+ );
34
+
35
+ test("only the @public-tagged block is returned, first row only", () => {
36
+ const rows = extractPublicRows([feature]);
37
+ expect(rows).toHaveLength(1);
38
+ expect(rows[0].scenario).toBe("charge");
39
+ expect(rows[0].headers).toEqual(["amountCents", "currency"]);
40
+ expect(rows[0].row).toEqual(["100", "usd"]);
41
+ });
42
+ });
43
+
44
+ describe("buildExampleObject — typed coercion, wiring tokens skipped", () => {
45
+ const body: JsonSchema = {
46
+ type: "object",
47
+ properties: { amountCents: { type: "integer" }, currency: { type: "string" }, live: { type: "boolean" } },
48
+ };
49
+ test("coerces cells by the field type", () => {
50
+ expect(buildExampleObject(["amountCents", "currency", "live"], ["100", "usd", "true"], body)).toEqual({
51
+ amountCents: 100,
52
+ currency: "usd",
53
+ live: true,
54
+ });
55
+ });
56
+ test("a sourced wiring token <op.select> is not a concrete public value — skipped", () => {
57
+ expect(buildExampleObject(["amountCents", "subId"], ["100", "<createSubscription.id>"], body)).toEqual({ amountCents: 100 });
58
+ });
59
+ });
60
+
61
+ describe("promoteExampleIntoZod — marked, idempotent, never-clobber", () => {
62
+ const base = `import { z } from "zod";\nexport const ChargeBody = z.object({ amountCents: z.number().int() });\n`;
63
+
64
+ test("appends a marked .meta({ examples }) to a schema with no meta", () => {
65
+ const r = promoteExampleIntoZod(base, "ChargeBody", { amountCents: 100 }, "charge");
66
+ expect(r.changed).toBe(true);
67
+ expect(r.source).toContain("@suluk-public: charge");
68
+ expect(r.source).toContain('examples: [{"amountCents":100}]');
69
+ expect(r.source).toContain("z.object({ amountCents: z.number().int() }).meta(");
70
+ });
71
+
72
+ test("is idempotent — re-promoting REPLACES the marked block (single .meta), no double-append", () => {
73
+ const once = promoteExampleIntoZod(base, "ChargeBody", { amountCents: 100 }, "charge").source;
74
+ const twice = promoteExampleIntoZod(once, "ChargeBody", { amountCents: 250 }, "charge").source;
75
+ expect(twice.match(/@suluk-public/g)).toHaveLength(1);
76
+ expect(twice).toContain('examples: [{"amountCents":250}]');
77
+ expect(twice).not.toContain('examples: [{"amountCents":100}]');
78
+ });
79
+
80
+ test("REFUSES to clobber a hand-authored top-level .meta({ examples })", () => {
81
+ const hand = `export const ChargeBody = z.object({ amountCents: z.number() }).meta({ examples: [{ amountCents: 1 }] });\n`;
82
+ const r = promoteExampleIntoZod(hand, "ChargeBody", { amountCents: 100 }, "charge");
83
+ expect(r.changed).toBe(false);
84
+ expect(r.reason).toMatch(/not clobbering/i);
85
+ expect(r.source).toBe(hand);
86
+ });
87
+
88
+ test("appends safely after a non-example .meta (description) — preserved", () => {
89
+ const desc = `export const ChargeBody = z.object({ amountCents: z.number() }).meta({ description: "a charge" });\n`;
90
+ const r = promoteExampleIntoZod(desc, "ChargeBody", { amountCents: 100 }, "charge");
91
+ expect(r.changed).toBe(true);
92
+ expect(r.source).toContain('description: "a charge"');
93
+ expect(r.source).toContain("@suluk-public: charge");
94
+ });
95
+
96
+ test("a PROPERTY-level .meta is not mistaken for the top-level example meta", () => {
97
+ const prop = `export const ChargeBody = z.object({ amountCents: z.number().meta({ description: "cents" }) });\n`;
98
+ const r = promoteExampleIntoZod(prop, "ChargeBody", { amountCents: 100 }, "charge");
99
+ expect(r.changed).toBe(true);
100
+ // the property meta is untouched; a NEW top-level meta is appended
101
+ expect(r.source).toContain('z.number().meta({ description: "cents" })');
102
+ expect(r.source).toContain(") }).meta(/* @suluk-public");
103
+ });
104
+
105
+ test("a missing schema var is reported, not edited", () => {
106
+ const r = promoteExampleIntoZod(base, "NopeBody", { x: 1 }, "x");
107
+ expect(r.changed).toBe(false);
108
+ expect(r.reason).toMatch(/not found/);
109
+ });
110
+ });
111
+
112
+ describe("promoteFeatureExamples — orchestrated over a feature, target injected", () => {
113
+ const source = `export const ChargeBody = z.object({ amountCents: z.number().int() });\n`;
114
+ const feature = parseFeature(
115
+ ["Feature: f", " Scenario Outline: charge", " When I charge", " @public", " Examples:", " | amountCents |", " | 100 |"].join("\n"),
116
+ );
117
+
118
+ test("applies the public row to the resolved schema var", () => {
119
+ const r = promoteFeatureExamples(source, [feature], (sc) => (sc === "charge" ? { schemaVar: "ChargeBody", bodySchema: { type: "object", properties: { amountCents: { type: "integer" } } } } : null));
120
+ expect(r.applied).toEqual([{ scenario: "charge", schemaVar: "ChargeBody", reason: expect.stringContaining("promoted") }]);
121
+ expect(r.source).toContain('examples: [{"amountCents":100}]');
122
+ });
123
+
124
+ test("an unresolved scenario is skipped, not applied", () => {
125
+ const r = promoteFeatureExamples(source, [feature], () => null);
126
+ expect(r.applied).toHaveLength(0);
127
+ expect(r.skipped[0].reason).toMatch(/no target/);
128
+ });
129
+ });