@dsh-workbench/shared 1.5.5 → 1.5.6
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/dist/api/dsh.d.ts +9 -1
- package/dist/components/card/ApprovalPlan.css +3 -0
- package/dist/components/card/ApprovalPlan.js +7 -2
- package/dist/components/card/LongTaskRow.css +32 -0
- package/dist/components/card/LongTaskRow.d.ts +8 -2
- package/dist/components/card/LongTaskRow.js +26 -5
- package/dist/components/card/board-module.d.ts +35 -4
- package/dist/components/card/use-text-marquee.d.ts +15 -0
- package/dist/components/card/use-text-marquee.js +66 -0
- package/dist/components/feedback/ProgressBar.css +56 -0
- package/dist/components/feedback/ProgressBar.d.ts +25 -0
- package/dist/components/feedback/ProgressBar.js +22 -0
- package/dist/config/constants.d.ts +2 -0
- package/dist/config/constants.js +2 -0
- package/dist/contract/guards.d.ts +1 -0
- package/dist/contract/guards.js +3 -0
- package/dist/contract/long-task.d.ts +56 -0
- package/dist/contract/long-task.js +41 -0
- package/dist/contract/protocol/long-tasks.d.ts +8 -2
- package/dist/contract/unit.d.ts +1 -0
- package/dist/host/diy/diy-preset.d.ts +1 -1
- package/dist/host/diy/diy-preset.js +3 -3
- package/dist/host/flow/create.d.ts +2 -1
- package/dist/host/flow/create.js +6 -0
- package/dist/host/flow/dsh-journal.d.ts +3 -0
- package/dist/host/flow/dsh-journal.js +55 -0
- package/dist/host/flow/journal.d.ts +20 -0
- package/dist/host/flow/journal.js +30 -0
- package/dist/host/flow/open.js +43 -26
- package/dist/host/flow/task.js +12 -1
- package/dist/host/flow/types.d.ts +5 -0
- package/dist/host/platform/integration/browser-launch.d.ts +1 -0
- package/dist/host/platform/integration/browser-launch.js +9 -2
- package/dist/host/profile/bundle-manager.js +4 -6
- package/dist/host/unit/long-task-host.d.ts +18 -0
- package/dist/host/unit/long-task-host.js +44 -0
- package/dist/host/unit/long-task-progress.d.ts +3 -0
- package/dist/host/unit/long-task-progress.js +32 -0
- package/dist/host/unit/long-task.d.ts +14 -38
- package/dist/host/unit/long-task.js +95 -94
- package/dist/host/unit/unit-face.d.ts +2 -1
- package/dist/host/unit/with-intervention-retry.d.ts +20 -0
- package/dist/host/unit/with-intervention-retry.js +45 -0
- package/dist/host.d.ts +30 -19
- package/dist/host.js +30 -19
- package/dist/i18n/en.js +33 -7
- package/dist/i18n/index.d.ts +33 -7
- package/dist/i18n/zh.d.ts +32 -6
- package/dist/i18n/zh.js +32 -6
- package/dist/index.d.ts +4 -2
- package/dist/index.js +4 -2
- package/package.json +2 -1
- package/src/api/dsh.ts +12 -1
- package/src/components/card/ApprovalPlan.css +3 -0
- package/src/components/card/ApprovalPlan.tsx +9 -2
- package/src/components/card/LongTaskRow.css +32 -0
- package/src/components/card/LongTaskRow.tsx +51 -4
- package/src/components/card/__tests__/ApprovalPlan.test.tsx +50 -0
- package/src/components/card/__tests__/long-task-row.test.ts +64 -2
- package/src/components/card/board-module.ts +36 -4
- package/src/components/card/use-text-marquee.ts +79 -0
- package/src/components/feedback/ProgressBar.css +56 -0
- package/src/components/feedback/ProgressBar.tsx +60 -0
- package/src/config/constants.ts +3 -0
- package/src/contract/__tests__/long-task.test.ts +43 -1
- package/src/contract/guards.ts +4 -0
- package/src/contract/long-task.ts +75 -0
- package/src/contract/protocol/long-tasks.ts +9 -1
- package/src/contract/unit.ts +1 -0
- package/src/host/diy/__tests__/diy-preset.test.ts +2 -0
- package/src/host/diy/diy-preset.ts +3 -2
- package/src/host/flow/__tests__/dsh-journal.test.ts +56 -0
- package/src/host/flow/__tests__/flow-create.test.ts +41 -1
- package/src/host/flow/__tests__/flow-open.test.ts +32 -0
- package/src/host/flow/create.ts +8 -5
- package/src/host/flow/dsh-journal.ts +74 -0
- package/src/host/flow/journal.ts +46 -0
- package/src/host/flow/open.ts +41 -26
- package/src/host/flow/task.ts +11 -1
- package/src/host/flow/types.ts +6 -0
- package/src/host/platform/integration/__tests__/browser-launch.test.ts +27 -0
- package/src/host/platform/integration/browser-launch.ts +15 -3
- package/src/host/profile/bundle-manager.ts +4 -7
- package/src/host/unit/__tests__/long-task.test.ts +203 -2
- package/src/host/unit/long-task-host.ts +64 -0
- package/src/host/unit/long-task-progress.ts +37 -0
- package/src/host/unit/long-task.ts +112 -126
- package/src/host/unit/unit-face.ts +2 -1
- package/src/host/unit/with-intervention-retry.ts +55 -0
- package/src/host.ts +31 -20
- package/src/i18n/en.ts +33 -7
- package/src/i18n/zh.ts +32 -6
- package/src/index.ts +4 -2
package/src/host/flow/open.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { targetOf } from './run.js';
|
|
|
4
4
|
import { schemaProblem } from './schema.js';
|
|
5
5
|
|
|
6
6
|
export function openFlow(options: FlowOpenOptions): FlowSession {
|
|
7
|
-
const steps: Record<string, unknown> = {};
|
|
7
|
+
const steps: Record<string, unknown> = { ...(options.resume?.values ?? {}) };
|
|
8
8
|
let current: FlowStepPosition | null = null;
|
|
9
9
|
let schemaOfCurrent: unknown;
|
|
10
10
|
let pending: { readonly stepId: string; readonly resolve: (value: unknown) => void } | null = null;
|
|
@@ -27,35 +27,50 @@ export function openFlow(options: FlowOpenOptions): FlowSession {
|
|
|
27
27
|
pending = { stepId, resolve };
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (step.when !== undefined && !evalPredicate(step.when, { in: options.input, steps })) {
|
|
35
|
-
emit({ stepId: step.id, title, status: 'skipped' });
|
|
36
|
-
continue;
|
|
37
|
-
}
|
|
38
|
-
if (
|
|
39
|
-
options.flow.stopWhen !== undefined &&
|
|
40
|
-
evalPredicate(options.flow.stopWhen, { in: options.input, steps })
|
|
41
|
-
) {
|
|
42
|
-
break;
|
|
43
|
-
}
|
|
44
|
-
current = { index, stepId: step.id, title };
|
|
45
|
-
schemaOfCurrent = 'schema' in step ? step.schema : undefined;
|
|
46
|
-
emit({ stepId: step.id, title, status: 'running' });
|
|
47
|
-
const value = await wait(step.id);
|
|
48
|
-
pending = null;
|
|
49
|
-
if (stopped) break;
|
|
50
|
-
steps[step.id] = value;
|
|
51
|
-
current = null;
|
|
52
|
-
}
|
|
30
|
+
const resumeIndex = options.resume === undefined
|
|
31
|
+
? 0
|
|
32
|
+
: options.flow.steps.findIndex((step) => step.id === options.resume?.stepId);
|
|
33
|
+
if (resumeIndex < 0) {
|
|
53
34
|
settle({
|
|
54
|
-
status:
|
|
35
|
+
status: 'failed',
|
|
55
36
|
target: targetOf(options.flow, { in: options.input, steps }),
|
|
56
37
|
steps,
|
|
38
|
+
failedStep: options.resume?.stepId,
|
|
39
|
+
error: `流程定义里没有步骤 ${String(options.resume?.stepId)}`,
|
|
57
40
|
});
|
|
58
|
-
}
|
|
41
|
+
} else {
|
|
42
|
+
void (async () => {
|
|
43
|
+
for (let index = resumeIndex; index < options.flow.steps.length; index += 1) {
|
|
44
|
+
const step = options.flow.steps[index];
|
|
45
|
+
if (step === undefined) continue;
|
|
46
|
+
if (stopped) break;
|
|
47
|
+
const title = step.title ?? step.id;
|
|
48
|
+
if (step.when !== undefined && !evalPredicate(step.when, { in: options.input, steps })) {
|
|
49
|
+
emit({ stepId: step.id, title, status: 'skipped' });
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (
|
|
53
|
+
options.flow.stopWhen !== undefined &&
|
|
54
|
+
evalPredicate(options.flow.stopWhen, { in: options.input, steps })
|
|
55
|
+
) {
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
current = { index, stepId: step.id, title };
|
|
59
|
+
schemaOfCurrent = 'schema' in step ? step.schema : undefined;
|
|
60
|
+
emit({ stepId: step.id, title, status: 'running' });
|
|
61
|
+
const value = await wait(step.id);
|
|
62
|
+
pending = null;
|
|
63
|
+
if (stopped) break;
|
|
64
|
+
steps[step.id] = value;
|
|
65
|
+
current = null;
|
|
66
|
+
}
|
|
67
|
+
settle({
|
|
68
|
+
status: stopped ? 'stopped' : 'ok',
|
|
69
|
+
target: targetOf(options.flow, { in: options.input, steps }),
|
|
70
|
+
steps,
|
|
71
|
+
});
|
|
72
|
+
})();
|
|
73
|
+
}
|
|
59
74
|
|
|
60
75
|
return {
|
|
61
76
|
result,
|
package/src/host/flow/task.ts
CHANGED
|
@@ -83,9 +83,15 @@ export function flowTaskDeps(
|
|
|
83
83
|
flowHost?: FlowHost,
|
|
84
84
|
): Pick<LongTaskDeps<LongTaskBase>, 'run' | 'outcome' | 'view' | 'stopped'> {
|
|
85
85
|
return {
|
|
86
|
-
run: async ({ payload, onStep, signal }) => {
|
|
86
|
+
run: async ({ payload, onStep, onProgress, signal }) => {
|
|
87
87
|
const parsed = payloadOf(payload);
|
|
88
88
|
if (parsed === null) throw new Error('流程 run 的 payload 不对');
|
|
89
|
+
const total = parsed.flow.steps.length;
|
|
90
|
+
const finished = new Set<string>();
|
|
91
|
+
const report = (): void => {
|
|
92
|
+
if (total > 0) onProgress({ done: finished.size, total });
|
|
93
|
+
};
|
|
94
|
+
report();
|
|
89
95
|
const runOptions = {
|
|
90
96
|
flow: parsed.flow,
|
|
91
97
|
input: parsed.input,
|
|
@@ -95,6 +101,10 @@ export function flowTaskDeps(
|
|
|
95
101
|
workspace: parsed.workspace,
|
|
96
102
|
...(parsed.session === undefined ? {} : { sessionId: parsed.session }),
|
|
97
103
|
onStep: (event: FlowStepEvent) => {
|
|
104
|
+
if (event.status === 'ok' || event.status === 'skipped') {
|
|
105
|
+
finished.add(event.stepId);
|
|
106
|
+
report();
|
|
107
|
+
}
|
|
98
108
|
onStep(longStepOf(event));
|
|
99
109
|
},
|
|
100
110
|
};
|
package/src/host/flow/types.ts
CHANGED
|
@@ -265,10 +265,16 @@ export interface FlowStepEvent {
|
|
|
265
265
|
/**
|
|
266
266
|
* 开一条由外部推进的流程:引擎不执行任何步骤,只算条件、报当前步、等外部递结果。
|
|
267
267
|
*/
|
|
268
|
+
export interface FlowResume {
|
|
269
|
+
readonly stepId: string;
|
|
270
|
+
readonly values: Readonly<Record<string, unknown>>;
|
|
271
|
+
}
|
|
272
|
+
|
|
268
273
|
export interface FlowOpenOptions {
|
|
269
274
|
readonly flow: FlowDefinition;
|
|
270
275
|
readonly input: unknown;
|
|
271
276
|
readonly onStep: (event: FlowStepEvent) => void;
|
|
277
|
+
readonly resume?: FlowResume | undefined;
|
|
272
278
|
readonly now?: (() => number) | undefined;
|
|
273
279
|
}
|
|
274
280
|
|
|
@@ -27,6 +27,33 @@ describe('浏览器启动命令', () => {
|
|
|
27
27
|
).toContain('cmd /d /s /c start');
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
+
it('带上要打开的页面时,把地址原样交给浏览器', () => {
|
|
31
|
+
expect(
|
|
32
|
+
browserLaunchOf('chrome', 'darwin', {
|
|
33
|
+
appPath: '/Applications/Google Chrome.app',
|
|
34
|
+
executable: undefined,
|
|
35
|
+
linuxBinary: '/usr/bin/google-chrome',
|
|
36
|
+
url: 'chrome://extensions',
|
|
37
|
+
})?.command,
|
|
38
|
+
).toBe("open -g -j -a '/Applications/Google Chrome.app' 'chrome://extensions'");
|
|
39
|
+
expect(
|
|
40
|
+
browserLaunchOf('chrome', 'linux', {
|
|
41
|
+
appPath: '',
|
|
42
|
+
executable: undefined,
|
|
43
|
+
linuxBinary: '/usr/bin/google-chrome',
|
|
44
|
+
url: 'chrome://extensions',
|
|
45
|
+
})?.command,
|
|
46
|
+
).toContain('chrome://extensions');
|
|
47
|
+
expect(
|
|
48
|
+
browserLaunchOf('chrome', 'win32', {
|
|
49
|
+
appPath: '',
|
|
50
|
+
executable: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
|
|
51
|
+
linuxBinary: '',
|
|
52
|
+
url: 'chrome://extensions',
|
|
53
|
+
})?.command,
|
|
54
|
+
).toContain('chrome://extensions');
|
|
55
|
+
});
|
|
56
|
+
|
|
30
57
|
it('找不到可执行文件 / 不认识平台就不给命令', () => {
|
|
31
58
|
expect(browserLaunchOf('chrome', 'win32', { appPath: '', executable: undefined, linuxBinary: '' })).toBeUndefined();
|
|
32
59
|
expect(browserLaunchOf('chrome', 'other', { appPath: '', executable: undefined, linuxBinary: '' })).toBeUndefined();
|
|
@@ -11,12 +11,24 @@ export interface BrowserLaunchSpec {
|
|
|
11
11
|
export function browserLaunchOf(
|
|
12
12
|
id: DesktopBrowserId,
|
|
13
13
|
platform: HostOs,
|
|
14
|
-
target: {
|
|
14
|
+
target: {
|
|
15
|
+
readonly appPath: string;
|
|
16
|
+
readonly executable: string | undefined;
|
|
17
|
+
readonly linuxBinary: string;
|
|
18
|
+
readonly url?: string;
|
|
19
|
+
},
|
|
15
20
|
): BrowserLaunchSpec | undefined {
|
|
21
|
+
const page = target.url === undefined ? '' : ` ${quoteShellArgFor(target.url, platform)}`;
|
|
16
22
|
if (platform === 'darwin') {
|
|
17
|
-
return { command: `open -g -j -a ${quoteShellArgFor(target.appPath, 'darwin')}` };
|
|
23
|
+
return { command: `open -g -j -a ${quoteShellArgFor(target.appPath, 'darwin')}${page}` };
|
|
18
24
|
}
|
|
19
25
|
if (platform === 'linux') {
|
|
26
|
+
if (target.url !== undefined) {
|
|
27
|
+
return {
|
|
28
|
+
probe: `command -v ${quoteShellArgFor('setsid', 'linux')}`,
|
|
29
|
+
command: `setsid ${quoteShellArgFor(target.linuxBinary, 'linux')} ${quoteShellArgFor(target.url, 'linux')} >/dev/null 2>&1 < /dev/null &`,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
20
32
|
return {
|
|
21
33
|
probe: `command -v ${quoteShellArgFor('setsid', 'linux')}`,
|
|
22
34
|
command: `setsid ${quoteShellArgFor(target.linuxBinary, 'linux')} --no-startup-window >/dev/null 2>&1 < /dev/null &`,
|
|
@@ -24,7 +36,7 @@ export function browserLaunchOf(
|
|
|
24
36
|
}
|
|
25
37
|
if (platform === 'win32') {
|
|
26
38
|
if (target.executable === undefined) return undefined;
|
|
27
|
-
return { command: `cmd /d /s /c start "" ${quoteShellArgFor(target.executable, 'win32')}` };
|
|
39
|
+
return { command: `cmd /d /s /c start "" ${quoteShellArgFor(target.executable, 'win32')}${page}` };
|
|
28
40
|
}
|
|
29
41
|
return undefined;
|
|
30
42
|
}
|
|
@@ -3,6 +3,7 @@ import { profileDirOf } from '../supply/installed-module.js';
|
|
|
3
3
|
import { generationDirOf } from '../supply/module-root.js';
|
|
4
4
|
import { reconcileWorkbenchLinks } from '../supply/workbench-links.js';
|
|
5
5
|
import { RESTART_REASONS } from '../../contract/protocol/bundle-install.js';
|
|
6
|
+
import { texts } from '../../contract/guards.js';
|
|
6
7
|
import type { BundleManager } from '../adapters/adapter-types.js';
|
|
7
8
|
import type {
|
|
8
9
|
BundleChange,
|
|
@@ -63,10 +64,6 @@ function asRegistry(value: unknown): BundleRegistry | undefined {
|
|
|
63
64
|
return typeof value === 'string' ? value : undefined;
|
|
64
65
|
}
|
|
65
66
|
|
|
66
|
-
function asTextArray(value: unknown): string[] {
|
|
67
|
-
return Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string') : [];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
67
|
function asRegistries(raw: unknown): BundleRegistries | null {
|
|
71
68
|
const record = asRecord(raw);
|
|
72
69
|
if (record === undefined) return null;
|
|
@@ -74,7 +71,7 @@ function asRegistries(raw: unknown): BundleRegistries | null {
|
|
|
74
71
|
if (registry === undefined) return null;
|
|
75
72
|
return {
|
|
76
73
|
registry,
|
|
77
|
-
fallbackRegistries:
|
|
74
|
+
fallbackRegistries: texts(record.fallbackRegistries),
|
|
78
75
|
resolved: typeof record.resolved === 'string' ? record.resolved : null,
|
|
79
76
|
};
|
|
80
77
|
}
|
|
@@ -100,7 +97,7 @@ function asChange(raw: unknown): BundleChange | null {
|
|
|
100
97
|
if (application === undefined || stage === undefined) return null;
|
|
101
98
|
const error = asRecord(record.error);
|
|
102
99
|
const packageResult = asPackageResult(record.packageResult);
|
|
103
|
-
const pendingBuilds =
|
|
100
|
+
const pendingBuilds = texts(record.pendingBuilds);
|
|
104
101
|
const restartReason = pick(RESTART_REASONS, record.restartReason);
|
|
105
102
|
const failedAt = pick(['registry', 'spec-host'] as const, record.failedAt);
|
|
106
103
|
const bundle = asText(record.bundle);
|
|
@@ -228,7 +225,7 @@ function asVersionEntry(raw: unknown): BundleVersionEntry | null {
|
|
|
228
225
|
const incompatible = asIncompatible(record.incompatible);
|
|
229
226
|
return {
|
|
230
227
|
version,
|
|
231
|
-
tags:
|
|
228
|
+
tags: texts(record.tags),
|
|
232
229
|
prerelease: record.prerelease === true,
|
|
233
230
|
bundle: record.bundle === true,
|
|
234
231
|
...(publishedAt === '' ? {} : { publishedAt }),
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest';
|
|
2
2
|
import type { LongTaskIntervention, LongTaskStep } from '../../../contract/long-task.js';
|
|
3
3
|
import {
|
|
4
|
-
createLongTaskHost,
|
|
5
4
|
createLongTaskRegistry,
|
|
6
5
|
longTaskRowBase,
|
|
7
|
-
sharedLongTaskRegistry,
|
|
8
6
|
withInterventionRetry,
|
|
9
7
|
type LongTaskBase,
|
|
10
8
|
type LongTaskDeps,
|
|
11
9
|
} from '../long-task.js';
|
|
10
|
+
import { createLongTaskHost, sharedLongTaskRegistry } from '../long-task-host.js';
|
|
12
11
|
|
|
13
12
|
interface Probe extends LongTaskBase {
|
|
14
13
|
readonly note?: string | undefined;
|
|
@@ -44,6 +43,102 @@ const flush = async (): Promise<void> => {
|
|
|
44
43
|
await Promise.resolve();
|
|
45
44
|
};
|
|
46
45
|
|
|
46
|
+
|
|
47
|
+
describe('长任务计划(plan):外壳自己数步数,模块不报数字', () => {
|
|
48
|
+
it('★★ 起手就是 0/N;步骤事件按 stepId 去重累计;ok 铺满', async () => {
|
|
49
|
+
const h = harness({
|
|
50
|
+
plan: { steps: [{ id: 'a', title: '合并' }, { id: 'b', title: '构建' }, { id: 'c', title: '发布' }] },
|
|
51
|
+
});
|
|
52
|
+
const runs = createLongTaskRegistry(h.deps);
|
|
53
|
+
const started = runs.start({ workspace: '/w/a', target: 'release' });
|
|
54
|
+
|
|
55
|
+
expect(runs.snapshot(started.id)?.progress).toEqual({ done: 0, total: 3 });
|
|
56
|
+
expect(runs.snapshot(started.id)?.plan?.map((one) => one.id)).toEqual(['a', 'b', 'c']);
|
|
57
|
+
|
|
58
|
+
h.step({ kind: 'succeed', text: '合并', stepId: 'a' });
|
|
59
|
+
expect(runs.snapshot(started.id)?.progress).toEqual({ done: 1, total: 3 });
|
|
60
|
+
|
|
61
|
+
h.step({ kind: 'succeed', text: '合并(重试成功)', stepId: 'a' });
|
|
62
|
+
expect(runs.snapshot(started.id)?.progress).toEqual({ done: 1, total: 3 });
|
|
63
|
+
|
|
64
|
+
h.step({ kind: 'info', text: '构建(跳过)', stepId: 'b' });
|
|
65
|
+
expect(runs.snapshot(started.id)?.progress).toEqual({ done: 2, total: 3 });
|
|
66
|
+
|
|
67
|
+
h.step({ kind: 'start', text: '发布', stepId: 'c' });
|
|
68
|
+
expect(runs.snapshot(started.id)?.progress).toEqual({ done: 2, total: 3 });
|
|
69
|
+
|
|
70
|
+
h.done({});
|
|
71
|
+
await flush();
|
|
72
|
+
expect(runs.snapshot(started.id)?.progress).toEqual({ done: 3, total: 3 });
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('★★ 计划外的事件不计数;只给个数时只认成功;超了夹到 total', async () => {
|
|
76
|
+
const only = harness({ plan: { total: 2 } });
|
|
77
|
+
const counted = createLongTaskRegistry(only.deps);
|
|
78
|
+
const one = counted.start({ workspace: '/w/a', target: 'release' });
|
|
79
|
+
|
|
80
|
+
only.step({ kind: 'info', text: '等一下', stepId: 'x' });
|
|
81
|
+
expect(counted.snapshot(one.id)?.progress).toEqual({ done: 0, total: 2 });
|
|
82
|
+
|
|
83
|
+
only.step({ kind: 'succeed', text: 'A', stepId: 'x' });
|
|
84
|
+
only.step({ kind: 'succeed', text: 'B', stepId: 'y' });
|
|
85
|
+
only.step({ kind: 'succeed', text: 'C', stepId: 'z' });
|
|
86
|
+
expect(counted.snapshot(one.id)?.progress).toEqual({ done: 2, total: 2 });
|
|
87
|
+
|
|
88
|
+
const planned = harness({ plan: { steps: [{ id: 'a' }] } });
|
|
89
|
+
const strict = createLongTaskRegistry(planned.deps);
|
|
90
|
+
const two = strict.start({ workspace: '/w/a', target: 'release' });
|
|
91
|
+
planned.step({ kind: 'succeed', text: '别的步', stepId: 'zzz' });
|
|
92
|
+
expect(strict.snapshot(two.id)?.progress).toEqual({ done: 0, total: 1 });
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
it('★ 报了 3 个步骤却没有计划:开发期只告警一次,不影响快照', () => {
|
|
97
|
+
const h = harness();
|
|
98
|
+
const runs = createLongTaskRegistry(h.deps);
|
|
99
|
+
const started = runs.start({ workspace: '/w/a', target: 'release' });
|
|
100
|
+
const calls: unknown[][] = [];
|
|
101
|
+
const original = console.warn;
|
|
102
|
+
console.warn = (...args: unknown[]) => void calls.push(args);
|
|
103
|
+
try {
|
|
104
|
+
for (const id of ['a', 'b', 'c', 'd']) {
|
|
105
|
+
h.step({ kind: 'succeed', text: id, stepId: id });
|
|
106
|
+
}
|
|
107
|
+
} finally {
|
|
108
|
+
console.warn = original;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
expect(calls).toHaveLength(1);
|
|
112
|
+
expect(String(calls[0]?.[0])).toContain('没有计划');
|
|
113
|
+
expect(runs.snapshot(started.id)?.progress).toBeUndefined();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('★★ 模块自己 onProgress 时报的优先:plan 不再覆盖它;没 plan 就不落这个键', async () => {
|
|
117
|
+
let report: ((next: { done: number; total: number }) => void) | undefined;
|
|
118
|
+
const h = harness({
|
|
119
|
+
plan: { total: 6 },
|
|
120
|
+
run: (options) =>
|
|
121
|
+
new Promise(() => {
|
|
122
|
+
report = options.onProgress;
|
|
123
|
+
}),
|
|
124
|
+
});
|
|
125
|
+
const runs = createLongTaskRegistry(h.deps);
|
|
126
|
+
const started = runs.start({ workspace: '/w/a', target: 'release' });
|
|
127
|
+
|
|
128
|
+
report?.({ done: 2, total: 100 });
|
|
129
|
+
expect(runs.snapshot(started.id)?.progress).toEqual({ done: 2, total: 100 });
|
|
130
|
+
|
|
131
|
+
h.step({ kind: 'succeed', text: '一步', stepId: 'a' });
|
|
132
|
+
expect(runs.snapshot(started.id)?.progress).toEqual({ done: 2, total: 100 });
|
|
133
|
+
|
|
134
|
+
const plain = harness();
|
|
135
|
+
const bare = createLongTaskRegistry(plain.deps);
|
|
136
|
+
const none = bare.start({ workspace: '/w/a', target: 'release' });
|
|
137
|
+
plain.step({ kind: 'succeed', text: '一步', stepId: 'a' });
|
|
138
|
+
expect('progress' in (bare.snapshot(none.id) ?? {})).toBe(false);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
47
142
|
describe('createLongTaskRegistry.stop(停止只是转发给生产方,认不认 signal 是模块的事)', () => {
|
|
48
143
|
it('★★ stop:run 拿到的 signal 被 abort,这一趟变 stopped,回 true', () => {
|
|
49
144
|
const h = harness();
|
|
@@ -303,6 +398,107 @@ describe('createLongTaskRegistry 的人工干预节点(needs-human)', () =>
|
|
|
303
398
|
expect(runs.active('/w/a', 'release')?.status).toBe('needs-human');
|
|
304
399
|
});
|
|
305
400
|
|
|
401
|
+
it('★★ recent():活着的都在;收尾的在 recentMs 内还在、超出就掉', async () => {
|
|
402
|
+
let clock = '2026-10-21T02:00:00.000Z';
|
|
403
|
+
const h = harness({ now: () => clock, recentMs: 60_000 });
|
|
404
|
+
const runs = createLongTaskRegistry(h.deps);
|
|
405
|
+
runs.start({ workspace: '/w/a', target: 'live' });
|
|
406
|
+
const settled = runs.start({ workspace: '/w/a', target: 'settled' });
|
|
407
|
+
|
|
408
|
+
h.done({});
|
|
409
|
+
await flush();
|
|
410
|
+
expect(settled.status).toBe('running');
|
|
411
|
+
expect(runs.recent().map((one) => one.target).sort()).toEqual(['live', 'settled']);
|
|
412
|
+
|
|
413
|
+
clock = '2026-10-21T02:01:01.000Z';
|
|
414
|
+
expect(runs.recent().map((one) => one.target)).toEqual(['live']);
|
|
415
|
+
expect(runs.live().map((one) => one.target)).toEqual(['live']);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it('★★ seen(id):只认已收尾的;标过不再通知,at 不动', async () => {
|
|
419
|
+
const h = harness();
|
|
420
|
+
const runs = createLongTaskRegistry(h.deps);
|
|
421
|
+
const started = runs.start({ workspace: '/w/a', target: 'release' });
|
|
422
|
+
|
|
423
|
+
expect(runs.seen(started.id)).toBe(false);
|
|
424
|
+
expect(runs.snapshot(started.id)?.seen).toBe(false);
|
|
425
|
+
|
|
426
|
+
h.done({});
|
|
427
|
+
await flush();
|
|
428
|
+
const at = runs.snapshot(started.id)?.at;
|
|
429
|
+
const kinds: string[] = [];
|
|
430
|
+
runs.onChange((change) => kinds.push(change.kind));
|
|
431
|
+
|
|
432
|
+
expect(runs.seen(started.id)).toBe(true);
|
|
433
|
+
expect(runs.snapshot(started.id)?.seen).toBe(true);
|
|
434
|
+
expect(runs.snapshot(started.id)?.at).toBe(at);
|
|
435
|
+
expect(kinds).toEqual(['seen']);
|
|
436
|
+
expect(runs.seen(started.id)).toBe(true);
|
|
437
|
+
expect(kinds).toEqual(['seen']);
|
|
438
|
+
expect(runs.seen('run-nope')).toBe(false);
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
it('★★ 进度只增不减:回退的 done 被忽略;结算时补齐到 total', async () => {
|
|
443
|
+
let progress: ((next: { done: number; total: number }) => void) | undefined;
|
|
444
|
+
let settle: ((result: unknown) => void) | undefined;
|
|
445
|
+
const h = harness({
|
|
446
|
+
run: (options) =>
|
|
447
|
+
new Promise((resolve) => {
|
|
448
|
+
progress = options.onProgress;
|
|
449
|
+
settle = resolve as (result: unknown) => void;
|
|
450
|
+
}),
|
|
451
|
+
});
|
|
452
|
+
const runs = createLongTaskRegistry(h.deps);
|
|
453
|
+
const started = runs.start({ workspace: '/w/a', target: 'release' });
|
|
454
|
+
|
|
455
|
+
progress?.({ done: 3, total: 7 });
|
|
456
|
+
expect(runs.snapshot(started.id)?.progress).toEqual({ done: 3, total: 7 });
|
|
457
|
+
|
|
458
|
+
progress?.({ done: 1, total: 7 });
|
|
459
|
+
expect(runs.snapshot(started.id)?.progress).toEqual({ done: 3, total: 7 });
|
|
460
|
+
|
|
461
|
+
progress?.({ done: 5, total: 7 });
|
|
462
|
+
expect(runs.snapshot(started.id)?.progress).toEqual({ done: 5, total: 7 });
|
|
463
|
+
|
|
464
|
+
settle?.({});
|
|
465
|
+
await flush();
|
|
466
|
+
expect(runs.snapshot(started.id)?.status).toBe('ok');
|
|
467
|
+
expect(runs.snapshot(started.id)?.progress).toEqual({ done: 7, total: 7 });
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it('★★ 非法进度当作没有:清掉这个键;不动 at;变更事件各一次', async () => {
|
|
471
|
+
let progress: ((next: { done: number; total: number }) => void) | undefined;
|
|
472
|
+
const h = harness({
|
|
473
|
+
run: (options) =>
|
|
474
|
+
new Promise(() => {
|
|
475
|
+
progress = options.onProgress;
|
|
476
|
+
}),
|
|
477
|
+
});
|
|
478
|
+
const runs = createLongTaskRegistry(h.deps);
|
|
479
|
+
const started = runs.start({ workspace: '/w/a', target: 'release' });
|
|
480
|
+
const at = runs.snapshot(started.id)?.at;
|
|
481
|
+
const kinds: string[] = [];
|
|
482
|
+
runs.onChange((change) => kinds.push(change.kind));
|
|
483
|
+
|
|
484
|
+
progress?.({ done: 2, total: 5 });
|
|
485
|
+
expect(runs.snapshot(started.id)?.progress).toEqual({ done: 2, total: 5 });
|
|
486
|
+
expect(runs.snapshot(started.id)?.at).toBe(at);
|
|
487
|
+
|
|
488
|
+
progress?.({ done: 0, total: 0 });
|
|
489
|
+
expect(runs.snapshot(started.id)?.progress).toBeUndefined();
|
|
490
|
+
expect('progress' in (runs.snapshot(started.id) ?? {})).toBe(false);
|
|
491
|
+
expect(kinds).toEqual(['progress', 'progress']);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it('★ 没报过进度就不落这个键(旧快照不多一个空键)', () => {
|
|
495
|
+
const h = harness();
|
|
496
|
+
const runs = createLongTaskRegistry(h.deps);
|
|
497
|
+
const started = runs.start({ workspace: '/w/a', target: 'release' });
|
|
498
|
+
|
|
499
|
+
expect('progress' in (runs.snapshot(started.id) ?? {})).toBe(false);
|
|
500
|
+
});
|
|
501
|
+
|
|
306
502
|
it('★ 等人那一趟能停,载荷跟着清掉', () => {
|
|
307
503
|
const h = harness();
|
|
308
504
|
const runs = createLongTaskRegistry(h.deps);
|
|
@@ -847,6 +1043,11 @@ describe('longTaskRowBase(外壳先铺这一层,view 只覆盖不遗漏)',
|
|
|
847
1043
|
expect('links' in longTaskRowBase(base)).toBe(false);
|
|
848
1044
|
});
|
|
849
1045
|
|
|
1046
|
+
it('★★ seen 只在 true 时带上(活着的不落这个键)', () => {
|
|
1047
|
+
expect('seen' in longTaskRowBase(base)).toBe(false);
|
|
1048
|
+
expect(longTaskRowBase({ ...base, seen: true }).seen).toBe(true);
|
|
1049
|
+
});
|
|
1050
|
+
|
|
850
1051
|
it('★★ 有 links 就原样带上(逐条是 { href, text })', () => {
|
|
851
1052
|
const links = [{ href: 'https://ci.example/42', text: '看流水线' }];
|
|
852
1053
|
expect(longTaskRowBase({ ...base, links }).links).toEqual(links);
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { runsRouteOf } from '../../contract/long-task.js';
|
|
2
|
+
import { createLongTaskRegistry, type LongTaskBase, type LongTaskChange, type LongTaskDeps, type LongTaskRegistry } from './long-task.js';
|
|
3
|
+
|
|
4
|
+
function isRegistry(value: unknown): value is LongTaskRegistry<LongTaskBase> {
|
|
5
|
+
if (typeof value !== 'object' || value === null) return false;
|
|
6
|
+
const candidate = value as Partial<LongTaskRegistry<LongTaskBase>>;
|
|
7
|
+
return (
|
|
8
|
+
typeof candidate.start === 'function' &&
|
|
9
|
+
typeof candidate.current === 'function' &&
|
|
10
|
+
typeof candidate.snapshot === 'function' &&
|
|
11
|
+
typeof candidate.onChange === 'function'
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function sharedLongTaskRegistry<S extends LongTaskBase>(
|
|
16
|
+
scope: string,
|
|
17
|
+
key: string,
|
|
18
|
+
deps: LongTaskDeps<S>,
|
|
19
|
+
): LongTaskRegistry<S> {
|
|
20
|
+
const store = globalThis as unknown as Record<symbol, LongTaskRegistry<LongTaskBase> | undefined>;
|
|
21
|
+
const slot = Symbol.for(`dsh-workbench:long-task:${scope}:${key}`);
|
|
22
|
+
const existing = store[slot];
|
|
23
|
+
if (existing !== undefined && isRegistry(existing)) {
|
|
24
|
+
existing.rebind?.(deps as LongTaskDeps<LongTaskBase>);
|
|
25
|
+
return existing as unknown as LongTaskRegistry<S>;
|
|
26
|
+
}
|
|
27
|
+
const created = createLongTaskRegistry(deps);
|
|
28
|
+
store[slot] = created as unknown as LongTaskRegistry<LongTaskBase>;
|
|
29
|
+
return created;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 一个单元的长任务入口:按 key 建注册表。 */
|
|
33
|
+
export interface LongTaskHost {
|
|
34
|
+
/** key 是模块内稳定的标识;run / view 必给,recheck / answer 决定行上有没有恢复与回执。 */
|
|
35
|
+
create<S extends LongTaskBase>(options: { readonly key: string } & LongTaskDeps<S>): LongTaskRegistry<S>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function createLongTaskHost(options: {
|
|
39
|
+
readonly scope: string;
|
|
40
|
+
readonly unit: string;
|
|
41
|
+
readonly bump: (facts: readonly string[]) => void;
|
|
42
|
+
readonly notify?: ((change: LongTaskChange<LongTaskBase>) => void) | undefined;
|
|
43
|
+
}): { readonly host: LongTaskHost; dispose(): void } {
|
|
44
|
+
const offs: Array<() => void> = [];
|
|
45
|
+
return {
|
|
46
|
+
host: {
|
|
47
|
+
create(createOptions) {
|
|
48
|
+
const { key, facts, ...deps } = createOptions;
|
|
49
|
+
const registry = sharedLongTaskRegistry(options.scope, `${options.unit}:${key}`, deps);
|
|
50
|
+
offs.push(
|
|
51
|
+
registry.onChange((change) => {
|
|
52
|
+
const named = [runsRouteOf(options.unit), ...(facts?.(change) ?? [])];
|
|
53
|
+
options.bump([...new Set(named)]);
|
|
54
|
+
options.notify?.(change);
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
return registry;
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
dispose() {
|
|
61
|
+
for (const off of offs.splice(0)) off();
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {
|
|
2
|
+
longTaskProgressIsValid,
|
|
3
|
+
type LongTaskPlanStep,
|
|
4
|
+
type LongTaskProgress,
|
|
5
|
+
type LongTaskStepRow,
|
|
6
|
+
} from '../../contract/long-task.js';
|
|
7
|
+
|
|
8
|
+
export function nextLongTaskProgress(
|
|
9
|
+
next: LongTaskProgress,
|
|
10
|
+
current: LongTaskProgress | undefined,
|
|
11
|
+
): LongTaskProgress | null {
|
|
12
|
+
if (!longTaskProgressIsValid(next)) return null;
|
|
13
|
+
if (current === undefined) return { done: next.done, total: next.total };
|
|
14
|
+
if (current.total !== next.total) return { done: next.done, total: next.total };
|
|
15
|
+
if (next.done < current.done) return current;
|
|
16
|
+
return { done: next.done, total: next.total };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function derivedLongTaskProgress(
|
|
20
|
+
history: readonly LongTaskStepRow[],
|
|
21
|
+
total: number,
|
|
22
|
+
plan: readonly LongTaskPlanStep[],
|
|
23
|
+
): LongTaskProgress | undefined {
|
|
24
|
+
if (total <= 0) return undefined;
|
|
25
|
+
const wanted = new Set(plan.map((step) => step.id));
|
|
26
|
+
const done = new Set<string>();
|
|
27
|
+
for (const row of history) {
|
|
28
|
+
if (row.stepId === undefined) continue;
|
|
29
|
+
if (wanted.size === 0) {
|
|
30
|
+
if (row.kind === 'succeed') done.add(row.stepId);
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (row.kind !== 'succeed' && row.kind !== 'info') continue;
|
|
34
|
+
if (wanted.has(row.stepId)) done.add(row.stepId);
|
|
35
|
+
}
|
|
36
|
+
return { done: Math.min(done.size, total), total };
|
|
37
|
+
}
|