@canonical/summon 0.37.0 → 0.38.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.
Files changed (36) hide show
  1. package/dist/src/index.d.ts +15 -0
  2. package/dist/src/index.js +16 -0
  3. package/dist/src/index.js.map +1 -0
  4. package/package.json +17 -12
  5. package/src/bin.tsx +238 -0
  6. package/src/completion.test.ts +202 -0
  7. package/src/completion.ts +492 -0
  8. package/src/components/App.askMissing.test.tsx +164 -0
  9. package/src/components/App.confirmGate.test.tsx +111 -0
  10. package/src/components/App.escBack.test.tsx +98 -0
  11. package/src/components/App.invalidAnswer.test.tsx +71 -0
  12. package/src/components/App.tsx +1305 -0
  13. package/src/components/ExecutionProgress.tsx +222 -0
  14. package/src/components/FileTreePreview.tsx +97 -0
  15. package/src/components/PromptSequence.tsx +558 -0
  16. package/src/components/Spinner.tsx +36 -0
  17. package/src/components/index.ts +16 -0
  18. package/src/components/undoPlan.test.ts +99 -0
  19. package/src/components/undoPlan.ts +70 -0
  20. package/src/discovery/index.ts +1 -0
  21. package/src/discovery/loadGenerator.ts +40 -0
  22. package/src/display/index.ts +2 -0
  23. package/src/display/printGeneratorHelp.ts +130 -0
  24. package/src/display/printNode.ts +76 -0
  25. package/src/index.ts +16 -0
  26. package/src/registration/buildCommandBarrel.test.ts +80 -0
  27. package/src/registration/buildCommandBarrel.ts +66 -0
  28. package/src/registration/conformance.subprocess.test.ts +192 -0
  29. package/src/registration/index.ts +2 -0
  30. package/src/registration/interaction.subprocess.test.ts +458 -0
  31. package/src/registration/registerFromBarrel.tsx +501 -0
  32. package/src/registration/resolveMode.test.ts +118 -0
  33. package/src/registration/resolveMode.ts +52 -0
  34. package/src/testing/globalSetup.test.ts +165 -0
  35. package/src/testing/globalSetup.ts +501 -0
  36. package/src/ui/index.tsx +31 -0
@@ -0,0 +1,111 @@
1
+ /**
2
+ * The confirm gate's RE-generate goes through the same `generateTask` choke
3
+ * point as the prompts-complete generate (its second call site): a STATEFUL
4
+ * generator whose `generate` succeeds for the preview and throws on the
5
+ * confirm re-generate must land in the App's error phase — GENERATE_ERROR,
6
+ * exit code 1 — not in Ink's error boundary (a crash box that owns no exit
7
+ * code: the silent-success class the cross-CLI matrix forbids). Rendered with
8
+ * ink-testing-library, driven to `Proceed?` and confirmed with `y`.
9
+ */
10
+
11
+ import type { GeneratorDefinition } from "@canonical/summon-core";
12
+ import { pure, task } from "@canonical/task";
13
+ import { render } from "ink-testing-library";
14
+ import { afterEach, describe, expect, it } from "vitest";
15
+ import { App } from "./App.js";
16
+
17
+ let generateCalls = 0;
18
+ const stateful: GeneratorDefinition = {
19
+ meta: {
20
+ name: "fixture/stateful",
21
+ displayName: "stateful",
22
+ description: "A fixture that only survives its first generate",
23
+ version: "0.0.1",
24
+ },
25
+ prompts: [{ name: "title", type: "text", message: "Title:", default: "t" }],
26
+ generate: () => {
27
+ generateCalls += 1;
28
+ if (generateCalls > 1) {
29
+ throw new Error("second generate exploded");
30
+ }
31
+ return task(pure(undefined)).unwrap();
32
+ },
33
+ };
34
+
35
+ const tick = (ms = 25) => new Promise((resolve) => setTimeout(resolve, ms));
36
+
37
+ /**
38
+ * Poll until a frame satisfies `check`, then return it.
39
+ *
40
+ * The confirm gate is NOT the mount commit — it is the tail of an async chain
41
+ * (`PromptSequence`'s `onComplete` -> `handlePromptsComplete` -> `generateTask`
42
+ * -> `setState({phase:"confirming"})`). A single fixed tick can therefore read a
43
+ * frame the gate has not painted yet, which is exactly what happened under
44
+ * suite parallelism: 2 failures in 84 loaded runs, 0 in 30 unloaded, and
45
+ * 216/216 when the file ran alone — i.e. visible only in the condition CI runs
46
+ * in. Lifted from `summon-core`'s `wizard.test.tsx`.
47
+ */
48
+ const waitForFrame = async (
49
+ read: () => string | undefined,
50
+ check: (frame: string) => boolean,
51
+ timeout = 15_000,
52
+ ): Promise<string> => {
53
+ const deadline = Date.now() + timeout;
54
+ let frame = read() ?? "";
55
+ while (!check(frame) && Date.now() < deadline) {
56
+ await tick(15);
57
+ frame = read() ?? "";
58
+ }
59
+ return frame;
60
+ };
61
+
62
+ describe("App — the confirm gate's re-generate lands in the error phase", () => {
63
+ // The error phase sets process.exitCode; never leak it across cases (or
64
+ // into the worker's own exit).
65
+ afterEach(() => {
66
+ process.exitCode = undefined;
67
+ generateCalls = 0;
68
+ });
69
+
70
+ it("a generate that throws only at the gate renders GENERATE_ERROR, exit code 1", async () => {
71
+ // A complete answer set goes straight to the gate (the askMissing suite
72
+ // proves that shape); the FIRST generate — the preview's — succeeds.
73
+ const { lastFrame, stdin, unmount } = render(
74
+ <App generator={stateful} askMissing answers={{ title: "Widget" }} />,
75
+ );
76
+ expect(
77
+ await waitForFrame(lastFrame, (frame) => frame.includes("Proceed?")),
78
+ ).toContain("Proceed?");
79
+ expect(generateCalls).toBe(1);
80
+
81
+ // Confirming re-generates; the second call throws — the one path that
82
+ // could bypass both the choke point and handleExecutionError. Ink
83
+ // attaches its stdin listener asynchronously in a fresh worker
84
+ // (measured: one 25ms tick is not always enough when the app goes
85
+ // STRAIGHT to the gate), so the `y` is re-written until the gate takes
86
+ // it — extra writes are harmless: each accepted `y` re-runs the same
87
+ // throwing re-generate, and once the error phase renders, gate input
88
+ // is inactive.
89
+ // The exit code is owned by a passive effect (App.tsx:996) that flushes
90
+ // AFTER the commit painting the error, so a loop exiting on the FRAME alone
91
+ // can read a rendered message and a still-unset exitCode. Wait for the
92
+ // settled PAIR — the remedy `App.invalidAnswer.test.tsx` already took, in
93
+ // the file it copied this shape from. Measured on the real package: 4 of 8
94
+ // runs failed here under contention, every failure the code, never the frame.
95
+ const settled = () =>
96
+ (lastFrame() ?? "").includes("Code: GENERATE_ERROR") &&
97
+ process.exitCode !== undefined;
98
+ const deadline = Date.now() + 15_000;
99
+ while (!settled() && Date.now() < deadline) {
100
+ stdin.write("y");
101
+ await tick(50);
102
+ }
103
+ const frame = lastFrame() ?? "";
104
+ expect(frame).toContain("✗ Error: second generate exploded");
105
+ expect(frame).toContain("Code: GENERATE_ERROR");
106
+ // The error phase, not the raw throw: no stack frames in the frame.
107
+ expect(frame).not.toContain("at generate");
108
+ expect(process.exitCode).toBe(1);
109
+ unmount();
110
+ }, 20_000);
111
+ });
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Esc from the confirm gate re-asks EVERY prompt (the `reasking` design), and
3
+ * the re-asked prompts must be seeded with the answers already collected —
4
+ * not re-initialized from their declared defaults. The defect class: a
5
+ * default-true confirm given explicitly as `--no-with-stories` reached the
6
+ * gate as `false`; esc + Enter then submitted the DECLARED default (`true`),
7
+ * silently overwriting the explicit answer. Rendered with
8
+ * ink-testing-library, driven gate → esc → Enter → gate.
9
+ */
10
+
11
+ import type { GeneratorDefinition } from "@canonical/summon-core";
12
+ import { pure, task } from "@canonical/task";
13
+ import { render } from "ink-testing-library";
14
+ import { afterEach, describe, expect, it } from "vitest";
15
+ import { App } from "./App.js";
16
+
17
+ /** Every answer set the preview generate was handed, in call order. */
18
+ const generateAnswers: Array<Record<string, unknown>> = [];
19
+ const fixture: GeneratorDefinition = {
20
+ meta: {
21
+ name: "fixture/esc-back",
22
+ displayName: "esc-back",
23
+ description: "A fixture recording the answers each generate receives",
24
+ version: "0.0.1",
25
+ },
26
+ prompts: [
27
+ {
28
+ name: "withStories",
29
+ type: "confirm",
30
+ message: "Include stories?",
31
+ default: true,
32
+ },
33
+ ],
34
+ generate: (answers) => {
35
+ generateAnswers.push(answers as Record<string, unknown>);
36
+ return task(pure(undefined)).unwrap();
37
+ },
38
+ };
39
+
40
+ const tick = (ms = 25) => new Promise((resolve) => setTimeout(resolve, ms));
41
+
42
+ /** Poll until a frame satisfies `check`, then return it (App.confirmGate's shape). */
43
+ const waitForFrame = async (
44
+ read: () => string | undefined,
45
+ check: (frame: string) => boolean,
46
+ timeout = 15_000,
47
+ ): Promise<string> => {
48
+ const deadline = Date.now() + timeout;
49
+ let frame = read() ?? "";
50
+ while (!check(frame) && Date.now() < deadline) {
51
+ await tick(15);
52
+ frame = read() ?? "";
53
+ }
54
+ return frame;
55
+ };
56
+
57
+ describe("App — esc from the confirm gate keeps explicit answers", () => {
58
+ afterEach(() => {
59
+ process.exitCode = undefined;
60
+ generateAnswers.length = 0;
61
+ });
62
+
63
+ it("Enter on a re-asked default-true confirm re-submits the explicit false", async () => {
64
+ // The explicit `--no-with-stories` shape: a complete answer set goes
65
+ // straight to the gate (the askMissing suite proves that shape).
66
+ const { lastFrame, stdin, unmount } = render(
67
+ <App generator={fixture} askMissing answers={{ withStories: false }} />,
68
+ );
69
+ await waitForFrame(lastFrame, (frame) => frame.includes("Proceed?"));
70
+ expect(generateAnswers[0]?.withStories).toBe(false);
71
+
72
+ // Esc re-opens the wizard with every prompt askable again. Ink attaches
73
+ // its stdin listener asynchronously, so the escape is re-written until
74
+ // the wizard renders (App.confirmGate's remedy). The gate frame also
75
+ // shows the completed "Include stories?" row, so the wizard is detected
76
+ // by its progress header, which only the prompting phase paints.
77
+ await waitForFrame(lastFrame, (frame) => {
78
+ if (frame.includes("Step 1 of 1")) return true;
79
+ stdin.write("\u001B");
80
+ return false;
81
+ });
82
+ // The hint reflects the SEEDED answer (false → `y/N`), not the declared
83
+ // default's `Y/n`.
84
+ expect(lastFrame() ?? "").toContain("(y/N)");
85
+
86
+ // Enter accepts what is shown — the explicit false — and returns to the
87
+ // gate; the re-generate must still see it.
88
+ await waitForFrame(lastFrame, (frame) => {
89
+ if (frame.includes("Proceed?")) return true;
90
+ stdin.write("\r");
91
+ return false;
92
+ });
93
+ const last = generateAnswers[generateAnswers.length - 1];
94
+ expect(generateAnswers.length).toBeGreaterThan(1);
95
+ expect(last?.withStories).toBe(false);
96
+ unmount();
97
+ }, 20_000);
98
+ });
@@ -0,0 +1,71 @@
1
+ /**
2
+ * A generator-raised typed invalid answer (summon-core's
3
+ * `invalidAnswersError`, code GENERATOR_INVALID_ANSWER — a cross-answer
4
+ * constraint, two answers only valid together; no shipped generator raises
5
+ * one today, so a fixture drives it) renders as the App's clean
6
+ * error phase — message + code, no crash box, no stack — on the run path
7
+ * (pre-filled answers), the same UI every execution failure gets.
8
+ */
9
+
10
+ import {
11
+ type GeneratorDefinition,
12
+ invalidAnswersError,
13
+ } from "@canonical/summon-core";
14
+ import { pure, task } from "@canonical/task";
15
+ import { render } from "ink-testing-library";
16
+ import { afterEach, describe, expect, it } from "vitest";
17
+ import { App } from "./App.js";
18
+
19
+ const guarded: GeneratorDefinition = {
20
+ meta: {
21
+ name: "fixture/guarded",
22
+ displayName: "guarded",
23
+ description: "A guarded fixture",
24
+ version: "0.0.1",
25
+ },
26
+ prompts: [{ name: "ok", type: "confirm", message: "OK?", default: true }],
27
+ generate: (answers) => {
28
+ if ((answers as { ok?: unknown }).ok !== true) {
29
+ throw invalidAnswersError("OK is required — drop --no-ok.");
30
+ }
31
+ return task(pure(undefined)).unwrap();
32
+ },
33
+ };
34
+
35
+ const tick = () => new Promise((resolve) => setTimeout(resolve, 25));
36
+
37
+ describe("App — a typed invalid answer is the clean error phase, not a crash", () => {
38
+ // The error phase sets process.exitCode; never leak it across cases (or
39
+ // into the worker's own exit).
40
+ afterEach(() => {
41
+ process.exitCode = undefined;
42
+ });
43
+
44
+ it("renders the message and its code on the run path, exit code 2", async () => {
45
+ const { lastFrame, unmount } = render(
46
+ <App generator={guarded} preview={false} answers={{ ok: false }} />,
47
+ );
48
+ // The error phase's exit code is owned by a passive effect (App.tsx:996),
49
+ // which flushes AFTER the commit that paints the error — so one fixed
50
+ // tick can read the rendered message and a still-unset exitCode
51
+ // (measured: 4 of 72 runs under load, always the code, never the frame).
52
+ // Poll for the settled pair; a real regression still falls through to the
53
+ // assertions below and fails there, loudly.
54
+ const settled = () =>
55
+ process.exitCode !== undefined &&
56
+ (lastFrame() ?? "").includes("Code: GENERATOR_INVALID_ANSWER");
57
+ const deadline = Date.now() + 15_000;
58
+ while (!settled() && Date.now() < deadline) {
59
+ await tick();
60
+ }
61
+ const frame = lastFrame() ?? "";
62
+ expect(frame).toContain("✗ Error: OK is required — drop --no-ok.");
63
+ expect(frame).toContain("Code: GENERATOR_INVALID_ANSWER");
64
+ // The error phase, not the raw throw: no stack frames in the frame.
65
+ expect(frame).not.toContain("at generate");
66
+ // The usage class of the cross-CLI matrix: a typed invalid answer exits 2
67
+ // in the run/wizard arms too — rendered in the App, never exit 0.
68
+ expect(process.exitCode).toBe(2);
69
+ unmount();
70
+ }, 20_000);
71
+ });