@histoire/cli 0.0.0-stage → 1.0.0-beta.3
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.
- package/LICENSE +21 -0
- package/README.md +8 -2
- package/dist/cli.d.ts +9 -0
- package/dist/cli.js +88 -0
- package/dist/commands/dev.d.ts +44 -0
- package/dist/commands/dev.js +117 -0
- package/dist/commands/host.d.ts +66 -0
- package/dist/commands/host.js +1 -0
- package/dist/commands/screenshot-setup-args.d.ts +4 -0
- package/dist/commands/screenshot-setup-args.js +35 -0
- package/dist/commands/signal-guard.d.ts +29 -0
- package/dist/commands/signal-guard.js +52 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/terminal/actions.d.ts +51 -0
- package/dist/terminal/actions.js +60 -0
- package/dist/terminal/banner.d.ts +19 -0
- package/dist/terminal/banner.js +48 -0
- package/dist/terminal/bridge.d.ts +58 -0
- package/dist/terminal/bridge.js +103 -0
- package/dist/terminal/clipboard.d.ts +46 -0
- package/dist/terminal/clipboard.js +85 -0
- package/dist/terminal/diagnostics/attach.d.ts +9 -0
- package/dist/terminal/diagnostics/attach.js +46 -0
- package/dist/terminal/diagnostics/client.d.ts +12 -0
- package/dist/terminal/diagnostics/client.js +19 -0
- package/dist/terminal/diagnostics/lines.d.ts +17 -0
- package/dist/terminal/diagnostics/lines.js +61 -0
- package/dist/terminal/dispatch.d.ts +28 -0
- package/dist/terminal/dispatch.js +52 -0
- package/dist/terminal/feedback.d.ts +14 -0
- package/dist/terminal/feedback.js +27 -0
- package/dist/terminal/history/bounded.d.ts +37 -0
- package/dist/terminal/history/bounded.js +54 -0
- package/dist/terminal/host-extras.d.ts +14 -0
- package/dist/terminal/host-extras.js +26 -0
- package/dist/terminal/info/client.d.ts +20 -0
- package/dist/terminal/info/client.js +25 -0
- package/dist/terminal/info/generation-client.d.ts +33 -0
- package/dist/terminal/info/generation-client.js +32 -0
- package/dist/terminal/info/lines.d.ts +19 -0
- package/dist/terminal/info/lines.js +33 -0
- package/dist/terminal/info/network.d.ts +17 -0
- package/dist/terminal/info/network.js +48 -0
- package/dist/terminal/info/qr.d.ts +7 -0
- package/dist/terminal/info/qr.js +14 -0
- package/dist/terminal/keys.d.ts +44 -0
- package/dist/terminal/keys.js +60 -0
- package/dist/terminal/logs/browser-options.d.ts +9 -0
- package/dist/terminal/logs/browser-options.js +19 -0
- package/dist/terminal/navigation/browser.d.ts +69 -0
- package/dist/terminal/navigation/browser.js +193 -0
- package/dist/terminal/navigation/list.d.ts +53 -0
- package/dist/terminal/navigation/list.js +104 -0
- package/dist/terminal/navigation/search.d.ts +22 -0
- package/dist/terminal/navigation/search.js +44 -0
- package/dist/terminal/output/decoder.d.ts +24 -0
- package/dist/terminal/output/decoder.js +63 -0
- package/dist/terminal/output/history.d.ts +43 -0
- package/dist/terminal/output/history.js +21 -0
- package/dist/terminal/output/pipeline.d.ts +33 -0
- package/dist/terminal/output/pipeline.js +59 -0
- package/dist/terminal/output/sanitize.d.ts +32 -0
- package/dist/terminal/output/sanitize.js +142 -0
- package/dist/terminal/panel-model.d.ts +20 -0
- package/dist/terminal/panel-model.js +38 -0
- package/dist/terminal/panel.d.ts +30 -0
- package/dist/terminal/panel.js +53 -0
- package/dist/terminal/plain.d.ts +29 -0
- package/dist/terminal/plain.js +68 -0
- package/dist/terminal/progress/format.d.ts +28 -0
- package/dist/terminal/progress/format.js +77 -0
- package/dist/terminal/progress/state.d.ts +71 -0
- package/dist/terminal/progress/state.js +150 -0
- package/dist/terminal/protocol/catalog.d.ts +143 -0
- package/dist/terminal/protocol/catalog.js +61 -0
- package/dist/terminal/protocol/core.d.ts +117 -0
- package/dist/terminal/protocol/core.js +98 -0
- package/dist/terminal/protocol/diagnostics.d.ts +79 -0
- package/dist/terminal/protocol/diagnostics.js +31 -0
- package/dist/terminal/protocol/index.d.ts +22 -0
- package/dist/terminal/protocol/index.js +1 -0
- package/dist/terminal/protocol/progress.d.ts +67 -0
- package/dist/terminal/protocol/progress.js +52 -0
- package/dist/terminal/protocol/requests.d.ts +30 -0
- package/dist/terminal/protocol/requests.js +13 -0
- package/dist/terminal/protocol/session.d.ts +55 -0
- package/dist/terminal/protocol/session.js +28 -0
- package/dist/terminal/protocol/tests.d.ts +212 -0
- package/dist/terminal/protocol/tests.js +87 -0
- package/dist/terminal/protocol/watch.d.ts +60 -0
- package/dist/terminal/protocol/watch.js +40 -0
- package/dist/terminal/requests/browser-options.d.ts +8 -0
- package/dist/terminal/requests/browser-options.js +24 -0
- package/dist/terminal/requests/history.d.ts +30 -0
- package/dist/terminal/requests/history.js +53 -0
- package/dist/terminal/retained.d.ts +27 -0
- package/dist/terminal/retained.js +21 -0
- package/dist/terminal/session-types.d.ts +76 -0
- package/dist/terminal/session-types.js +1 -0
- package/dist/terminal/session.d.ts +8 -0
- package/dist/terminal/session.js +282 -0
- package/dist/terminal/shutdown.d.ts +12 -0
- package/dist/terminal/shutdown.js +30 -0
- package/dist/terminal/stories/client.d.ts +26 -0
- package/dist/terminal/stories/client.js +28 -0
- package/dist/terminal/stories/handoff.d.ts +39 -0
- package/dist/terminal/stories/handoff.js +40 -0
- package/dist/terminal/stories/paged.d.ts +70 -0
- package/dist/terminal/stories/paged.js +137 -0
- package/dist/terminal/stories/render.d.ts +39 -0
- package/dist/terminal/stories/render.js +94 -0
- package/dist/terminal/stories/state.d.ts +93 -0
- package/dist/terminal/stories/state.js +191 -0
- package/dist/terminal/stories/target.d.ts +24 -0
- package/dist/terminal/stories/target.js +29 -0
- package/dist/terminal/style.d.ts +45 -0
- package/dist/terminal/style.js +82 -0
- package/dist/terminal/support.d.ts +53 -0
- package/dist/terminal/support.js +66 -0
- package/dist/terminal/surface.d.ts +41 -0
- package/dist/terminal/surface.js +200 -0
- package/dist/terminal/tests/client.d.ts +21 -0
- package/dist/terminal/tests/client.js +12 -0
- package/dist/terminal/tests/console.d.ts +95 -0
- package/dist/terminal/tests/console.js +202 -0
- package/dist/terminal/tests/format.d.ts +18 -0
- package/dist/terminal/tests/format.js +55 -0
- package/dist/terminal/tests/header.d.ts +16 -0
- package/dist/terminal/tests/header.js +90 -0
- package/dist/terminal/tests/reporter.d.ts +17 -0
- package/dist/terminal/tests/reporter.js +99 -0
- package/dist/terminal/tests/results.d.ts +44 -0
- package/dist/terminal/tests/results.js +104 -0
- package/dist/terminal/tests/scope.d.ts +25 -0
- package/dist/terminal/tests/scope.js +25 -0
- package/dist/terminal/tests/session.d.ts +11 -0
- package/dist/terminal/tests/session.js +24 -0
- package/dist/terminal/tests/state.d.ts +57 -0
- package/dist/terminal/tests/state.js +134 -0
- package/dist/terminal/views/clear-history.d.ts +6 -0
- package/dist/terminal/views/clear-history.js +10 -0
- package/dist/terminal/views/error.d.ts +8 -0
- package/dist/terminal/views/error.js +24 -0
- package/dist/terminal/views/frame.d.ts +20 -0
- package/dist/terminal/views/frame.js +23 -0
- package/dist/terminal/views/help.d.ts +3 -0
- package/dist/terminal/views/help.js +32 -0
- package/dist/terminal/views/index.d.ts +6 -0
- package/dist/terminal/views/index.js +13 -0
- package/dist/terminal/views/logs.d.ts +3 -0
- package/dist/terminal/views/logs.js +7 -0
- package/dist/terminal/views/registry.d.ts +114 -0
- package/dist/terminal/views/registry.js +40 -0
- package/dist/terminal/views/requests.d.ts +8 -0
- package/dist/terminal/views/requests.js +15 -0
- package/dist/terminal/views/session.d.ts +9 -0
- package/dist/terminal/views/session.js +133 -0
- package/dist/terminal/views/stories.d.ts +9 -0
- package/dist/terminal/views/stories.js +172 -0
- package/dist/terminal/views/tests.d.ts +9 -0
- package/dist/terminal/views/tests.js +105 -0
- package/dist/terminal/watch/client.d.ts +12 -0
- package/dist/terminal/watch/client.js +7 -0
- package/dist/terminal/watch/controller.d.ts +30 -0
- package/dist/terminal/watch/controller.js +157 -0
- package/dist/terminal/watch/format.d.ts +15 -0
- package/dist/terminal/watch/format.js +37 -0
- package/dist/terminal/watch/session.d.ts +17 -0
- package/dist/terminal/watch/session.js +12 -0
- package/dist/terminal/width.d.ts +24 -0
- package/dist/terminal/width.js +171 -0
- package/dist/workbench-url.d.ts +30 -0
- package/dist/workbench-url.js +55 -0
- package/package.json +54 -3
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { StringDecoder } from 'node:string_decoder';
|
|
2
|
+
import { sanitizeText } from './output/sanitize.js';
|
|
3
|
+
import { TERMINAL_BOUNDS, truncateText } from './protocol/core.js';
|
|
4
|
+
import { displayWidth, fitWidth } from './width.js';
|
|
5
|
+
/** Alternate screen enter/leave and cursor visibility. */
|
|
6
|
+
const ENTER_ALTERNATE = '\u001B[?1049h';
|
|
7
|
+
const LEAVE_ALTERNATE = '\u001B[?1049l';
|
|
8
|
+
const HIDE_CURSOR = '\u001B[?25l';
|
|
9
|
+
const SHOW_CURSOR = '\u001B[?25h';
|
|
10
|
+
/** Erases from the cursor to the end of the screen. */
|
|
11
|
+
const ERASE_BELOW = '\r\u001B[J';
|
|
12
|
+
/** Log text held while a view owns the screen is bounded. */
|
|
13
|
+
const MAX_HELD_LINES = 2000;
|
|
14
|
+
/** An unfinished log line is shown after this quiet period so it is never lost. */
|
|
15
|
+
const TAIL_FLUSH_MS = 150;
|
|
16
|
+
/** Longest unfinished or complete line kept; the same bound the retained-log decoder applies. */
|
|
17
|
+
const MAX_LINE_LENGTH = TERMINAL_BOUNDS.logTextLength;
|
|
18
|
+
/** Matches SGR styling, the only escape kept in child output. */
|
|
19
|
+
const SGR = /(\u001B\[[0-9;:]*m)/;
|
|
20
|
+
/**
|
|
21
|
+
* Keeps colors of child output but removes every other control sequence, so logs
|
|
22
|
+
* cannot move the cursor or leave the panel region. The line is bounded, and a carriage
|
|
23
|
+
* return keeps only the text after it (what a terminal would show), so the row can never
|
|
24
|
+
* rewrite itself later.
|
|
25
|
+
* @param line One complete line.
|
|
26
|
+
*/
|
|
27
|
+
function cleanLine(line) {
|
|
28
|
+
const bounded = truncateText(line.endsWith('\r') ? line.slice(0, -1) : line, MAX_LINE_LENGTH).text;
|
|
29
|
+
return bounded.slice(bounded.lastIndexOf('\r') + 1).split(SGR).map((part, index) => index % 2 ? part : sanitizeText(part)).join('');
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Creates the painter. Every terminal write is guarded: a failing stream reports once
|
|
33
|
+
* through `onFailure` and the surface goes inert, so a render failure never stops the server.
|
|
34
|
+
* @param options Stream, content providers and failure callback.
|
|
35
|
+
* @param options.stdout Terminal output stream.
|
|
36
|
+
* @param options.content Panel and screen providers.
|
|
37
|
+
* @param options.onFailure Called once when painting fails.
|
|
38
|
+
*/
|
|
39
|
+
export function createSurface(options) {
|
|
40
|
+
const { stdout, content } = options;
|
|
41
|
+
let attached = false;
|
|
42
|
+
let failed = false;
|
|
43
|
+
let alternate = false;
|
|
44
|
+
/** Panel lines currently on the normal screen. */
|
|
45
|
+
let drawn = [];
|
|
46
|
+
const decoders = { stdout: new StringDecoder('utf8'), stderr: new StringDecoder('utf8') };
|
|
47
|
+
const tails = { stdout: '', stderr: '' };
|
|
48
|
+
let held = [];
|
|
49
|
+
let tailTimer;
|
|
50
|
+
/** Runs painting work; the first failure reports and silences the surface. */
|
|
51
|
+
function guard(work) {
|
|
52
|
+
if (failed)
|
|
53
|
+
return;
|
|
54
|
+
try {
|
|
55
|
+
work();
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
failed = true;
|
|
59
|
+
options.onFailure(error);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** Terminal rows the drawn panel occupies at the current width (long lines may wrap after a resize). */
|
|
63
|
+
function drawnRows() {
|
|
64
|
+
const columns = Math.max(1, stdout.columns ?? 80);
|
|
65
|
+
return drawn.reduce((rows, line) => rows + Math.max(1, Math.ceil(displayWidth(line) / columns)), 0);
|
|
66
|
+
}
|
|
67
|
+
/** Moves to the panel's first row and erases it; empty when no panel is drawn. */
|
|
68
|
+
function erase() {
|
|
69
|
+
const rows = drawnRows();
|
|
70
|
+
if (!rows)
|
|
71
|
+
return '';
|
|
72
|
+
return `${rows > 1 ? `\u001B[${rows - 1}A` : ''}${ERASE_BELOW}`;
|
|
73
|
+
}
|
|
74
|
+
/** Panel text; the cursor stays at the end of its last line. */
|
|
75
|
+
function panel(lines = content.panel()) {
|
|
76
|
+
drawn = lines;
|
|
77
|
+
return `\u001B[0m${drawn.join('\n')}`;
|
|
78
|
+
}
|
|
79
|
+
/** Full-screen text for the alternate screen. */
|
|
80
|
+
function screen(lines) {
|
|
81
|
+
const columns = stdout.columns ?? 80;
|
|
82
|
+
const rows = stdout.rows ?? 24;
|
|
83
|
+
return `\u001B[H${lines.slice(0, rows).map(line => `${fitWidth(line, columns)}\u001B[K`).join('\r\n')}\u001B[J`;
|
|
84
|
+
}
|
|
85
|
+
/** Writes complete lines above the panel in a single stream write. */
|
|
86
|
+
function insert(lines) {
|
|
87
|
+
if (!lines.length)
|
|
88
|
+
return;
|
|
89
|
+
if (alternate) {
|
|
90
|
+
held.push(...lines);
|
|
91
|
+
if (held.length > MAX_HELD_LINES)
|
|
92
|
+
held = held.slice(-MAX_HELD_LINES);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
stdout.write(`${erase()}\u001B[0m${lines.join('\n')}\n${panel()}`);
|
|
96
|
+
}
|
|
97
|
+
/** Emits unfinished text of both streams, ending their lines. */
|
|
98
|
+
function flushTails() {
|
|
99
|
+
clearTimeout(tailTimer);
|
|
100
|
+
tailTimer = undefined;
|
|
101
|
+
const lines = ['stdout', 'stderr'].flatMap((name) => {
|
|
102
|
+
const tail = tails[name] + decoders[name].end();
|
|
103
|
+
tails[name] = '';
|
|
104
|
+
return tail ? [cleanLine(tail)] : [];
|
|
105
|
+
});
|
|
106
|
+
guard(() => insert(lines));
|
|
107
|
+
}
|
|
108
|
+
/** Brings the terminal in line with the content: alternate screen when a view is open. */
|
|
109
|
+
function paint() {
|
|
110
|
+
const lines = content.screen();
|
|
111
|
+
if (lines) {
|
|
112
|
+
if (!alternate)
|
|
113
|
+
stdout.write(ENTER_ALTERNATE);
|
|
114
|
+
alternate = true;
|
|
115
|
+
stdout.write(screen(lines));
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (alternate) {
|
|
119
|
+
alternate = false;
|
|
120
|
+
const returning = held;
|
|
121
|
+
held = [];
|
|
122
|
+
stdout.write(LEAVE_ALTERNATE);
|
|
123
|
+
stdout.write(`${erase()}${returning.length ? `\u001B[0m${returning.join('\n')}\n` : ''}${panel()}`);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
// Timer ticks and unrelated state changes must not repaint an unchanged panel.
|
|
127
|
+
const next = content.panel();
|
|
128
|
+
if (drawn.length === next.length && drawn.every((line, index) => line === next[index]))
|
|
129
|
+
return;
|
|
130
|
+
stdout.write(`${erase()}${panel(next)}`);
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
get attached() {
|
|
134
|
+
return attached;
|
|
135
|
+
},
|
|
136
|
+
attach() {
|
|
137
|
+
if (attached)
|
|
138
|
+
return;
|
|
139
|
+
attached = true;
|
|
140
|
+
guard(() => {
|
|
141
|
+
stdout.write(HIDE_CURSOR);
|
|
142
|
+
paint();
|
|
143
|
+
});
|
|
144
|
+
},
|
|
145
|
+
detach() {
|
|
146
|
+
if (!attached)
|
|
147
|
+
return;
|
|
148
|
+
attached = false;
|
|
149
|
+
flushTails();
|
|
150
|
+
// Restoration is attempted even after a failure; it must not throw.
|
|
151
|
+
try {
|
|
152
|
+
if (alternate)
|
|
153
|
+
stdout.write(LEAVE_ALTERNATE);
|
|
154
|
+
alternate = false;
|
|
155
|
+
stdout.write(`${erase()}${held.length ? `${held.join('\n')}\n` : ''}${SHOW_CURSOR}`);
|
|
156
|
+
}
|
|
157
|
+
catch { }
|
|
158
|
+
held = [];
|
|
159
|
+
drawn = [];
|
|
160
|
+
},
|
|
161
|
+
write(stream, data) {
|
|
162
|
+
if (!attached)
|
|
163
|
+
return;
|
|
164
|
+
const text = tails[stream] + (typeof data === 'string' ? data : decoders[stream].write(data));
|
|
165
|
+
const end = text.lastIndexOf('\n');
|
|
166
|
+
const complete = end >= 0 ? text.slice(0, end).split('\n').map(cleanLine) : [];
|
|
167
|
+
tails[stream] = text.slice(end + 1);
|
|
168
|
+
// An unterminated line past the bound is emitted now instead of growing without limit.
|
|
169
|
+
if (tails[stream].length > MAX_LINE_LENGTH) {
|
|
170
|
+
complete.push(cleanLine(tails[stream]));
|
|
171
|
+
tails[stream] = '';
|
|
172
|
+
}
|
|
173
|
+
// The timer is not restarted by later writes, so a spinner that never ends its line is still shown.
|
|
174
|
+
if ((tails.stdout || tails.stderr) && !tailTimer) {
|
|
175
|
+
tailTimer = setTimeout(flushTails, TAIL_FLUSH_MS);
|
|
176
|
+
tailTimer.unref();
|
|
177
|
+
}
|
|
178
|
+
else if (!tails.stdout && !tails.stderr) {
|
|
179
|
+
clearTimeout(tailTimer);
|
|
180
|
+
tailTimer = undefined;
|
|
181
|
+
}
|
|
182
|
+
if (complete.length)
|
|
183
|
+
guard(() => insert(complete));
|
|
184
|
+
},
|
|
185
|
+
repaint() {
|
|
186
|
+
if (attached)
|
|
187
|
+
guard(paint);
|
|
188
|
+
},
|
|
189
|
+
clear() {
|
|
190
|
+
if (!attached)
|
|
191
|
+
return;
|
|
192
|
+
held = [];
|
|
193
|
+
guard(() => {
|
|
194
|
+
if (alternate)
|
|
195
|
+
return;
|
|
196
|
+
stdout.write(`${erase()}\u001B[2J\u001B[3J\u001B[H${panel()}`);
|
|
197
|
+
});
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { TerminalBridge } from '../bridge.js';
|
|
2
|
+
import type { TestOperationDto, TestResultsArgs, TestResultsPage, TestTarget } from '../protocol/tests.js';
|
|
3
|
+
/** Typed wrappers of the `tests.*` operations over the generic bridge. */
|
|
4
|
+
export interface TestsClient {
|
|
5
|
+
/**
|
|
6
|
+
* Admits a run; settles with the operation snapshot right after admission.
|
|
7
|
+
* A timeout means admission is unknown and is never retried automatically.
|
|
8
|
+
*/
|
|
9
|
+
run: (target: TestTarget, expectedRevision?: string) => Promise<TestOperationDto>;
|
|
10
|
+
/** Reads one operation's snapshot. */
|
|
11
|
+
get: (operationId: string) => Promise<TestOperationDto>;
|
|
12
|
+
/** Reads one page of retained cases. */
|
|
13
|
+
results: (args: TestResultsArgs) => Promise<TestResultsPage>;
|
|
14
|
+
/** Requests cancellation of exactly this operation; the state stays `cancelling` until cleanup. */
|
|
15
|
+
cancel: (operationId: string) => Promise<TestOperationDto>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Creates the typed client. Pure sugar over `bridge.send`; holds no state.
|
|
19
|
+
* @param bridge Parent bridge (or any object with a compatible `send`).
|
|
20
|
+
*/
|
|
21
|
+
export declare function createTestsClient(bridge: Pick<TerminalBridge, 'send'>): TestsClient;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates the typed client. Pure sugar over `bridge.send`; holds no state.
|
|
3
|
+
* @param bridge Parent bridge (or any object with a compatible `send`).
|
|
4
|
+
*/
|
|
5
|
+
export function createTestsClient(bridge) {
|
|
6
|
+
return {
|
|
7
|
+
run: (target, expectedRevision) => bridge.send('tests.run', { target, ...(expectedRevision === undefined ? {} : { expectedRevision }) }),
|
|
8
|
+
get: operationId => bridge.send('tests.get', { operationId }),
|
|
9
|
+
results: args => bridge.send('tests.results', args),
|
|
10
|
+
cancel: operationId => bridge.send('tests.cancel', { operationId }),
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { TerminalBridge } from '../bridge.js';
|
|
2
|
+
import type { ProgressState } from '../progress/state.js';
|
|
3
|
+
import type { TestTargetHandoff } from '../stories/handoff.js';
|
|
4
|
+
import type { TestsClient } from './client.js';
|
|
5
|
+
import type { ScopeOption, TestScopeKind } from './scope.js';
|
|
6
|
+
import type { TestEntry, TestsState } from './state.js';
|
|
7
|
+
/** Outcome of asking for a run: the admitted operation, or why nothing was admitted. */
|
|
8
|
+
export type TestsRunResult = {
|
|
9
|
+
id: string;
|
|
10
|
+
} | {
|
|
11
|
+
problem: string;
|
|
12
|
+
};
|
|
13
|
+
/** Extra work a stop request ends (the watch ends here, before any cancellation); returns a short note when it did something. */
|
|
14
|
+
export type TestsStopHook = () => string | void | Promise<string | void>;
|
|
15
|
+
/** Watch control registered by the session so `w` and the plain `watch` command have something to toggle. */
|
|
16
|
+
export interface TestsWatchControl {
|
|
17
|
+
/** Toggles watching and answers with the new state in words. */
|
|
18
|
+
toggle: () => string | Promise<string>;
|
|
19
|
+
/** Whether scheduling is on, for status lines. */
|
|
20
|
+
active: () => boolean;
|
|
21
|
+
/** One-line description of the watch (scope and phase) for the tests screen; undefined when off. */
|
|
22
|
+
status: () => string | undefined;
|
|
23
|
+
/** Short main-panel text about the watch; undefined when off. */
|
|
24
|
+
segment: () => string | undefined;
|
|
25
|
+
/** Observes watch changes so screens repaint. Returns an unsubscribe function. */
|
|
26
|
+
subscribe: (listener: () => void) => () => void;
|
|
27
|
+
}
|
|
28
|
+
/** Inputs of the console controller. */
|
|
29
|
+
export interface TestsConsoleOptions {
|
|
30
|
+
/** Cache of observed operations (fed by bridge events). */
|
|
31
|
+
state: TestsState;
|
|
32
|
+
/** Typed `tests.*` operations. */
|
|
33
|
+
client: TestsClient;
|
|
34
|
+
/** Command bridge; only its events are read here (catalog revisions). */
|
|
35
|
+
bridge: Pick<TerminalBridge, 'subscribe'>;
|
|
36
|
+
/** Generation mirror: scope targets belong to exactly one runtime generation. */
|
|
37
|
+
progress: Pick<ProgressState, 'snapshot' | 'subscribe'>;
|
|
38
|
+
/** Target handed over by the stories screen. */
|
|
39
|
+
handoff: TestTargetHandoff;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Session-lifetime controller shared by the Tests view, the main panel, and the plain
|
|
43
|
+
* commands: scope selection, admission of manual runs, stop, and the historical/current
|
|
44
|
+
* judgement of every result. It owns no operation model of its own: it only submits and
|
|
45
|
+
* cancels through the child and reads what {@link TestsState} observed.
|
|
46
|
+
*/
|
|
47
|
+
export interface TestsConsole {
|
|
48
|
+
/** Observed operations and cached result pages. */
|
|
49
|
+
readonly state: TestsState;
|
|
50
|
+
/** The three scope options with availability. */
|
|
51
|
+
scopeOptions: () => ScopeOption[];
|
|
52
|
+
/** Scope `r` would run. */
|
|
53
|
+
selectedScope: () => TestScopeKind;
|
|
54
|
+
/** Explicit scope change; refuses (returns false) a scope that cannot run. */
|
|
55
|
+
selectScope: (scope: TestScopeKind) => boolean;
|
|
56
|
+
/** Moves to the next available scope in a direction; returns false at an edge. */
|
|
57
|
+
moveScope: (direction: 1 | -1) => boolean;
|
|
58
|
+
/** The selected scope when it can run, otherwise why not. */
|
|
59
|
+
runnable: () => ScopeOption;
|
|
60
|
+
/** Tracked operations, newest first. */
|
|
61
|
+
entries: () => TestEntry[];
|
|
62
|
+
/** Operation whose results are shown: the picked one, else the newest. */
|
|
63
|
+
viewed: () => TestEntry | undefined;
|
|
64
|
+
/** Picks an older (+1) or newer (-1) operation by ID; the pick stays on that ID. */
|
|
65
|
+
moveViewed: (direction: 1 | -1) => void;
|
|
66
|
+
/** Why an entry no longer describes the running server, or undefined when it is current. */
|
|
67
|
+
historical: (entry: TestEntry) => string | undefined;
|
|
68
|
+
/** A run of this console is being admitted or has not finished. */
|
|
69
|
+
busy: () => boolean;
|
|
70
|
+
/** Latest admission problem (busy, unavailable, timeout, unknown target), cleared by the next attempt. */
|
|
71
|
+
problem: () => string | undefined;
|
|
72
|
+
/** Submits the selected scope. Never throws and never retries. */
|
|
73
|
+
run: () => Promise<TestsRunResult>;
|
|
74
|
+
/** Ends terminal work: registered hooks first, then cancels every active operation. Resolves with a one-line outcome. */
|
|
75
|
+
stop: () => Promise<string>;
|
|
76
|
+
/** Registers extra work `stop` ends; returns a remover. */
|
|
77
|
+
onStop: (hook: TestsStopHook) => () => void;
|
|
78
|
+
/** Registers the watch toggle; returns a remover. */
|
|
79
|
+
setWatchControl: (control: TestsWatchControl) => () => void;
|
|
80
|
+
/** Toggles watching or says it is not available. */
|
|
81
|
+
toggleWatch: () => Promise<string>;
|
|
82
|
+
/** Whether a watch control reports scheduling on. */
|
|
83
|
+
watching: () => boolean;
|
|
84
|
+
/** Description of the watch for the tests screen, when it is on. */
|
|
85
|
+
watchStatus: () => string | undefined;
|
|
86
|
+
/** Short main-panel segment about the newest operation. */
|
|
87
|
+
segment: () => string | undefined;
|
|
88
|
+
/** Observes any change a screen would show. Returns an unsubscribe function. */
|
|
89
|
+
subscribe: (listener: () => void) => () => void;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Creates the controller. Subscriptions live as long as the session.
|
|
93
|
+
* @param options State, client, generation mirror and target handoff.
|
|
94
|
+
*/
|
|
95
|
+
export declare function createTestsConsole(options: TestsConsoleOptions): TestsConsole;
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { isCatalogState } from '../protocol/catalog.js';
|
|
2
|
+
import { TerminalError } from '../protocol/core.js';
|
|
3
|
+
import { TEST_FINAL_STATES } from '../protocol/tests.js';
|
|
4
|
+
import { describeSegment } from './format.js';
|
|
5
|
+
import { resolveScopeOptions } from './scope.js';
|
|
6
|
+
/** Operations still in flight (not final, not lost with their generation). */
|
|
7
|
+
const isActive = (entry) => !entry.lost && !TEST_FINAL_STATES.includes(entry.operation.state);
|
|
8
|
+
/**
|
|
9
|
+
* Creates the controller. Subscriptions live as long as the session.
|
|
10
|
+
* @param options State, client, generation mirror and target handoff.
|
|
11
|
+
*/
|
|
12
|
+
export function createTestsConsole(options) {
|
|
13
|
+
const { state, client, progress, handoff } = options;
|
|
14
|
+
const listeners = new Set();
|
|
15
|
+
const stopHooks = new Set();
|
|
16
|
+
let watch;
|
|
17
|
+
let unwatch;
|
|
18
|
+
let scope = 'all';
|
|
19
|
+
let submitting = false;
|
|
20
|
+
let ownedId;
|
|
21
|
+
let problem;
|
|
22
|
+
let pickedId;
|
|
23
|
+
/** Revision of the latest catalog the running generation published. */
|
|
24
|
+
let catalog;
|
|
25
|
+
/** Notifies subscribers; a faulty listener never breaks the others. */
|
|
26
|
+
function notify() {
|
|
27
|
+
for (const listener of [...listeners]) {
|
|
28
|
+
try {
|
|
29
|
+
listener();
|
|
30
|
+
}
|
|
31
|
+
catch { }
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
state.subscribe(notify);
|
|
35
|
+
progress.subscribe(notify);
|
|
36
|
+
// An explicit hand-over from Stories is the only thing that retargets the selection.
|
|
37
|
+
handoff.subscribe(() => {
|
|
38
|
+
const selection = handoff.current();
|
|
39
|
+
if (selection)
|
|
40
|
+
scope = selection.target.scope;
|
|
41
|
+
notify();
|
|
42
|
+
});
|
|
43
|
+
options.bridge.subscribe(({ generation, event, data }) => {
|
|
44
|
+
if (event !== 'catalog.state' || !isCatalogState(data) || data.revision === undefined || data.updating)
|
|
45
|
+
return;
|
|
46
|
+
catalog = { generation, revision: data.revision };
|
|
47
|
+
notify();
|
|
48
|
+
});
|
|
49
|
+
const generation = () => progress.snapshot().generation;
|
|
50
|
+
const scopeOptions = () => resolveScopeOptions(handoff.current(), generation());
|
|
51
|
+
const entries = () => [...state.snapshot()].reverse();
|
|
52
|
+
/** Option of the selected scope. */
|
|
53
|
+
const runnable = () => scopeOptions().find(option => option.scope === scope);
|
|
54
|
+
/** Text for an admission failure. */
|
|
55
|
+
function describeFailure(error) {
|
|
56
|
+
if (error instanceof TerminalError) {
|
|
57
|
+
if (error.code === 'timeout')
|
|
58
|
+
return 'Run request timed out, admission unknown. Check this screen before running again';
|
|
59
|
+
if (error.code === 'busy')
|
|
60
|
+
return 'Another test operation is queued, try again when it finishes';
|
|
61
|
+
if (error.code === 'unavailable')
|
|
62
|
+
return `Tests are unavailable: ${error.message}`;
|
|
63
|
+
return error.message;
|
|
64
|
+
}
|
|
65
|
+
return error instanceof Error ? error.message : String(error);
|
|
66
|
+
}
|
|
67
|
+
const api = {
|
|
68
|
+
state,
|
|
69
|
+
scopeOptions,
|
|
70
|
+
selectedScope: () => scope,
|
|
71
|
+
selectScope(next) {
|
|
72
|
+
if (!scopeOptions().find(option => option.scope === next)?.available)
|
|
73
|
+
return false;
|
|
74
|
+
scope = next;
|
|
75
|
+
notify();
|
|
76
|
+
return true;
|
|
77
|
+
},
|
|
78
|
+
moveScope(direction) {
|
|
79
|
+
const available = scopeOptions().filter(option => option.available);
|
|
80
|
+
const index = available.findIndex(option => option.scope === scope);
|
|
81
|
+
// A selection that became unavailable moves to the nearest available edge.
|
|
82
|
+
const next = index < 0 ? available[direction === 1 ? 0 : available.length - 1] : available[index + direction];
|
|
83
|
+
return next ? api.selectScope(next.scope) : false;
|
|
84
|
+
},
|
|
85
|
+
runnable,
|
|
86
|
+
entries,
|
|
87
|
+
viewed() {
|
|
88
|
+
const all = entries();
|
|
89
|
+
return all.find(entry => entry.operation.id === pickedId) ?? all[0];
|
|
90
|
+
},
|
|
91
|
+
moveViewed(direction) {
|
|
92
|
+
const all = entries();
|
|
93
|
+
const index = Math.max(0, all.findIndex(entry => entry.operation.id === api.viewed()?.operation.id));
|
|
94
|
+
const next = all[Math.min(all.length - 1, Math.max(0, index + direction))];
|
|
95
|
+
// Back on the newest entry the pick is released, so new runs show up again.
|
|
96
|
+
pickedId = next && next !== all[0] ? next.operation.id : undefined;
|
|
97
|
+
notify();
|
|
98
|
+
},
|
|
99
|
+
historical(entry) {
|
|
100
|
+
if (entry.lost)
|
|
101
|
+
return 'runtime ended before the run finished';
|
|
102
|
+
if (entry.generation !== generation())
|
|
103
|
+
return 'runtime restarted since this run';
|
|
104
|
+
if (catalog?.generation === entry.generation && catalog.revision !== entry.operation.revision)
|
|
105
|
+
return 'stories changed since this run';
|
|
106
|
+
return undefined;
|
|
107
|
+
},
|
|
108
|
+
busy() {
|
|
109
|
+
if (submitting)
|
|
110
|
+
return true;
|
|
111
|
+
const owned = ownedId === undefined ? undefined : state.get(ownedId);
|
|
112
|
+
return owned !== undefined && isActive(owned);
|
|
113
|
+
},
|
|
114
|
+
problem: () => problem,
|
|
115
|
+
async run() {
|
|
116
|
+
if (api.busy())
|
|
117
|
+
return { problem: 'A test run is already in progress' };
|
|
118
|
+
const option = runnable();
|
|
119
|
+
if (!option.available || !option.target) {
|
|
120
|
+
problem = option.reason ?? 'Scope cannot run';
|
|
121
|
+
notify();
|
|
122
|
+
return { problem };
|
|
123
|
+
}
|
|
124
|
+
submitting = true;
|
|
125
|
+
problem = undefined;
|
|
126
|
+
pickedId = undefined;
|
|
127
|
+
notify();
|
|
128
|
+
try {
|
|
129
|
+
const operation = await client.run(option.target);
|
|
130
|
+
ownedId = operation.id;
|
|
131
|
+
// The reply is the first sight of the operation; events keep it current afterwards.
|
|
132
|
+
const owner = generation();
|
|
133
|
+
if (owner)
|
|
134
|
+
state.onEvent(owner, 'tests.state', operation);
|
|
135
|
+
return { id: operation.id };
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
problem = describeFailure(error);
|
|
139
|
+
return { problem };
|
|
140
|
+
}
|
|
141
|
+
finally {
|
|
142
|
+
submitting = false;
|
|
143
|
+
notify();
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
async stop() {
|
|
147
|
+
const notes = [];
|
|
148
|
+
for (const hook of [...stopHooks]) {
|
|
149
|
+
try {
|
|
150
|
+
const note = await hook();
|
|
151
|
+
if (note)
|
|
152
|
+
notes.push(note);
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
notes.push(`Stop failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const active = entries().filter(isActive);
|
|
159
|
+
const results = await Promise.allSettled(active.map(entry => client.cancel(entry.operation.id)));
|
|
160
|
+
// Cancelling stays visible until the child reports the final state; the reply only confirms the request.
|
|
161
|
+
results.forEach((result, index) => {
|
|
162
|
+
if (result.status === 'fulfilled')
|
|
163
|
+
state.onEvent(active[index].generation, 'tests.state', result.value);
|
|
164
|
+
else
|
|
165
|
+
notes.push(`Cancel failed: ${describeFailure(result.reason)}`);
|
|
166
|
+
});
|
|
167
|
+
if (active.length)
|
|
168
|
+
notes.unshift(active.length === 1 ? 'Cancelling tests, waiting for cleanup' : `Cancelling ${active.length} test operations, waiting for cleanup`);
|
|
169
|
+
return notes.join('. ') || 'Nothing to stop';
|
|
170
|
+
},
|
|
171
|
+
onStop(hook) {
|
|
172
|
+
stopHooks.add(hook);
|
|
173
|
+
return () => void stopHooks.delete(hook);
|
|
174
|
+
},
|
|
175
|
+
setWatchControl(control) {
|
|
176
|
+
unwatch?.();
|
|
177
|
+
watch = control;
|
|
178
|
+
unwatch = control.subscribe(notify);
|
|
179
|
+
notify();
|
|
180
|
+
return () => {
|
|
181
|
+
if (watch !== control)
|
|
182
|
+
return;
|
|
183
|
+
unwatch?.();
|
|
184
|
+
unwatch = watch = undefined;
|
|
185
|
+
notify();
|
|
186
|
+
};
|
|
187
|
+
},
|
|
188
|
+
toggleWatch: async () => watch ? watch.toggle() : 'Watch is not available yet',
|
|
189
|
+
watching: () => watch?.active() === true,
|
|
190
|
+
watchStatus: () => watch?.status(),
|
|
191
|
+
segment() {
|
|
192
|
+
const newest = entries()[0];
|
|
193
|
+
const tests = describeSegment(newest, newest ? api.historical(newest) !== undefined : false);
|
|
194
|
+
return [watch?.segment(), tests].filter(Boolean).join(' · ') || undefined;
|
|
195
|
+
},
|
|
196
|
+
subscribe(listener) {
|
|
197
|
+
listeners.add(listener);
|
|
198
|
+
return () => void listeners.delete(listener);
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
return api;
|
|
202
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { TestOperationDto, TestProgressDto, TestTargetDto } from '../protocol/tests.js';
|
|
2
|
+
import type { TestEntry } from './state.js';
|
|
3
|
+
/** Short plain description of what an operation ran. */
|
|
4
|
+
export declare function describeTarget(target: TestTargetDto): string;
|
|
5
|
+
/** Collection and run counters are different units, so they are always named. */
|
|
6
|
+
export declare function describeProgress(progress: TestProgressDto): string;
|
|
7
|
+
/**
|
|
8
|
+
* One-line state of an operation: lifecycle, current progress or final totals, failure.
|
|
9
|
+
* @param operation Latest snapshot.
|
|
10
|
+
* @param progress Latest live progress.
|
|
11
|
+
*/
|
|
12
|
+
export declare function describeOperation(operation: TestOperationDto, progress?: TestProgressDto): string;
|
|
13
|
+
/**
|
|
14
|
+
* Panel segment for the newest operation; undefined when it is historical or finished long ago.
|
|
15
|
+
* @param entry Newest tracked operation.
|
|
16
|
+
* @param historical Whether the entry no longer describes the running server.
|
|
17
|
+
*/
|
|
18
|
+
export declare function describeSegment(entry: TestEntry | undefined, historical: boolean): string | undefined;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { sanitizeText } from '../output/sanitize.js';
|
|
2
|
+
/** Short plain description of what an operation ran. */
|
|
3
|
+
export function describeTarget(target) {
|
|
4
|
+
if (target.scope === 'all')
|
|
5
|
+
return 'all stories';
|
|
6
|
+
if (target.scope === 'story')
|
|
7
|
+
return `story ${sanitizeText(target.storyId)}`;
|
|
8
|
+
if (target.scope === 'variant')
|
|
9
|
+
return `${sanitizeText(target.storyId)} / ${sanitizeText(target.variantId)}`;
|
|
10
|
+
return `${target.count} affected ${target.count === 1 ? 'story' : 'stories'}`;
|
|
11
|
+
}
|
|
12
|
+
/** Collection and run counters are different units, so they are always named. */
|
|
13
|
+
export function describeProgress(progress) {
|
|
14
|
+
const unit = progress.phase === 'collect' ? 'Collecting' : 'Running';
|
|
15
|
+
const stories = progress.stories ? ` · story ${progress.stories.completed}/${progress.stories.total}` : '';
|
|
16
|
+
return `${unit} ${progress.completed}/${progress.total}${stories}`;
|
|
17
|
+
}
|
|
18
|
+
/** Exact totals of a completed run. */
|
|
19
|
+
function describeTotals(summary) {
|
|
20
|
+
return `${summary.total} ${summary.total === 1 ? 'test' : 'tests'} · ${summary.passed} passed · ${summary.failed} failed · ${summary.skipped} skipped`;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* One-line state of an operation: lifecycle, current progress or final totals, failure.
|
|
24
|
+
* @param operation Latest snapshot.
|
|
25
|
+
* @param progress Latest live progress.
|
|
26
|
+
*/
|
|
27
|
+
export function describeOperation(operation, progress) {
|
|
28
|
+
switch (operation.state) {
|
|
29
|
+
case 'queued': return 'Queued';
|
|
30
|
+
case 'running': return progress ? describeProgress(progress) : 'Starting';
|
|
31
|
+
case 'cancelling': return 'Cancelling, waiting for cleanup';
|
|
32
|
+
case 'cancelled': return 'Cancelled after cleanup';
|
|
33
|
+
case 'failed': return `Failed · ${sanitizeText(operation.failure?.message ?? 'unknown error')}${operation.failure ? ` (${operation.failure.code})` : ''}`;
|
|
34
|
+
case 'completed': return `${operation.summary?.ok ? 'Passed' : 'Finished with problems'} · ${operation.summary ? describeTotals(operation.summary) : 'no summary'}`;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Panel segment for the newest operation; undefined when it is historical or finished long ago.
|
|
39
|
+
* @param entry Newest tracked operation.
|
|
40
|
+
* @param historical Whether the entry no longer describes the running server.
|
|
41
|
+
*/
|
|
42
|
+
export function describeSegment(entry, historical) {
|
|
43
|
+
if (!entry || historical || entry.lost)
|
|
44
|
+
return undefined;
|
|
45
|
+
const { operation } = entry;
|
|
46
|
+
if (operation.state === 'completed' && operation.summary)
|
|
47
|
+
return `tests ${operation.summary.passed} passed, ${operation.summary.failed} failed${operation.summary.uncollected.length ? `, ${operation.summary.uncollected.length} uncollected` : ''}`;
|
|
48
|
+
if (operation.state === 'failed')
|
|
49
|
+
return 'tests failed to run';
|
|
50
|
+
if (operation.state === 'cancelled')
|
|
51
|
+
return 'tests cancelled';
|
|
52
|
+
if (operation.state === 'cancelling')
|
|
53
|
+
return 'tests cancelling';
|
|
54
|
+
return operation.state === 'queued' || !entry.progress ? `tests ${operation.state}` : `tests ${entry.progress.phase === 'collect' ? 'collecting' : 'running'} ${entry.progress.completed}/${entry.progress.total}`;
|
|
55
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { TestsConsole } from './console.js';
|
|
2
|
+
import type { ResultsList } from './results.js';
|
|
3
|
+
/** Status line above the case list. */
|
|
4
|
+
export declare function describeResultsStatus(tests: TestsConsole, results: ResultsList, shown: number): string;
|
|
5
|
+
/** Empty-list text for the current load state. */
|
|
6
|
+
export declare function describeEmptyResults(tests: TestsConsole, results: ResultsList): string;
|
|
7
|
+
/**
|
|
8
|
+
* Lines above the case list: scope selector, admission problem, the viewed run (with its
|
|
9
|
+
* historical label), uncollected stories and run-level errors. Counts come from the
|
|
10
|
+
* summary, never from the loaded case pages.
|
|
11
|
+
* @param tests Console.
|
|
12
|
+
* @param results Results list (filter state).
|
|
13
|
+
*/
|
|
14
|
+
export declare function buildTestsHeader(tests: TestsConsole, results: ResultsList): string[];
|
|
15
|
+
/** Footer hint of the Tests view. */
|
|
16
|
+
export declare const TESTS_FOOTER = "Left/Right scope \u00B7 r run \u00B7 s stop \u00B7 w watch \u00B7 Enter detail \u00B7 f failures \u00B7 [ ] runs \u00B7 / search \u00B7 Esc back";
|