@hop-top/kit 0.4.0-alpha.1

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 (174) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/LICENSE +25 -0
  3. package/README.md +189 -0
  4. package/dist/aim.d.ts +116 -0
  5. package/dist/aim.js +417 -0
  6. package/dist/alias.d.ts +73 -0
  7. package/dist/alias.js +207 -0
  8. package/dist/api.d.ts +62 -0
  9. package/dist/api.js +165 -0
  10. package/dist/auth.d.ts +26 -0
  11. package/dist/auth.js +41 -0
  12. package/dist/cli.d.ts +309 -0
  13. package/dist/cli.js +885 -0
  14. package/dist/config.d.ts +97 -0
  15. package/dist/config.js +79 -0
  16. package/dist/errcorrect.d.ts +37 -0
  17. package/dist/errcorrect.js +69 -0
  18. package/dist/llm.d.ts +188 -0
  19. package/dist/llm.js +356 -0
  20. package/dist/output.d.ts +213 -0
  21. package/dist/output.js +447 -0
  22. package/dist/progress.d.ts +30 -0
  23. package/dist/progress.js +56 -0
  24. package/dist/provenance.d.ts +24 -0
  25. package/dist/provenance.js +32 -0
  26. package/dist/routellm.d.ts +64 -0
  27. package/dist/routellm.js +258 -0
  28. package/dist/rpc.d.ts +25 -0
  29. package/dist/rpc.js +101 -0
  30. package/dist/safety.d.ts +24 -0
  31. package/dist/safety.js +44 -0
  32. package/dist/scope.d.ts +121 -0
  33. package/dist/scope.js +380 -0
  34. package/dist/sqlstore.d.ts +84 -0
  35. package/dist/sqlstore.js +85 -0
  36. package/dist/stream.d.ts +35 -0
  37. package/dist/stream.js +52 -0
  38. package/dist/tui/index.d.ts +300 -0
  39. package/dist/tui/index.js +332 -0
  40. package/dist/upgrade.d.ts +95 -0
  41. package/dist/upgrade.js +115 -0
  42. package/dist/xdg.d.ts +169 -0
  43. package/dist/xdg.js +129 -0
  44. package/eslint.config.mjs +17 -0
  45. package/package.json +133 -0
  46. package/src/README.md +3 -0
  47. package/src/aim.test.ts +331 -0
  48. package/src/aim.ts +388 -0
  49. package/src/alias.test.ts +423 -0
  50. package/src/alias.ts +256 -0
  51. package/src/api.test.ts +190 -0
  52. package/src/api.ts +214 -0
  53. package/src/auth.test.ts +24 -0
  54. package/src/auth.ts +37 -0
  55. package/src/bus.test.ts +178 -0
  56. package/src/bus.ts +114 -0
  57. package/src/cli.test.ts +690 -0
  58. package/src/cli.ts +741 -0
  59. package/src/completion-values.test.ts +202 -0
  60. package/src/completion-values.ts +173 -0
  61. package/src/completion.test.ts +212 -0
  62. package/src/completion.ts +158 -0
  63. package/src/compliance.test.ts +134 -0
  64. package/src/compliance.ts +669 -0
  65. package/src/config.test.ts +110 -0
  66. package/src/config.ts +164 -0
  67. package/src/errcorrect.test.ts +134 -0
  68. package/src/errcorrect.ts +84 -0
  69. package/src/flagregister.test.ts +103 -0
  70. package/src/flagregister.ts +78 -0
  71. package/src/gen/crud_connect.ts +76 -0
  72. package/src/gen/crud_pb.ts +269 -0
  73. package/src/hint.test.ts +315 -0
  74. package/src/hint.ts +178 -0
  75. package/src/llm.test.ts +612 -0
  76. package/src/llm.ts +577 -0
  77. package/src/log.test.ts +145 -0
  78. package/src/log.ts +155 -0
  79. package/src/output/builtins.ts +34 -0
  80. package/src/output/dispatch.ts +195 -0
  81. package/src/output/flags.ts +151 -0
  82. package/src/output/format_help.ts +80 -0
  83. package/src/output/formatter.ts +167 -0
  84. package/src/output/formatters/csv.ts +72 -0
  85. package/src/output/formatters/json.ts +44 -0
  86. package/src/output/formatters/table.ts +44 -0
  87. package/src/output/formatters/text.ts +109 -0
  88. package/src/output/formatters/yaml.ts +47 -0
  89. package/src/output/projection.ts +126 -0
  90. package/src/output/registry.ts +88 -0
  91. package/src/output/template.ts +63 -0
  92. package/src/output.test.ts +117 -0
  93. package/src/output.ts +90 -0
  94. package/src/progress.test.ts +93 -0
  95. package/src/progress.ts +58 -0
  96. package/src/provenance.test.ts +59 -0
  97. package/src/provenance.ts +26 -0
  98. package/src/routellm.test.ts +179 -0
  99. package/src/routellm.ts +365 -0
  100. package/src/router/bert.test.ts +180 -0
  101. package/src/router/bert.ts +145 -0
  102. package/src/router/controller.test.ts +183 -0
  103. package/src/router/controller.ts +165 -0
  104. package/src/router/integration.test.ts +309 -0
  105. package/src/router/intent.test.ts +187 -0
  106. package/src/router/intent.ts +208 -0
  107. package/src/router/mf.test.ts +66 -0
  108. package/src/router/mf.ts +52 -0
  109. package/src/router/random.test.ts +23 -0
  110. package/src/router/random.ts +13 -0
  111. package/src/router/router.test.ts +31 -0
  112. package/src/router/router.ts +62 -0
  113. package/src/router/server.test.ts +193 -0
  114. package/src/router/server.ts +134 -0
  115. package/src/router/sw.test.ts +199 -0
  116. package/src/router/sw.ts +232 -0
  117. package/src/rpc.test.ts +47 -0
  118. package/src/rpc.ts +91 -0
  119. package/src/safety.test.ts +53 -0
  120. package/src/safety.ts +42 -0
  121. package/src/scope-defaults.json +72 -0
  122. package/src/scope.test.ts +177 -0
  123. package/src/scope.ts +382 -0
  124. package/src/setflag.test.ts +110 -0
  125. package/src/setflag.ts +102 -0
  126. package/src/sqlstore.test.ts +110 -0
  127. package/src/sqlstore.ts +144 -0
  128. package/src/stream.test.ts +69 -0
  129. package/src/stream.ts +47 -0
  130. package/src/textflag.test.ts +105 -0
  131. package/src/textflag.ts +68 -0
  132. package/src/toolspec.test.ts +187 -0
  133. package/src/toolspec.ts +279 -0
  134. package/src/triton/client.test.ts +205 -0
  135. package/src/triton/client.ts +261 -0
  136. package/src/tui/anim.test.ts +146 -0
  137. package/src/tui/anim.ts +134 -0
  138. package/src/tui/badge.test.ts +41 -0
  139. package/src/tui/badge.ts +55 -0
  140. package/src/tui/confirm.test.ts +68 -0
  141. package/src/tui/confirm.ts +49 -0
  142. package/src/tui/index.ts +29 -0
  143. package/src/tui/list.test.ts +84 -0
  144. package/src/tui/list.ts +72 -0
  145. package/src/tui/parity.ts +41 -0
  146. package/src/tui/pills.test.ts +40 -0
  147. package/src/tui/pills.ts +43 -0
  148. package/src/tui/progress.test.ts +164 -0
  149. package/src/tui/progress.ts +120 -0
  150. package/src/tui/spinner.test.ts +111 -0
  151. package/src/tui/spinner.ts +96 -0
  152. package/src/tui/status.test.ts +68 -0
  153. package/src/tui/status.ts +66 -0
  154. package/src/upgrade.test.ts +298 -0
  155. package/src/upgrade.ts +205 -0
  156. package/src/uri.ts +199 -0
  157. package/src/wizard.test.ts +441 -0
  158. package/src/wizard.ts +662 -0
  159. package/src/xdg.test.ts +202 -0
  160. package/src/xdg.ts +280 -0
  161. package/test/output/cols.test.ts +148 -0
  162. package/test/output/csv.test.ts +129 -0
  163. package/test/output/dispatch.test.ts +194 -0
  164. package/test/output/format_help.test.ts +131 -0
  165. package/test/output/formatter.test.ts +137 -0
  166. package/test/output/output_flag.test.ts +124 -0
  167. package/test/output/registry.test.ts +127 -0
  168. package/test/output/smoke-capture.ts +83 -0
  169. package/test/output/smoke.test.ts +142 -0
  170. package/test/output/template.test.ts +95 -0
  171. package/test/output/text.test.ts +75 -0
  172. package/test/testdata/api-fixture.json +115 -0
  173. package/test/testdata/query-vectors.json +187 -0
  174. package/tsconfig.json +12 -0
@@ -0,0 +1,441 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import {
3
+ StepKind,
4
+ ErrorAction,
5
+ Wizard,
6
+ textInput,
7
+ select,
8
+ confirm,
9
+ multiSelect,
10
+ action,
11
+ summary,
12
+ ValidationError,
13
+ ActionError,
14
+ ActionRequest,
15
+ resultString,
16
+ resultBool,
17
+ resultStrings,
18
+ resultChoice,
19
+ type Step,
20
+ } from "./wizard";
21
+
22
+ // --- construction ---
23
+
24
+ describe("Wizard construction", () => {
25
+ it("accepts valid steps", () => {
26
+ const w = new Wizard(
27
+ textInput("name", "Name"),
28
+ confirm("ok", "OK?"),
29
+ );
30
+ expect(w).toBeTruthy();
31
+ });
32
+
33
+ it("rejects duplicate keys", () => {
34
+ expect(
35
+ () => new Wizard(textInput("x", "A"), textInput("x", "B")),
36
+ ).toThrow(/duplicate/i);
37
+ });
38
+
39
+ it("rejects empty key", () => {
40
+ expect(
41
+ () =>
42
+ new Wizard({
43
+ key: "",
44
+ kind: StepKind.TextInput,
45
+ label: "Name",
46
+ } as Step),
47
+ ).toThrow(/key must not be empty/i);
48
+ });
49
+
50
+ it("rejects bad default type for confirm", () => {
51
+ expect(
52
+ () => new Wizard(confirm("ok", "OK?").withDefault("nope")),
53
+ ).toThrow(/bool/i);
54
+ });
55
+
56
+ it("rejects action without fn", () => {
57
+ expect(
58
+ () =>
59
+ new Wizard({
60
+ key: "act",
61
+ kind: StepKind.Action,
62
+ label: "do",
63
+ } as Step),
64
+ ).toThrow(/actionFn/i);
65
+ });
66
+
67
+ it("rejects select without options", () => {
68
+ expect(
69
+ () =>
70
+ new Wizard({
71
+ key: "sel",
72
+ kind: StepKind.Select,
73
+ label: "pick",
74
+ options: [],
75
+ } as Step),
76
+ ).toThrow(/options/i);
77
+ });
78
+
79
+ it("auto-generates summary key", () => {
80
+ const w = new Wizard(
81
+ textInput("a", "A"),
82
+ summary("Review"),
83
+ );
84
+ expect(w.current()!.key).toBe("a");
85
+ });
86
+ });
87
+
88
+ // --- advance ---
89
+
90
+ describe("Advance", () => {
91
+ it("text input stores string", () => {
92
+ const w = new Wizard(textInput("name", "Name"));
93
+ const [res, err] = w.advance("Alice");
94
+ expect(err).toBeNull();
95
+ expect(res).toBeNull();
96
+ expect(resultString(w.results(), "name")).toBe("Alice");
97
+ expect(w.done()).toBe(true);
98
+ });
99
+
100
+ it("select stores valid option", () => {
101
+ const opts = [
102
+ { value: "a", label: "A" },
103
+ { value: "b", label: "B" },
104
+ ];
105
+ const w = new Wizard(select("color", "Color", opts));
106
+ w.advance("a");
107
+ expect(resultChoice(w.results(), "color")).toBe("a");
108
+ });
109
+
110
+ it("select rejects invalid option", () => {
111
+ const opts = [
112
+ { value: "a", label: "A" },
113
+ { value: "b", label: "B" },
114
+ ];
115
+ const w = new Wizard(select("color", "Color", opts));
116
+ const [, err] = w.advance("z");
117
+ expect(err).toBeInstanceOf(ValidationError);
118
+ expect(err!.message).toMatch(/invalid option/);
119
+ });
120
+
121
+ it("confirm stores bool", () => {
122
+ const w = new Wizard(confirm("ok", "Sure?"));
123
+ w.advance(true);
124
+ expect(resultBool(w.results(), "ok")).toBe(true);
125
+ });
126
+
127
+ it("confirm rejects non-bool", () => {
128
+ const w = new Wizard(confirm("ok", "Sure?"));
129
+ const [, err] = w.advance("yes");
130
+ expect(err).toBeInstanceOf(ValidationError);
131
+ });
132
+
133
+ it("multi_select stores string array", () => {
134
+ const opts = [
135
+ { value: "x", label: "X" },
136
+ { value: "y", label: "Y" },
137
+ ];
138
+ const w = new Wizard(multiSelect("tags", "Tags", opts));
139
+ w.advance(["x", "y"]);
140
+ expect(resultStrings(w.results(), "tags")).toEqual(["x", "y"]);
141
+ });
142
+
143
+ it("multi_select rejects invalid option", () => {
144
+ const opts = [
145
+ { value: "x", label: "X" },
146
+ { value: "y", label: "Y" },
147
+ ];
148
+ const w = new Wizard(multiSelect("tags", "Tags", opts));
149
+ const [, err] = w.advance(["x", "nope"]);
150
+ expect(err).toBeInstanceOf(ValidationError);
151
+ expect(err!.message).toMatch(/invalid option.*nope/);
152
+ });
153
+
154
+ it("action returns ActionRequest", () => {
155
+ let _called = false;
156
+ const fn = async () => {
157
+ _called = true;
158
+ };
159
+ const w = new Wizard(action("act", "Go", fn));
160
+ const [res, err] = w.advance(null);
161
+ expect(err).toBeNull();
162
+ expect(res).toBeInstanceOf(ActionRequest);
163
+ expect((res as ActionRequest).stepKey).toBe("act");
164
+ });
165
+
166
+ it("summary advances without value", () => {
167
+ const w = new Wizard(
168
+ textInput("a", "A"),
169
+ summary("Review"),
170
+ );
171
+ w.advance("val");
172
+ expect(w.current()!.kind).toBe(StepKind.Summary);
173
+ w.advance(null);
174
+ expect(w.done()).toBe(true);
175
+ });
176
+
177
+ it("validates required text", () => {
178
+ const w = new Wizard(textInput("name", "Name").withRequired());
179
+ const [, err] = w.advance("");
180
+ expect(err).toBeInstanceOf(ValidationError);
181
+ expect(err!.message).toMatch(/required/);
182
+ });
183
+
184
+ it("validates required multi_select", () => {
185
+ const opts = [{ value: "x", label: "X" }];
186
+ const w = new Wizard(
187
+ multiSelect("tags", "Tags", opts).withRequired(),
188
+ );
189
+ const [, err] = w.advance([]);
190
+ expect(err).toBeInstanceOf(ValidationError);
191
+ expect(err!.message).toMatch(/required/);
192
+ });
193
+
194
+ it("runs custom text validator", () => {
195
+ const w = new Wizard(
196
+ textInput("email", "Email").withValidateText((s) => {
197
+ if (s === "bad") throw new Error("invalid email");
198
+ }),
199
+ );
200
+ const [, err] = w.advance("bad");
201
+ expect(err).toBeInstanceOf(ValidationError);
202
+ expect(err!.message).toMatch(/invalid email/);
203
+ });
204
+ });
205
+
206
+ // --- resolveAction ---
207
+
208
+ describe("ResolveAction", () => {
209
+ it("advances on success", () => {
210
+ const w = new Wizard(
211
+ action("a", "A", async () => {}),
212
+ textInput("b", "B"),
213
+ );
214
+ w.advance(null); // get ActionRequest
215
+ const err = w.resolveAction(null);
216
+ expect(err).toBeNull();
217
+ expect(w.current()!.key).toBe("b");
218
+ });
219
+
220
+ it("returns ActionError on abort", () => {
221
+ const w = new Wizard(
222
+ action("a", "A", async () => {}).withOnError(
223
+ ErrorAction.Abort,
224
+ ),
225
+ );
226
+ w.advance(null);
227
+ const err = w.resolveAction(new Error("boom"));
228
+ expect(err).toBeInstanceOf(ActionError);
229
+ expect((err as ActionError).action).toBe(ErrorAction.Abort);
230
+ });
231
+
232
+ it("stays on step for retry", () => {
233
+ const w = new Wizard(
234
+ action("a", "A", async () => {}).withOnError(
235
+ ErrorAction.Retry,
236
+ ),
237
+ );
238
+ w.advance(null);
239
+ const err = w.resolveAction(new Error("transient"));
240
+ expect(err).toBeNull();
241
+ expect(w.current()!.key).toBe("a");
242
+ });
243
+
244
+ it("skips on skip policy", () => {
245
+ const w = new Wizard(
246
+ action("a", "A", async () => {}).withOnError(
247
+ ErrorAction.Skip,
248
+ ),
249
+ textInput("b", "B"),
250
+ );
251
+ w.advance(null);
252
+ const err = w.resolveAction(new Error("meh"));
253
+ expect(err).toBeNull();
254
+ expect(w.current()!.key).toBe("b");
255
+ });
256
+ });
257
+
258
+ // --- back ---
259
+
260
+ describe("Back", () => {
261
+ it("goes to previous step and clears result", () => {
262
+ const w = new Wizard(textInput("a", "A"), textInput("b", "B"));
263
+ w.advance("hello");
264
+ expect(w.current()!.key).toBe("b");
265
+ w.back();
266
+ expect(w.current()!.key).toBe("a");
267
+ expect(resultString(w.results(), "a")).toBe("");
268
+ });
269
+
270
+ it("no-op at start", () => {
271
+ const w = new Wizard(textInput("a", "A"));
272
+ w.back();
273
+ expect(w.current()!.key).toBe("a");
274
+ });
275
+
276
+ it("skips hidden steps during back", () => {
277
+ const w = new Wizard(
278
+ confirm("show", "Show?"),
279
+ textInput("hidden", "Hidden").withWhen("show", (v) =>
280
+ Boolean(v),
281
+ ),
282
+ textInput("last", "Last"),
283
+ );
284
+ w.advance(false);
285
+ expect(w.current()!.key).toBe("last");
286
+ w.back();
287
+ expect(w.current()!.key).toBe("show");
288
+ });
289
+ });
290
+
291
+ // --- conditional ---
292
+
293
+ describe("Conditional steps", () => {
294
+ it("shows step when condition true", () => {
295
+ const w = new Wizard(
296
+ confirm("advanced", "Advanced?"),
297
+ textInput("extra", "Extra").withWhen("advanced", (v) =>
298
+ Boolean(v),
299
+ ),
300
+ );
301
+ w.advance(true);
302
+ expect(w.current()!.key).toBe("extra");
303
+ });
304
+
305
+ it("skips step when condition false", () => {
306
+ const w = new Wizard(
307
+ confirm("advanced", "Advanced?"),
308
+ textInput("extra", "Extra").withWhen("advanced", (v) =>
309
+ Boolean(v),
310
+ ),
311
+ );
312
+ w.advance(false);
313
+ expect(w.done()).toBe(true);
314
+ });
315
+
316
+ it("clears stale results on re-evaluation", () => {
317
+ const w = new Wizard(
318
+ confirm("show", "Show?"),
319
+ textInput("extra", "Extra").withWhen("show", (v) =>
320
+ Boolean(v),
321
+ ),
322
+ textInput("last", "Last"),
323
+ );
324
+ // show=true, fill extra
325
+ w.advance(true);
326
+ w.advance("filled");
327
+ expect(w.results()["extra"]).toBe("filled");
328
+
329
+ // go back, change show to false
330
+ w.back(); // back to extra
331
+ w.back(); // back to show
332
+ w.advance(false);
333
+
334
+ expect(w.current()!.key).toBe("last");
335
+ expect(w.results()["extra"]).toBeUndefined();
336
+ });
337
+ });
338
+
339
+ // --- done ---
340
+
341
+ describe("Done", () => {
342
+ it("false initially, true after all steps", () => {
343
+ const w = new Wizard(textInput("a", "A"));
344
+ expect(w.done()).toBe(false);
345
+ w.advance("val");
346
+ expect(w.done()).toBe(true);
347
+ });
348
+ });
349
+
350
+ // --- stepCount / stepIndex ---
351
+
352
+ describe("StepCount and StepIndex", () => {
353
+ it("excludes hidden steps", () => {
354
+ const w = new Wizard(
355
+ confirm("show", "Show?"),
356
+ textInput("hidden", "Hidden").withWhen("show", (v) =>
357
+ Boolean(v),
358
+ ),
359
+ textInput("visible", "Visible"),
360
+ );
361
+ // before answering, show default nil → pred false
362
+ expect(w.stepCount()).toBe(2);
363
+ w.advance(true);
364
+ expect(w.stepCount()).toBe(3);
365
+ });
366
+
367
+ it("stepIndex tracks visible position", () => {
368
+ const w = new Wizard(
369
+ textInput("a", "A"),
370
+ textInput("b", "B"),
371
+ textInput("c", "C"),
372
+ );
373
+ expect(w.stepIndex()).toBe(0);
374
+ w.advance("x");
375
+ expect(w.stepIndex()).toBe(1);
376
+ w.advance("y");
377
+ expect(w.stepIndex()).toBe(2);
378
+ });
379
+ });
380
+
381
+ // --- dryRun / complete ---
382
+
383
+ describe("Complete and DryRun", () => {
384
+ it("calls onComplete", () => {
385
+ const w = new Wizard(textInput("a", "A"));
386
+ let got: Record<string, unknown> | null = null;
387
+ w.setOnComplete((r) => {
388
+ got = r;
389
+ });
390
+ w.advance("done");
391
+ w.complete();
392
+ expect(got!["a"]).toBe("done");
393
+ });
394
+
395
+ it("skips onComplete in dryRun", () => {
396
+ const w = new Wizard(textInput("a", "A"));
397
+ let called = false;
398
+ w.setOnComplete(() => {
399
+ called = true;
400
+ });
401
+ w.setDryRun(true);
402
+ expect(w.dryRun()).toBe(true);
403
+ w.advance("val");
404
+ w.complete();
405
+ expect(called).toBe(false);
406
+ });
407
+ });
408
+
409
+ // --- result accessors ---
410
+
411
+ describe("Result accessors", () => {
412
+ const results: Record<string, unknown> = {
413
+ name: "Alice",
414
+ ok: true,
415
+ tags: ["a", "b"],
416
+ color: "red",
417
+ bad: 42,
418
+ };
419
+
420
+ it("string", () => {
421
+ expect(resultString(results, "name")).toBe("Alice");
422
+ expect(resultString(results, "missing")).toBe("");
423
+ expect(resultString(results, "bad")).toBe("");
424
+ });
425
+
426
+ it("bool", () => {
427
+ expect(resultBool(results, "ok")).toBe(true);
428
+ expect(resultBool(results, "missing")).toBe(false);
429
+ expect(resultBool(results, "bad")).toBe(false);
430
+ });
431
+
432
+ it("strings", () => {
433
+ expect(resultStrings(results, "tags")).toEqual(["a", "b"]);
434
+ expect(resultStrings(results, "missing")).toBeNull();
435
+ expect(resultStrings(results, "bad")).toBeNull();
436
+ });
437
+
438
+ it("choice (alias for string)", () => {
439
+ expect(resultChoice(results, "color")).toBe("red");
440
+ });
441
+ });