@firstpick/pi-package-webui 0.11.0 → 0.11.2
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/DEVELOPMENT.md +181 -12
- package/README.md +41 -9
- package/TECHNICAL.md +91 -8
- package/bin/pi-webui.mjs +5432 -11428
- package/index.ts +10 -3
- package/lib/codex-cli-usage.mjs +232 -0
- package/lib/codex-usage-auth.mjs +39 -3
- package/lib/codex-usage-source.mjs +36 -0
- package/lib/component-update-state.mjs +2 -2
- package/lib/git-workflow-preference-values.mjs +122 -0
- package/lib/git-workflow-preferences.mjs +35 -563
- package/lib/package-updates.mjs +260 -0
- package/lib/rpc-supervisor-client.mjs +8 -4
- package/lib/server/app-runners.mjs +1667 -0
- package/lib/server/codex-fast-mode.mjs +161 -0
- package/lib/server/conversation-mode.mjs +218 -0
- package/lib/server/conversation-voice.mjs +318 -0
- package/lib/server/extension-ui-requests.mjs +114 -0
- package/lib/server/git-generation.mjs +459 -0
- package/lib/server/git-mutations.mjs +1827 -0
- package/lib/server/git-reads.mjs +608 -0
- package/lib/server/git-worktree-sessions.mjs +408 -0
- package/lib/server/helper-requests.mjs +82 -0
- package/lib/server/no-project-workspaces.mjs +103 -0
- package/lib/server/rpc-transports.mjs +394 -0
- package/lib/server/settings-store.mjs +544 -0
- package/lib/server/sse-delivery.mjs +138 -0
- package/lib/server/workspace-files.mjs +563 -0
- package/lib/update/supervisor.mjs +4 -0
- package/lib/webui-workspaces.mjs +2 -0
- package/package.json +5 -5
- package/public/activity-agents-controller.mjs +269 -0
- package/public/activity-events-controller.mjs +236 -0
- package/public/activity-intercom-controller.mjs +627 -0
- package/public/activity-queue-controller.mjs +534 -0
- package/public/activity-todo-view.mjs +89 -0
- package/public/activity-usage-controller.mjs +589 -0
- package/public/app.js +3761 -3878
- package/public/conversation-overview.css +84 -0
- package/public/conversation-overview.mjs +193 -0
- package/public/draft-conversations.mjs +135 -0
- package/public/focused-composer-controls.css +111 -0
- package/public/focused-composer-controls.mjs +143 -0
- package/public/focused-composer-disclosure.mjs +223 -0
- package/public/focused-shell-adapter.css +264 -0
- package/public/focused-shell-adapter.mjs +257 -0
- package/public/index.html +137 -80
- package/public/model-catalog-order.mjs +63 -0
- package/public/platform-event-stream.mjs +64 -0
- package/public/project-artifact-controller.mjs +218 -0
- package/public/project-file-viewer-controller.mjs +383 -0
- package/public/project-git-tools-controller.mjs +181 -0
- package/public/project-runners-controller.mjs +220 -0
- package/public/service-worker.js +52 -1
- package/public/session-attention-scheduler.mjs +182 -0
- package/public/session-composer-store.mjs +56 -0
- package/public/session-context.mjs +51 -0
- package/public/session-extension-requests.mjs +63 -0
- package/public/session-shelf-state.mjs +151 -0
- package/public/session-shelves.css +171 -0
- package/public/session-shelves.mjs +95 -0
- package/public/settings-navigation-controller.mjs +249 -0
- package/public/settings-navigation.css +61 -0
- package/public/settings-resource-controller.mjs +232 -0
- package/public/settings-workspace.css +19 -0
- package/public/settings-workspace.mjs +175 -0
- package/public/shell-actions.mjs +87 -0
- package/public/shell-attention-inbox.css +72 -0
- package/public/shell-attention-inbox.mjs +198 -0
- package/public/shell-context-actions.css +61 -0
- package/public/shell-context-actions.mjs +168 -0
- package/public/shell-inspector-resize.mjs +124 -0
- package/public/shell-navigation-resize.mjs +107 -0
- package/public/shell-navigation.mjs +127 -0
- package/public/shell-preferences.mjs +108 -0
- package/public/shell-surfaces.mjs +99 -0
- package/public/shell-tools-launcher.css +97 -0
- package/public/shell-tools-launcher.mjs +222 -0
- package/public/shell-view-host.mjs +115 -0
- package/public/shell-workspace.css +266 -0
- package/public/shell-workspace.mjs +402 -0
- package/public/sidebar-update-menu.css +190 -0
- package/public/sidebar-update-menu.mjs +512 -0
- package/public/styles.css +320 -169
- package/public/subagent-launch-slot-state.mjs +2 -2
- package/public/theme-contract.mjs +1 -1
- package/public/themed-selects.css +156 -0
- package/public/workspace-choices.mjs +104 -0
- package/tests/activity-agents-controller.test.mjs +652 -0
- package/tests/activity-events-controller.test.mjs +511 -0
- package/tests/app-runner-terminal-static.test.mjs +3 -3
- package/tests/append-system-http.test.mjs +7 -6
- package/tests/append-system-selector-static.test.mjs +6 -6
- package/tests/aur-review-widget-static.test.mjs +11 -3
- package/tests/boot-failure-diagnostics.test.mjs +8 -3
- package/tests/browser/activity-intercom-controller.spec.mjs +457 -0
- package/tests/browser/activity-navigation-integration.spec.mjs +45 -0
- package/tests/browser/activity-owner-integration.spec.mjs +201 -0
- package/tests/browser/activity-queue-controller.spec.mjs +405 -0
- package/tests/browser/activity-section-picker.spec.mjs +294 -0
- package/tests/browser/activity-todo-view.spec.mjs +60 -0
- package/tests/browser/activity-usage-controller.spec.mjs +416 -0
- package/tests/browser/compact-layout-integration.spec.mjs +58 -0
- package/tests/browser/composer-action-grid-reorder.spec.mjs +16 -0
- package/tests/browser/composer-disclosure.spec.mjs +389 -0
- package/tests/browser/composer-toolbar-polish.spec.mjs +343 -0
- package/tests/browser/control-deck-component-updates.spec.mjs +5 -14
- package/tests/browser/control-deck-side-panels.spec.mjs +27 -12
- package/tests/browser/control-visibility.spec.mjs +5 -0
- package/tests/browser/controls-layout.spec.mjs +21 -2
- package/tests/browser/conversation-overview.spec.mjs +251 -0
- package/tests/browser/draft-conversations.spec.mjs +276 -0
- package/tests/browser/empty-startup-fixture.mjs +135 -0
- package/tests/browser/empty-startup.spec.mjs +282 -0
- package/tests/browser/events-tree-context-menu.spec.mjs +1 -0
- package/tests/browser/focused-composer-more.spec.mjs +127 -0
- package/tests/browser/focused-model-shortcuts.spec.mjs +120 -0
- package/tests/browser/focused-navigation-fixture.mjs +32 -0
- package/tests/browser/focused-project-activity.spec.mjs +111 -0
- package/tests/browser/focused-shell-integration.spec.mjs +208 -0
- package/tests/browser/folder-picker-layout-fixture.mjs +13 -0
- package/tests/browser/folder-picker-layout.spec.mjs +302 -0
- package/tests/browser/footer-model-advanced-layout.spec.mjs +27 -14
- package/tests/browser/git-setup-cancel.spec.mjs +251 -0
- package/tests/browser/guided-git-fallback.spec.mjs +40 -21
- package/tests/browser/inspector-header.spec.mjs +221 -0
- package/tests/browser/inspector-overview-integration.spec.mjs +104 -0
- package/tests/browser/inspector-resize.spec.mjs +227 -0
- package/tests/browser/inspector-topic-overflow.spec.mjs +56 -0
- package/tests/browser/interaction-action-discovery.spec.mjs +332 -0
- package/tests/browser/interaction-attention-inbox.spec.mjs +364 -0
- package/tests/browser/interaction-attention-integration.spec.mjs +53 -0
- package/tests/browser/interaction-composer-controls.spec.mjs +267 -0
- package/tests/browser/interaction-composer-integration.spec.mjs +175 -0
- package/tests/browser/interaction-continuity.spec.mjs +50 -26
- package/tests/browser/interaction-model-picker.spec.mjs +86 -0
- package/tests/browser/last-terminal-close.spec.mjs +280 -0
- package/tests/browser/mobile-foundation.spec.mjs +172 -20
- package/tests/browser/model-catalog-groups.spec.mjs +104 -0
- package/tests/browser/more-row-menus.spec.mjs +258 -0
- package/tests/browser/new-tab-hit-target.spec.mjs +228 -0
- package/tests/browser/no-project-conversation.spec.mjs +113 -0
- package/tests/browser/optimistic-prompt.spec.mjs +3 -0
- package/tests/browser/overhaul-w7-accessibility.spec.mjs +227 -0
- package/tests/browser/overhaul-w7-capabilities.spec.mjs +298 -0
- package/tests/browser/overhaul-w7-companions.spec.mjs +290 -0
- package/tests/browser/overhaul-w7-focus-repairs.spec.mjs +210 -0
- package/tests/browser/overhaul-w7-layout-repairs.spec.mjs +183 -0
- package/tests/browser/overhaul-w7-parent-repairs.spec.mjs +97 -0
- package/tests/browser/overhaul-w7-pwa.spec.mjs +247 -0
- package/tests/browser/overhaul-w7-responsive.spec.mjs +450 -0
- package/tests/browser/overhaul-w7-update.spec.mjs +215 -0
- package/tests/browser/overhaul-w7-zoom.spec.mjs +243 -0
- package/tests/browser/overview-agent-ownership.spec.mjs +45 -0
- package/tests/browser/persistent-ui-layout.spec.mjs +3 -0
- package/tests/browser/project-artifact-controller.spec.mjs +324 -0
- package/tests/browser/project-artifact-integration.spec.mjs +103 -0
- package/tests/browser/project-file-buffer-integration.spec.mjs +230 -0
- package/tests/browser/project-git-tools-integration.spec.mjs +369 -0
- package/tests/browser/session-fixture.mjs +118 -0
- package/tests/browser/session-ownership.spec.mjs +175 -0
- package/tests/browser/session-shelves-integration.spec.mjs +219 -0
- package/tests/browser/session-shelves.spec.mjs +340 -0
- package/tests/browser/session-sidebar-resize.spec.mjs +138 -0
- package/tests/browser/session-summary.spec.mjs +17 -12
- package/tests/browser/settings-complete-home.spec.mjs +463 -0
- package/tests/browser/settings-entrypoint.spec.mjs +104 -0
- package/tests/browser/settings-navigation-controller.spec.mjs +383 -0
- package/tests/browser/settings-navigation-integration.spec.mjs +348 -0
- package/tests/browser/settings-resource-lifetime.spec.mjs +349 -0
- package/tests/browser/settings-workspace-integration.spec.mjs +132 -0
- package/tests/browser/shell-ownership.spec.mjs +398 -0
- package/tests/browser/shell-rollout-fixture.mjs +259 -0
- package/tests/browser/shell-rollout.spec.mjs +357 -0
- package/tests/browser/shell-tools-launcher.spec.mjs +260 -0
- package/tests/browser/shell-workspace.spec.mjs +433 -0
- package/tests/browser/side-panel-section-reorder.spec.mjs +3 -1
- package/tests/browser/sidebar-floating-tooltips.spec.mjs +37 -0
- package/tests/browser/sidebar-update-integration.spec.mjs +277 -0
- package/tests/browser/sidebar-update-menu.spec.mjs +402 -0
- package/tests/browser/sidebar-update-progress.spec.mjs +472 -0
- package/tests/browser/stream-output-isolation.spec.mjs +6 -3
- package/tests/browser/themed-selects.spec.mjs +297 -0
- package/tests/browser/thread-start-flow.spec.mjs +481 -0
- package/tests/browser/ui-polish-activity-controls.spec.mjs +177 -0
- package/tests/browser/ui-polish-features.spec.mjs +85 -0
- package/tests/browser/ui-polish-file-header.spec.mjs +43 -0
- package/tests/browser/ui-polish-local-git.spec.mjs +183 -0
- package/tests/browser/ui-polish-mobile-recovery.spec.mjs +36 -0
- package/tests/browser/ui-polish-navigation.spec.mjs +127 -0
- package/tests/browser/ui-polish-session-actions.spec.mjs +282 -0
- package/tests/browser/user-noticeable-fixes.spec.mjs +10 -3
- package/tests/browser/windows-drive-parent.spec.mjs +4 -2
- package/tests/browser/workspace-draft-selection.spec.mjs +266 -0
- package/tests/codex-cli-usage.test.mjs +189 -0
- package/tests/codex-fast-mode-static.test.mjs +16 -11
- package/tests/codex-speed-mode-browser-state.test.mjs +6 -3
- package/tests/codex-usage-auth.test.mjs +168 -9
- package/tests/codex-usage-http.test.mjs +124 -0
- package/tests/codex-usage-source.test.mjs +146 -0
- package/tests/component-update-api-static.test.mjs +2 -2
- package/tests/composer-action-grid-reorder-static.test.mjs +3 -3
- package/tests/control-deck-component-updates-static.test.mjs +9 -4
- package/tests/control-visibility-static.test.mjs +3 -3
- package/tests/controls-layout-static.test.mjs +5 -5
- package/tests/conversation-overview-adapter.test.mjs +100 -0
- package/tests/document-artifact-static.test.mjs +2 -1
- package/tests/draft-conversations.test.mjs +205 -0
- package/tests/empty-startup-launcher.test.mjs +197 -0
- package/tests/events-tree-context-menu-static.test.mjs +22 -18
- package/tests/fake-pi-session-ownership.test.mjs +302 -0
- package/tests/fast-mode-client-static.test.mjs +1 -1
- package/tests/feature-decision-output-popup-static.test.mjs +2 -2
- package/tests/feature-system-prompt-optional-feature-static.test.mjs +1 -1
- package/tests/file-viewer-image-static.test.mjs +15 -11
- package/tests/file-viewer-search-static.test.mjs +5 -5
- package/tests/fixtures/fake-codex-app-server.mjs +61 -0
- package/tests/fixtures/fake-pi.mjs +41 -9
- package/tests/footer-provider-usage-visual-static.test.mjs +7 -6
- package/tests/foreground-transcript-catch-up-static.test.mjs +7 -4
- package/tests/git-footer-context-menu-static.test.mjs +1 -1
- package/tests/git-panel-file-preview-static.test.mjs +41 -48
- package/tests/git-panel-live-updates-static.test.mjs +2 -1
- package/tests/git-workflow-dispatch.test.mjs +66 -0
- package/tests/git-workflow-preferences.test.mjs +9 -1
- package/tests/guided-git-activation-static.test.mjs +14 -11
- package/tests/guided-git-fallback-ui-static.test.mjs +8 -5
- package/tests/guided-git-message-reliability-static.test.mjs +10 -6
- package/tests/guided-git-publication-backend-static.test.mjs +16 -11
- package/tests/http-endpoints-harness.test.mjs +29 -9
- package/tests/interaction-state-stability-static.test.mjs +4 -3
- package/tests/intercom-conversation-viewer-static.test.mjs +29 -17
- package/tests/intercom-tag-layout-static.test.mjs +13 -12
- package/tests/intercom-tag-overflow-layout.test.mjs +11 -10
- package/tests/main-output-loading-static.test.mjs +4 -4
- package/tests/middle-button-drag-scroll-static.test.mjs +3 -3
- package/tests/mobile-compact-layout.test.mjs +2 -2
- package/tests/mobile-continuity-tablet-static.test.mjs +3 -3
- package/tests/mobile-foundation-static.test.mjs +10 -7
- package/tests/mobile-phone-experience-static.test.mjs +3 -2
- package/tests/mobile-static.test.mjs +173 -135
- package/tests/model-catalog-order.test.mjs +103 -0
- package/tests/native-parity.test.mjs +6 -4
- package/tests/native-session-choice-owner.test.mjs +102 -0
- package/tests/native-update-lifecycle.test.mjs +10 -5
- package/tests/no-project-http-harness.test.mjs +328 -0
- package/tests/no-project-navigation.test.mjs +54 -0
- package/tests/no-project-workspaces.test.mjs +77 -0
- package/tests/open-issue-wizard-static.test.mjs +1 -1
- package/tests/optional-feature-migration-frontend.test.mjs +4 -4
- package/tests/optional-feature-prompt-impact-static.test.mjs +3 -3
- package/tests/package-updates.test.mjs +630 -0
- package/tests/persistent-ui-layout-static.test.mjs +3 -3
- package/tests/platform-event-stream.test.mjs +253 -0
- package/tests/project-file-viewer-controller.test.mjs +652 -0
- package/tests/project-git-tools-adapter.test.mjs +103 -0
- package/tests/project-git-tools-controller.test.mjs +577 -0
- package/tests/project-runners-controller.test.mjs +453 -0
- package/tests/prompt-input-vertical-scroll-static.test.mjs +3 -3
- package/tests/prompt-routing-input-clear-static.test.mjs +2 -2
- package/tests/questionnaire-dialog.test.mjs +6 -6
- package/tests/queue-edit-reorder-static.test.mjs +48 -51
- package/tests/rpc-supervisor-host.test.mjs +4 -1
- package/tests/safety-guard-auto-review-static.test.mjs +1 -1
- package/tests/sampling-native-controls-user-flow.test.mjs +7 -1
- package/tests/server-app-runners.test.mjs +536 -0
- package/tests/server-codex-fast-mode.test.mjs +346 -0
- package/tests/server-conversation-mode.test.mjs +386 -0
- package/tests/server-conversation-voice.test.mjs +426 -0
- package/tests/server-extension-ui-requests.test.mjs +357 -0
- package/tests/server-git-generation.test.mjs +558 -0
- package/tests/server-git-mutations.test.mjs +729 -0
- package/tests/server-git-reads.test.mjs +381 -0
- package/tests/server-git-worktree-sessions.test.mjs +502 -0
- package/tests/server-helper-requests.test.mjs +364 -0
- package/tests/server-rpc-transports.test.mjs +553 -0
- package/tests/server-settings-store.test.mjs +132 -0
- package/tests/server-shell-rollout.test.mjs +478 -0
- package/tests/server-sse-delivery.test.mjs +410 -0
- package/tests/server-workspace-files.test.mjs +546 -0
- package/tests/session-attention-scheduler.test.mjs +514 -0
- package/tests/session-composer-store.test.mjs +153 -0
- package/tests/session-context.test.mjs +134 -0
- package/tests/session-extension-requests.test.mjs +193 -0
- package/tests/session-ownership-adapter.test.mjs +130 -0
- package/tests/session-sampling-ui-static.test.mjs +10 -4
- package/tests/session-send-lifetime.test.mjs +140 -0
- package/tests/session-shelf-state.test.mjs +348 -0
- package/tests/session-shelves-adapter.test.mjs +79 -0
- package/tests/session-summary-lifetime.test.mjs +54 -0
- package/tests/settings-resource-controller.test.mjs +548 -0
- package/tests/shell-action-adapter.test.mjs +169 -0
- package/tests/shell-actions.test.mjs +182 -0
- package/tests/shell-navigation.test.mjs +348 -0
- package/tests/shell-preferences.test.mjs +367 -0
- package/tests/side-panel-section-height-removal-static.test.mjs +3 -3
- package/tests/stream-output-isolation-static.test.mjs +1 -1
- package/tests/subagent-gate-visibility.test.mjs +1 -1
- package/tests/subagent-launch-slot-state.test.mjs +2 -2
- package/tests/subagent-launch-slots-static.test.mjs +1 -1
- package/tests/subagent-observability-static.test.mjs +2 -2
- package/tests/subagent-reliability-integration.test.mjs +13 -30
- package/tests/syntax-highlighting-static.test.mjs +3 -3
- package/tests/tab-close-continuity-static.test.mjs +1 -1
- package/tests/tab-metadata-ordering.test.mjs +99 -0
- package/tests/terminal-cwd-grouping-static.test.mjs +2 -2
- package/tests/terminal-tab-prompt-tooltip-static.test.mjs +2 -0
- package/tests/terminal-tab-workspace-static.test.mjs +5 -4
- package/tests/terminal-tabs-resize-static.test.mjs +1 -1
- package/tests/theme-contract.test.mjs +10 -3
- package/tests/theme-customizer-static.test.mjs +1 -1
- package/tests/thread-display-title.test.mjs +44 -0
- package/tests/update-api-harness.test.mjs +15 -14
- package/tests/update-tab-restore-harness.test.mjs +3 -0
- package/tests/webui-settings-locking.test.mjs +2 -2
- package/tests/windows-drive-cwd-picker-static.test.mjs +7 -9
- package/tests/workspace-choices.test.mjs +114 -0
- package/tests/workspace-save-load-static.test.mjs +7 -4
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { createAttentionScheduler } from "../public/session-attention-scheduler.mjs";
|
|
4
|
+
|
|
5
|
+
async function flush() {
|
|
6
|
+
await Promise.resolve();
|
|
7
|
+
await Promise.resolve();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
class FakeClock {
|
|
11
|
+
now = 0;
|
|
12
|
+
next = 0;
|
|
13
|
+
timers = new Map();
|
|
14
|
+
peakTimers = 0;
|
|
15
|
+
|
|
16
|
+
setTimeout = (callback, delay) => {
|
|
17
|
+
assert.ok(Number.isFinite(delay) && delay >= 0);
|
|
18
|
+
const id = this.next++;
|
|
19
|
+
this.timers.set(id, { callback, at: this.now + delay });
|
|
20
|
+
this.peakTimers = Math.max(this.peakTimers, this.timers.size);
|
|
21
|
+
assert.ok(this.timers.size <= 1, "only one scheduler timer may exist, including the request timeout");
|
|
22
|
+
return id;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
clearTimeout = (id) => this.timers.delete(id);
|
|
26
|
+
|
|
27
|
+
async tick(ms) {
|
|
28
|
+
const end = this.now + ms;
|
|
29
|
+
await flush();
|
|
30
|
+
for (let count = 0; ; count++) {
|
|
31
|
+
assert.ok(count < 1000, "timer execution must remain bounded");
|
|
32
|
+
const due = [...this.timers.entries()]
|
|
33
|
+
.filter(([, timer]) => timer.at <= end)
|
|
34
|
+
.sort((a, b) => a[1].at - b[1].at || a[0] - b[0])[0];
|
|
35
|
+
if (!due) break;
|
|
36
|
+
const [id, timer] = due;
|
|
37
|
+
this.now = timer.at;
|
|
38
|
+
this.timers.delete(id);
|
|
39
|
+
timer.callback();
|
|
40
|
+
await flush();
|
|
41
|
+
}
|
|
42
|
+
this.now = end;
|
|
43
|
+
await flush();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function harness(t, { state = {}, abortable = true, onRefresh, onError } = {}) {
|
|
48
|
+
const clock = new FakeClock();
|
|
49
|
+
t.mock.method(Date, "now", () => clock.now);
|
|
50
|
+
const current = { tabCount: 2, working: false, visible: true, menuOpen: false, ...state };
|
|
51
|
+
const requests = [];
|
|
52
|
+
const errors = [];
|
|
53
|
+
const scheduler = createAttentionScheduler({
|
|
54
|
+
getState: () => current,
|
|
55
|
+
setTimeout: clock.setTimeout,
|
|
56
|
+
clearTimeout: clock.clearTimeout,
|
|
57
|
+
onError(error) {
|
|
58
|
+
errors.push(error);
|
|
59
|
+
onError?.(error);
|
|
60
|
+
},
|
|
61
|
+
refresh({ signal }) {
|
|
62
|
+
assert.ok(signal instanceof AbortSignal);
|
|
63
|
+
assert.ok(requests.every((request) => request.settled), "refresh calls must never overlap");
|
|
64
|
+
let resolve;
|
|
65
|
+
let reject;
|
|
66
|
+
const promise = new Promise((yes, no) => { resolve = yes; reject = no; });
|
|
67
|
+
const request = { signal, at: clock.now, settled: false };
|
|
68
|
+
const finish = (callback, value) => {
|
|
69
|
+
if (request.settled) return;
|
|
70
|
+
request.settled = true;
|
|
71
|
+
signal.removeEventListener("abort", abort);
|
|
72
|
+
callback(value);
|
|
73
|
+
};
|
|
74
|
+
const abort = () => finish(reject, signal.reason);
|
|
75
|
+
request.resolve = (value) => finish(resolve, value);
|
|
76
|
+
request.reject = (error) => finish(reject, error);
|
|
77
|
+
if (abortable) signal.addEventListener("abort", abort, { once: true });
|
|
78
|
+
requests.push(request);
|
|
79
|
+
onRefresh?.(request);
|
|
80
|
+
return promise;
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
t.after(async () => {
|
|
84
|
+
scheduler.dispose();
|
|
85
|
+
for (const request of requests) request.resolve();
|
|
86
|
+
await flush();
|
|
87
|
+
assert.equal(clock.timers.size, 0, "disposal must leave no timers");
|
|
88
|
+
});
|
|
89
|
+
return { clock, state: current, requests, errors, scheduler };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
test("multiple initially idle tabs discover a later background blocker without manual refresh", async (t) => {
|
|
93
|
+
let serverBlocked = false;
|
|
94
|
+
let discovered = false;
|
|
95
|
+
const h = harness(t, {
|
|
96
|
+
onRefresh(request) {
|
|
97
|
+
discovered = serverBlocked;
|
|
98
|
+
h.state.working = discovered;
|
|
99
|
+
request.resolve();
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
h.scheduler.sync();
|
|
103
|
+
await h.clock.tick(9999);
|
|
104
|
+
assert.equal(h.requests.length, 0);
|
|
105
|
+
serverBlocked = true;
|
|
106
|
+
await h.clock.tick(1);
|
|
107
|
+
assert.equal(discovered, true);
|
|
108
|
+
assert.equal(h.requests.length, 1);
|
|
109
|
+
await h.clock.tick(1499);
|
|
110
|
+
assert.equal(h.requests.length, 1);
|
|
111
|
+
await h.clock.tick(1);
|
|
112
|
+
assert.equal(h.requests.length, 2);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
for (const [name, state, interval] of [
|
|
116
|
+
["no tabs", { tabCount: 0, working: true }, null],
|
|
117
|
+
["one idle visible tab", { tabCount: 1 }, null],
|
|
118
|
+
["one idle hidden tab", { tabCount: 1, visible: false }, null],
|
|
119
|
+
["one working visible tab", { tabCount: 1, working: true }, 1500],
|
|
120
|
+
["multiple idle visible tabs", {}, 10000],
|
|
121
|
+
["multiple idle hidden tabs", { visible: false }, 30000],
|
|
122
|
+
["one working hidden tab", { tabCount: 1, working: true, visible: false }, 30000],
|
|
123
|
+
]) {
|
|
124
|
+
test(`recurring cadence for ${name}`, async (t) => {
|
|
125
|
+
const h = harness(t, { state, onRefresh: (request) => request.resolve() });
|
|
126
|
+
h.scheduler.sync();
|
|
127
|
+
if (interval === null) {
|
|
128
|
+
await h.clock.tick(120000);
|
|
129
|
+
assert.equal(h.requests.length, 0);
|
|
130
|
+
assert.equal(h.clock.timers.size, 0);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
await h.clock.tick(interval - 1);
|
|
134
|
+
assert.equal(h.requests.length, 0);
|
|
135
|
+
await h.clock.tick(1);
|
|
136
|
+
assert.equal(h.requests.length, 1);
|
|
137
|
+
await h.clock.tick(interval);
|
|
138
|
+
assert.deepEqual(h.requests.map((request) => request.at), [interval, interval * 2]);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
test("sync and manual requests preserve the earliest deadline across cadence changes", async (t) => {
|
|
143
|
+
const h = harness(t);
|
|
144
|
+
h.scheduler.sync();
|
|
145
|
+
await h.clock.tick(1000);
|
|
146
|
+
h.scheduler.sync();
|
|
147
|
+
await h.clock.tick(1000);
|
|
148
|
+
h.scheduler.request(5000);
|
|
149
|
+
h.scheduler.request(10000);
|
|
150
|
+
await h.clock.tick(1000);
|
|
151
|
+
h.state.working = true;
|
|
152
|
+
h.scheduler.sync();
|
|
153
|
+
h.state.visible = false;
|
|
154
|
+
h.scheduler.sync();
|
|
155
|
+
await h.clock.tick(1499);
|
|
156
|
+
assert.equal(h.requests.length, 0);
|
|
157
|
+
await h.clock.tick(1);
|
|
158
|
+
assert.equal(h.requests[0].at, 4500);
|
|
159
|
+
assert.equal(h.clock.peakTimers, 1);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test("repeated sync never postpones an existing idle discovery deadline", async (t) => {
|
|
163
|
+
const h = harness(t);
|
|
164
|
+
h.scheduler.sync();
|
|
165
|
+
for (let i = 0; i < 9; i++) {
|
|
166
|
+
await h.clock.tick(1000);
|
|
167
|
+
h.scheduler.sync();
|
|
168
|
+
}
|
|
169
|
+
await h.clock.tick(1000);
|
|
170
|
+
assert.equal(h.requests[0].at, 10000);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test("zero tabs reject manual network work; a single idle tab permits it", async (t) => {
|
|
174
|
+
const h = harness(t, { state: { tabCount: 0 } });
|
|
175
|
+
h.scheduler.request();
|
|
176
|
+
h.scheduler.request(100);
|
|
177
|
+
await h.clock.tick(10000);
|
|
178
|
+
assert.equal(h.requests.length, 0);
|
|
179
|
+
assert.equal(h.clock.timers.size, 0);
|
|
180
|
+
h.state.tabCount = 1;
|
|
181
|
+
h.scheduler.request(1000);
|
|
182
|
+
h.scheduler.request(100);
|
|
183
|
+
h.scheduler.request(500);
|
|
184
|
+
h.scheduler.sync();
|
|
185
|
+
await h.clock.tick(99);
|
|
186
|
+
assert.equal(h.requests.length, 0);
|
|
187
|
+
await h.clock.tick(1);
|
|
188
|
+
h.requests[0].resolve();
|
|
189
|
+
await h.clock.tick(120000);
|
|
190
|
+
assert.equal(h.requests.length, 1);
|
|
191
|
+
assert.equal(h.clock.timers.size, 0);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test("manual requests during a refresh coalesce without replacing its timeout", async (t) => {
|
|
195
|
+
const h = harness(t, { state: { tabCount: 1 } });
|
|
196
|
+
h.scheduler.request();
|
|
197
|
+
await h.clock.tick(0);
|
|
198
|
+
const timeout = [...h.clock.timers.values()][0];
|
|
199
|
+
h.scheduler.request(5000);
|
|
200
|
+
await h.clock.tick(200);
|
|
201
|
+
for (let i = 0; i < 20; i++) {
|
|
202
|
+
h.scheduler.request();
|
|
203
|
+
h.scheduler.sync();
|
|
204
|
+
}
|
|
205
|
+
assert.equal(h.requests.length, 1);
|
|
206
|
+
assert.equal([...h.clock.timers.values()][0], timeout);
|
|
207
|
+
h.requests[0].resolve();
|
|
208
|
+
await h.clock.tick(0);
|
|
209
|
+
assert.equal(h.requests.length, 2);
|
|
210
|
+
h.requests[1].resolve();
|
|
211
|
+
await h.clock.tick(10000);
|
|
212
|
+
assert.equal(h.requests.length, 2);
|
|
213
|
+
assert.equal(h.clock.timers.size, 0);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test("a delayed manual request during a refresh retains its earliest requested deadline", async (t) => {
|
|
217
|
+
const h = harness(t, { state: { tabCount: 1 } });
|
|
218
|
+
h.scheduler.request();
|
|
219
|
+
await h.clock.tick(0);
|
|
220
|
+
h.scheduler.request(5000);
|
|
221
|
+
h.scheduler.request(8000);
|
|
222
|
+
await h.clock.tick(1000);
|
|
223
|
+
h.requests[0].resolve();
|
|
224
|
+
await h.clock.tick(3999);
|
|
225
|
+
assert.equal(h.requests.length, 1);
|
|
226
|
+
await h.clock.tick(1);
|
|
227
|
+
assert.equal(h.requests[1].at, 5000);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test("sync during refresh schedules the next recurring interval from settlement", async (t) => {
|
|
231
|
+
const h = harness(t, { state: { working: true } });
|
|
232
|
+
h.scheduler.sync();
|
|
233
|
+
await h.clock.tick(1500);
|
|
234
|
+
await h.clock.tick(2000);
|
|
235
|
+
h.scheduler.sync();
|
|
236
|
+
h.scheduler.sync();
|
|
237
|
+
h.requests[0].resolve();
|
|
238
|
+
await h.clock.tick(1499);
|
|
239
|
+
assert.equal(h.requests.length, 1);
|
|
240
|
+
await h.clock.tick(1);
|
|
241
|
+
assert.equal(h.requests[1].at, 5000);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test("open menus defer in 600ms steps, including repeated manual requests and sync", async (t) => {
|
|
245
|
+
const h = harness(t, { state: { tabCount: 1, menuOpen: true } });
|
|
246
|
+
h.scheduler.request();
|
|
247
|
+
await h.clock.tick(0);
|
|
248
|
+
await h.clock.tick(599);
|
|
249
|
+
h.scheduler.sync();
|
|
250
|
+
h.scheduler.request();
|
|
251
|
+
assert.equal(h.requests.length, 0);
|
|
252
|
+
await h.clock.tick(1);
|
|
253
|
+
assert.equal(h.requests.length, 0);
|
|
254
|
+
h.state.menuOpen = false;
|
|
255
|
+
h.scheduler.sync();
|
|
256
|
+
await h.clock.tick(599);
|
|
257
|
+
assert.equal(h.requests.length, 0);
|
|
258
|
+
await h.clock.tick(1);
|
|
259
|
+
assert.equal(h.requests[0].at, 1200);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
test("sync cancels recurring work when only one idle tab remains but preserves manual work", async (t) => {
|
|
263
|
+
const h = harness(t);
|
|
264
|
+
h.scheduler.sync();
|
|
265
|
+
h.state.tabCount = 1;
|
|
266
|
+
h.scheduler.sync();
|
|
267
|
+
assert.equal(h.clock.timers.size, 0);
|
|
268
|
+
h.scheduler.request(50);
|
|
269
|
+
h.scheduler.sync();
|
|
270
|
+
await h.clock.tick(50);
|
|
271
|
+
assert.equal(h.requests.length, 1);
|
|
272
|
+
h.state.tabCount = 0;
|
|
273
|
+
h.scheduler.sync();
|
|
274
|
+
assert.equal(h.requests[0].signal.aborted, true);
|
|
275
|
+
await h.clock.tick(60000);
|
|
276
|
+
assert.equal(h.errors.length, 0);
|
|
277
|
+
assert.equal(h.clock.timers.size, 0);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
test("a no-longer-needed automatic poll cannot pull a later manual deadline forward", async (t) => {
|
|
281
|
+
const h = harness(t);
|
|
282
|
+
h.scheduler.sync();
|
|
283
|
+
h.scheduler.request(20000);
|
|
284
|
+
h.state.tabCount = 1;
|
|
285
|
+
await h.clock.tick(19999);
|
|
286
|
+
assert.equal(h.requests.length, 0);
|
|
287
|
+
await h.clock.tick(1);
|
|
288
|
+
assert.equal(h.requests[0].at, 20000);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
test("timer callbacks recheck zero-tab and single-idle state even without sync", async (t) => {
|
|
292
|
+
const h = harness(t);
|
|
293
|
+
h.scheduler.sync();
|
|
294
|
+
h.state.tabCount = 1;
|
|
295
|
+
await h.clock.tick(10000);
|
|
296
|
+
assert.equal(h.requests.length, 0);
|
|
297
|
+
assert.equal(h.clock.timers.size, 0);
|
|
298
|
+
h.scheduler.request(100);
|
|
299
|
+
h.state.tabCount = 0;
|
|
300
|
+
await h.clock.tick(100);
|
|
301
|
+
assert.equal(h.requests.length, 0);
|
|
302
|
+
assert.equal(h.clock.timers.size, 0);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
test("settlement after the last tab disappears neither reports nor retries", async (t) => {
|
|
306
|
+
const h = harness(t);
|
|
307
|
+
h.scheduler.request();
|
|
308
|
+
await h.clock.tick(0);
|
|
309
|
+
h.state.tabCount = 0;
|
|
310
|
+
h.requests[0].reject(new Error("last tab closed"));
|
|
311
|
+
await h.clock.tick(60000);
|
|
312
|
+
assert.equal(h.errors.length, 0);
|
|
313
|
+
assert.equal(h.requests.length, 1);
|
|
314
|
+
assert.equal(h.clock.timers.size, 0);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test("10s timeout aborts through AbortSignal and retries after backoff", async (t) => {
|
|
318
|
+
const h = harness(t, { state: { working: true } });
|
|
319
|
+
h.scheduler.sync();
|
|
320
|
+
await h.clock.tick(1500);
|
|
321
|
+
await h.clock.tick(9999);
|
|
322
|
+
assert.equal(h.requests[0].signal.aborted, false);
|
|
323
|
+
await h.clock.tick(1);
|
|
324
|
+
assert.equal(h.requests[0].signal.aborted, true);
|
|
325
|
+
assert.equal(h.errors.length, 1);
|
|
326
|
+
assert.equal(h.errors[0].name, "TimeoutError");
|
|
327
|
+
assert.equal(h.requests[0].signal.reason, h.errors[0]);
|
|
328
|
+
h.scheduler.request();
|
|
329
|
+
h.scheduler.sync();
|
|
330
|
+
await h.clock.tick(1499);
|
|
331
|
+
assert.equal(h.requests.length, 1);
|
|
332
|
+
await h.clock.tick(1);
|
|
333
|
+
assert.equal(h.requests[1].at, 13000);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
test("an unsettled timed-out refresh cannot overlap a retry even if it ignores abort", async (t) => {
|
|
337
|
+
const h = harness(t, { state: { tabCount: 1 }, abortable: false });
|
|
338
|
+
h.scheduler.request();
|
|
339
|
+
await h.clock.tick(10000);
|
|
340
|
+
assert.equal(h.requests[0].signal.aborted, true);
|
|
341
|
+
h.scheduler.request();
|
|
342
|
+
h.scheduler.sync();
|
|
343
|
+
await h.clock.tick(60000);
|
|
344
|
+
assert.equal(h.requests.length, 1);
|
|
345
|
+
assert.equal(h.clock.timers.size, 0);
|
|
346
|
+
h.requests[0].resolve();
|
|
347
|
+
await h.clock.tick(0);
|
|
348
|
+
assert.equal(h.errors.length, 1);
|
|
349
|
+
assert.equal(h.errors[0].name, "TimeoutError", "late success cannot turn a timeout into success");
|
|
350
|
+
await h.clock.tick(1500);
|
|
351
|
+
assert.equal(h.requests.length, 2);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
test("failure backoff doubles to 30s, survives sync/manual pressure, and resets on success", async (t) => {
|
|
355
|
+
const h = harness(t, { state: { working: true } });
|
|
356
|
+
h.scheduler.request();
|
|
357
|
+
await h.clock.tick(0);
|
|
358
|
+
for (const [index, delay] of [1500, 3000, 6000, 12000, 24000, 30000, 30000].entries()) {
|
|
359
|
+
const error = new Error(`failure ${index}`);
|
|
360
|
+
h.requests.at(-1).reject(error);
|
|
361
|
+
await h.clock.tick(0);
|
|
362
|
+
assert.equal(h.errors.at(-1), error);
|
|
363
|
+
h.scheduler.sync();
|
|
364
|
+
h.scheduler.request();
|
|
365
|
+
await h.clock.tick(delay - 1);
|
|
366
|
+
assert.equal(h.requests.length, index + 1);
|
|
367
|
+
await h.clock.tick(1);
|
|
368
|
+
assert.equal(h.requests.length, index + 2);
|
|
369
|
+
}
|
|
370
|
+
h.requests.at(-1).resolve();
|
|
371
|
+
await h.clock.tick(1499);
|
|
372
|
+
assert.equal(h.requests.length, 8);
|
|
373
|
+
await h.clock.tick(1);
|
|
374
|
+
assert.equal(h.requests.length, 9);
|
|
375
|
+
h.requests.at(-1).reject(new Error("new failure"));
|
|
376
|
+
await h.clock.tick(1500);
|
|
377
|
+
assert.equal(h.requests.length, 10);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
test("idle and hidden failures retain their slower reconciliation intervals", async (t) => {
|
|
381
|
+
const h = harness(t);
|
|
382
|
+
h.scheduler.request();
|
|
383
|
+
await h.clock.tick(0);
|
|
384
|
+
h.requests[0].reject(new Error("idle failure"));
|
|
385
|
+
await h.clock.tick(9999);
|
|
386
|
+
assert.equal(h.requests.length, 1);
|
|
387
|
+
await h.clock.tick(1);
|
|
388
|
+
h.state.visible = false;
|
|
389
|
+
h.requests[1].reject(new Error("hidden failure"));
|
|
390
|
+
await h.clock.tick(29999);
|
|
391
|
+
assert.equal(h.requests.length, 2);
|
|
392
|
+
await h.clock.tick(1);
|
|
393
|
+
assert.equal(h.requests[2].at, 40000);
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
test("stop aborts and suppresses late results; restart waits for old settlement", async (t) => {
|
|
397
|
+
const h = harness(t, { abortable: false });
|
|
398
|
+
h.scheduler.request();
|
|
399
|
+
await h.clock.tick(0);
|
|
400
|
+
h.scheduler.request();
|
|
401
|
+
h.scheduler.stop();
|
|
402
|
+
assert.equal(h.requests[0].signal.aborted, true);
|
|
403
|
+
assert.equal(h.clock.timers.size, 0);
|
|
404
|
+
h.scheduler.request();
|
|
405
|
+
await h.clock.tick(60000);
|
|
406
|
+
assert.equal(h.requests.length, 1);
|
|
407
|
+
h.requests[0].reject(new Error("stale stopped request"));
|
|
408
|
+
await h.clock.tick(0);
|
|
409
|
+
assert.equal(h.errors.length, 0);
|
|
410
|
+
assert.equal(h.requests.length, 2);
|
|
411
|
+
h.scheduler.stop();
|
|
412
|
+
h.requests[1].resolve();
|
|
413
|
+
await h.clock.tick(60000);
|
|
414
|
+
assert.equal(h.requests.length, 2);
|
|
415
|
+
assert.equal(h.clock.timers.size, 0);
|
|
416
|
+
h.scheduler.sync();
|
|
417
|
+
await h.clock.tick(10000);
|
|
418
|
+
assert.equal(h.requests.length, 3);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
test("dispose is terminal and suppresses late errors and saved timer callbacks", async (t) => {
|
|
422
|
+
const h = harness(t, { abortable: false });
|
|
423
|
+
h.scheduler.sync();
|
|
424
|
+
const stalePoll = [...h.clock.timers.values()][0].callback;
|
|
425
|
+
h.scheduler.request();
|
|
426
|
+
await h.clock.tick(0);
|
|
427
|
+
const staleTimeout = [...h.clock.timers.values()][0].callback;
|
|
428
|
+
h.scheduler.dispose();
|
|
429
|
+
h.requests[0].reject(new Error("late disposed request"));
|
|
430
|
+
stalePoll();
|
|
431
|
+
staleTimeout();
|
|
432
|
+
h.scheduler.sync();
|
|
433
|
+
h.scheduler.request();
|
|
434
|
+
h.scheduler.stop();
|
|
435
|
+
h.scheduler.dispose();
|
|
436
|
+
await h.clock.tick(60000);
|
|
437
|
+
assert.equal(h.errors.length, 0);
|
|
438
|
+
assert.equal(h.requests.length, 1);
|
|
439
|
+
assert.equal(h.clock.timers.size, 0);
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
test("stopped timers cannot refresh after restart even if their callbacks were already queued", async (t) => {
|
|
443
|
+
const h = harness(t, { state: { tabCount: 1 } });
|
|
444
|
+
h.scheduler.request();
|
|
445
|
+
const stale = [...h.clock.timers.values()][0].callback;
|
|
446
|
+
h.scheduler.stop();
|
|
447
|
+
h.scheduler.request(100);
|
|
448
|
+
stale();
|
|
449
|
+
assert.equal(h.requests.length, 0);
|
|
450
|
+
await h.clock.tick(100);
|
|
451
|
+
assert.equal(h.requests.length, 1);
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
test("an error callback may stop ownership or throw without leaking timers", async (t) => {
|
|
455
|
+
const h = harness(t, { onError() { h.scheduler.stop(); throw new Error("presentation failed"); } });
|
|
456
|
+
h.scheduler.request();
|
|
457
|
+
await h.clock.tick(0);
|
|
458
|
+
h.requests[0].reject(new Error("network failed"));
|
|
459
|
+
await h.clock.tick(60000);
|
|
460
|
+
assert.equal(h.errors.length, 1);
|
|
461
|
+
assert.equal(h.requests.length, 1);
|
|
462
|
+
assert.equal(h.clock.timers.size, 0);
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
test("a throwing error callback preserves bounded retry ownership", async (t) => {
|
|
466
|
+
const h = harness(t, { onError() { throw new Error("presentation failed"); } });
|
|
467
|
+
h.scheduler.request();
|
|
468
|
+
await h.clock.tick(0);
|
|
469
|
+
h.requests[0].reject(new Error("network failed"));
|
|
470
|
+
await h.clock.tick(9999);
|
|
471
|
+
assert.equal(h.requests.length, 1);
|
|
472
|
+
await h.clock.tick(1);
|
|
473
|
+
assert.equal(h.requests.length, 2);
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
test("synchronous refresh throws retry and successful synchronous results release ownership", async (t) => {
|
|
477
|
+
const clock = new FakeClock();
|
|
478
|
+
t.mock.method(Date, "now", () => clock.now);
|
|
479
|
+
let calls = 0;
|
|
480
|
+
const failure = new Error("synchronous failure");
|
|
481
|
+
const errors = [];
|
|
482
|
+
const scheduler = createAttentionScheduler({
|
|
483
|
+
getState: () => ({ tabCount: 1, working: false, visible: true, menuOpen: false }),
|
|
484
|
+
refresh() { if (++calls === 1) throw failure; },
|
|
485
|
+
onError: (error) => errors.push(error),
|
|
486
|
+
setTimeout: clock.setTimeout,
|
|
487
|
+
clearTimeout: clock.clearTimeout,
|
|
488
|
+
});
|
|
489
|
+
t.after(() => scheduler.dispose());
|
|
490
|
+
scheduler.request();
|
|
491
|
+
await clock.tick(1500);
|
|
492
|
+
assert.equal(calls, 2);
|
|
493
|
+
assert.deepEqual(errors, [failure]);
|
|
494
|
+
assert.equal(clock.timers.size, 0);
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
test("global timer defaults work and invalid inputs fail at the boundary", async (t) => {
|
|
498
|
+
const clock = new FakeClock();
|
|
499
|
+
t.mock.method(Date, "now", () => clock.now);
|
|
500
|
+
t.mock.method(globalThis, "setTimeout", clock.setTimeout);
|
|
501
|
+
t.mock.method(globalThis, "clearTimeout", clock.clearTimeout);
|
|
502
|
+
const options = { refresh() {}, getState: () => ({ tabCount: 1 }) };
|
|
503
|
+
assert.throws(() => createAttentionScheduler(), /refresh must be a function/);
|
|
504
|
+
for (const key of ["refresh", "getState", "onError", "setTimeout", "clearTimeout"]) {
|
|
505
|
+
assert.throws(() => createAttentionScheduler({ ...options, [key]: null }), TypeError);
|
|
506
|
+
}
|
|
507
|
+
const scheduler = createAttentionScheduler(options);
|
|
508
|
+
t.after(() => scheduler.dispose());
|
|
509
|
+
assert.deepEqual(Object.keys(scheduler), ["sync", "request", "stop", "dispose"]);
|
|
510
|
+
for (const delay of [-1, NaN, Infinity, "0"]) assert.throws(() => scheduler.request(delay), RangeError);
|
|
511
|
+
scheduler.request();
|
|
512
|
+
await clock.tick(0);
|
|
513
|
+
assert.equal(clock.timers.size, 0);
|
|
514
|
+
});
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { createSessionComposerStore } from "../public/session-composer-store.mjs";
|
|
4
|
+
|
|
5
|
+
test("missing reads do not create draft, attachment or compaction state", () => {
|
|
6
|
+
const store = createSessionComposerStore();
|
|
7
|
+
assert.equal(store.getDraft("a"), "");
|
|
8
|
+
const absent = store.getAttachments("a");
|
|
9
|
+
absent.push({ id: "unowned" });
|
|
10
|
+
assert.deepEqual(store.getAttachments("a"), []);
|
|
11
|
+
assert.equal(store.hasDraft("a"), false);
|
|
12
|
+
assert.equal(store.hasAttachments("a"), false);
|
|
13
|
+
assert.equal(store.isCompactionPending("a"), false);
|
|
14
|
+
assert.deepEqual([...store.draftEntries()], []);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("drafts are isolated strings and an explicitly empty draft remains present", () => {
|
|
18
|
+
const store = createSessionComposerStore();
|
|
19
|
+
store.setDraft("a", "first\nline");
|
|
20
|
+
store.setDraft("b", "second");
|
|
21
|
+
store.setDraft("empty", "");
|
|
22
|
+
assert.equal(store.getDraft("a"), "first\nline");
|
|
23
|
+
assert.equal(store.getDraft("b"), "second");
|
|
24
|
+
assert.equal(store.hasDraft("empty"), true);
|
|
25
|
+
assert.equal(store.getDraft("empty"), "");
|
|
26
|
+
assert.deepEqual([...store.draftEntries()], [["a", "first\nline"], ["b", "second"], ["empty", ""]]);
|
|
27
|
+
const entry = store.draftEntries().next().value;
|
|
28
|
+
entry[1] = "external change";
|
|
29
|
+
assert.equal(store.getDraft("a"), "first\nline");
|
|
30
|
+
store.setDraft("a", "updated");
|
|
31
|
+
assert.equal(store.getDraft("b"), "second");
|
|
32
|
+
assert.equal(store.deleteDraft("a"), true);
|
|
33
|
+
assert.equal(store.deleteDraft("a"), false);
|
|
34
|
+
assert.equal(store.hasDraft("a"), false);
|
|
35
|
+
assert.equal(store.getDraft("a"), "");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("ensure retains a distinct mutable attachment array for each tab", () => {
|
|
39
|
+
const store = createSessionComposerStore();
|
|
40
|
+
const first = store.ensureAttachments("a");
|
|
41
|
+
const second = store.ensureAttachments("b");
|
|
42
|
+
assert.notEqual(first, second);
|
|
43
|
+
assert.equal(store.hasAttachments("a"), true);
|
|
44
|
+
assert.equal(store.getAttachments("a"), first);
|
|
45
|
+
assert.equal(store.ensureAttachments("a"), first);
|
|
46
|
+
const item = { id: "attachment-a" };
|
|
47
|
+
first.push(item);
|
|
48
|
+
assert.equal(store.getAttachments("a")[0], item);
|
|
49
|
+
assert.deepEqual(store.getAttachments("b"), []);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("setting attachments retains arrays, metadata objects, File and Blob references", () => {
|
|
53
|
+
const store = createSessionComposerStore();
|
|
54
|
+
const file = new File(["file content"], "example.txt", { type: "text/plain" });
|
|
55
|
+
const blob = new Blob(["blob content"], { type: "text/plain" });
|
|
56
|
+
const item = { id: "attachment-a", file, blob, metadata: { source: "picker" } };
|
|
57
|
+
const items = [item];
|
|
58
|
+
store.setAttachments("a", items);
|
|
59
|
+
assert.equal(store.getAttachments("a"), items);
|
|
60
|
+
assert.equal(store.ensureAttachments("a"), items);
|
|
61
|
+
assert.equal(store.getAttachments("a")[0], item);
|
|
62
|
+
assert.equal(store.getAttachments("a")[0].file, file);
|
|
63
|
+
assert.equal(store.getAttachments("a")[0].blob, blob);
|
|
64
|
+
assert.equal(store.getAttachments("a")[0].metadata, item.metadata);
|
|
65
|
+
store.getAttachments("a")[0].metadata.source = "edited";
|
|
66
|
+
assert.equal(item.metadata.source, "edited");
|
|
67
|
+
const replacement = [];
|
|
68
|
+
store.setAttachments("a", replacement);
|
|
69
|
+
assert.equal(store.getAttachments("a"), replacement);
|
|
70
|
+
assert.deepEqual(items, [item]);
|
|
71
|
+
assert.equal(store.deleteAttachments("a"), true);
|
|
72
|
+
assert.equal(store.deleteAttachments("a"), false);
|
|
73
|
+
assert.equal(store.hasAttachments("a"), false);
|
|
74
|
+
assert.notEqual(store.ensureAttachments("a"), replacement);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("pending compaction deduplicates per tab and repeated completion is harmless", () => {
|
|
78
|
+
const store = createSessionComposerStore();
|
|
79
|
+
assert.equal(store.beginCompaction("a"), true);
|
|
80
|
+
assert.equal(store.beginCompaction("a"), false);
|
|
81
|
+
assert.equal(store.beginCompaction("b"), true);
|
|
82
|
+
assert.equal(store.isCompactionPending("a"), true);
|
|
83
|
+
assert.equal(store.endCompaction("a"), true);
|
|
84
|
+
assert.equal(store.endCompaction("a"), false);
|
|
85
|
+
assert.equal(store.isCompactionPending("a"), false);
|
|
86
|
+
assert.equal(store.isCompactionPending("b"), true);
|
|
87
|
+
assert.equal(store.beginCompaction("a"), true);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("clearTab removes only its state and leaves resource cleanup to the caller", (t) => {
|
|
91
|
+
const revoke = t.mock.method(URL, "revokeObjectURL", () => assert.fail("store must not revoke URLs"));
|
|
92
|
+
const store = createSessionComposerStore();
|
|
93
|
+
const item = Object.freeze({
|
|
94
|
+
file: new File(["unsent"], "draft.txt"),
|
|
95
|
+
previewUrl: "blob:owned-by-parent",
|
|
96
|
+
close: () => assert.fail("store must not close resources"),
|
|
97
|
+
});
|
|
98
|
+
const first = Object.freeze([item]);
|
|
99
|
+
const second = [{ id: "b" }];
|
|
100
|
+
for (const tabId of ["a", "b"]) {
|
|
101
|
+
store.setDraft(tabId, `${tabId} draft`);
|
|
102
|
+
store.beginCompaction(tabId);
|
|
103
|
+
}
|
|
104
|
+
store.setAttachments("a", first);
|
|
105
|
+
store.setAttachments("b", second);
|
|
106
|
+
store.clearTab("a");
|
|
107
|
+
store.clearTab("a");
|
|
108
|
+
store.clearTab("missing");
|
|
109
|
+
assert.equal(store.getDraft("a"), "");
|
|
110
|
+
assert.equal(store.hasDraft("a"), false);
|
|
111
|
+
assert.deepEqual(store.getAttachments("a"), []);
|
|
112
|
+
assert.equal(store.hasAttachments("a"), false);
|
|
113
|
+
assert.equal(store.isCompactionPending("a"), false);
|
|
114
|
+
assert.equal(store.endCompaction("a"), false);
|
|
115
|
+
assert.equal(store.getDraft("b"), "b draft");
|
|
116
|
+
assert.equal(store.getAttachments("b"), second);
|
|
117
|
+
assert.equal(store.isCompactionPending("b"), true);
|
|
118
|
+
assert.deepEqual([...store.draftEntries()], [["b", "b draft"]]);
|
|
119
|
+
assert.equal(first[0], item);
|
|
120
|
+
assert.equal(revoke.mock.callCount(), 0);
|
|
121
|
+
assert.equal(store.beginCompaction("a"), true);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("null inactive tabs do not accumulate composer state", () => {
|
|
125
|
+
const store = createSessionComposerStore();
|
|
126
|
+
for (const tabId of [null, undefined, ""]) {
|
|
127
|
+
store.setDraft(tabId, "inactive");
|
|
128
|
+
store.setAttachments(tabId, [{ id: "inactive" }]);
|
|
129
|
+
store.ensureAttachments(tabId).push({ id: "temporary" });
|
|
130
|
+
assert.equal(store.getDraft(tabId), "");
|
|
131
|
+
assert.equal(store.hasDraft(tabId), false);
|
|
132
|
+
assert.deepEqual(store.getAttachments(tabId), []);
|
|
133
|
+
assert.equal(store.hasAttachments(tabId), false);
|
|
134
|
+
assert.equal(store.beginCompaction(tabId), false);
|
|
135
|
+
store.clearTab(tabId);
|
|
136
|
+
}
|
|
137
|
+
assert.deepEqual([...store.draftEntries()], []);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("draft, attachment and compaction state are local to each store instance", () => {
|
|
141
|
+
const first = createSessionComposerStore();
|
|
142
|
+
const second = createSessionComposerStore();
|
|
143
|
+
first.setDraft("a", "first instance");
|
|
144
|
+
first.ensureAttachments("a").push({ id: "first" });
|
|
145
|
+
first.beginCompaction("a");
|
|
146
|
+
assert.equal(second.getDraft("a"), "");
|
|
147
|
+
assert.deepEqual(second.getAttachments("a"), []);
|
|
148
|
+
assert.equal(second.isCompactionPending("a"), false);
|
|
149
|
+
second.clearTab("a");
|
|
150
|
+
assert.equal(first.getDraft("a"), "first instance");
|
|
151
|
+
assert.equal(first.getAttachments("a")[0].id, "first");
|
|
152
|
+
assert.equal(first.isCompactionPending("a"), true);
|
|
153
|
+
});
|