@firstpick/pi-package-webui 0.11.1 → 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.
Files changed (309) hide show
  1. package/DEVELOPMENT.md +151 -12
  2. package/README.md +34 -6
  3. package/TECHNICAL.md +75 -8
  4. package/bin/pi-webui.mjs +5390 -11423
  5. package/index.ts +10 -3
  6. package/lib/component-update-state.mjs +2 -2
  7. package/lib/git-workflow-preference-values.mjs +122 -0
  8. package/lib/git-workflow-preferences.mjs +35 -563
  9. package/lib/package-updates.mjs +260 -0
  10. package/lib/rpc-supervisor-client.mjs +8 -4
  11. package/lib/server/app-runners.mjs +1667 -0
  12. package/lib/server/codex-fast-mode.mjs +161 -0
  13. package/lib/server/conversation-mode.mjs +218 -0
  14. package/lib/server/conversation-voice.mjs +318 -0
  15. package/lib/server/extension-ui-requests.mjs +114 -0
  16. package/lib/server/git-generation.mjs +459 -0
  17. package/lib/server/git-mutations.mjs +1827 -0
  18. package/lib/server/git-reads.mjs +608 -0
  19. package/lib/server/git-worktree-sessions.mjs +408 -0
  20. package/lib/server/helper-requests.mjs +82 -0
  21. package/lib/server/no-project-workspaces.mjs +103 -0
  22. package/lib/server/rpc-transports.mjs +394 -0
  23. package/lib/server/settings-store.mjs +544 -0
  24. package/lib/server/sse-delivery.mjs +138 -0
  25. package/lib/server/workspace-files.mjs +563 -0
  26. package/lib/update/supervisor.mjs +4 -0
  27. package/lib/webui-workspaces.mjs +2 -0
  28. package/package.json +4 -4
  29. package/public/activity-agents-controller.mjs +269 -0
  30. package/public/activity-events-controller.mjs +236 -0
  31. package/public/activity-intercom-controller.mjs +627 -0
  32. package/public/activity-queue-controller.mjs +534 -0
  33. package/public/activity-todo-view.mjs +89 -0
  34. package/public/activity-usage-controller.mjs +589 -0
  35. package/public/app.js +3761 -3886
  36. package/public/conversation-overview.css +84 -0
  37. package/public/conversation-overview.mjs +193 -0
  38. package/public/draft-conversations.mjs +135 -0
  39. package/public/focused-composer-controls.css +111 -0
  40. package/public/focused-composer-controls.mjs +143 -0
  41. package/public/focused-composer-disclosure.mjs +223 -0
  42. package/public/focused-shell-adapter.css +264 -0
  43. package/public/focused-shell-adapter.mjs +257 -0
  44. package/public/index.html +137 -80
  45. package/public/model-catalog-order.mjs +63 -0
  46. package/public/platform-event-stream.mjs +64 -0
  47. package/public/project-artifact-controller.mjs +218 -0
  48. package/public/project-file-viewer-controller.mjs +383 -0
  49. package/public/project-git-tools-controller.mjs +181 -0
  50. package/public/project-runners-controller.mjs +220 -0
  51. package/public/service-worker.js +52 -1
  52. package/public/session-attention-scheduler.mjs +182 -0
  53. package/public/session-composer-store.mjs +56 -0
  54. package/public/session-context.mjs +51 -0
  55. package/public/session-extension-requests.mjs +63 -0
  56. package/public/session-shelf-state.mjs +151 -0
  57. package/public/session-shelves.css +171 -0
  58. package/public/session-shelves.mjs +95 -0
  59. package/public/settings-navigation-controller.mjs +249 -0
  60. package/public/settings-navigation.css +61 -0
  61. package/public/settings-resource-controller.mjs +232 -0
  62. package/public/settings-workspace.css +19 -0
  63. package/public/settings-workspace.mjs +175 -0
  64. package/public/shell-actions.mjs +87 -0
  65. package/public/shell-attention-inbox.css +72 -0
  66. package/public/shell-attention-inbox.mjs +198 -0
  67. package/public/shell-context-actions.css +61 -0
  68. package/public/shell-context-actions.mjs +168 -0
  69. package/public/shell-inspector-resize.mjs +124 -0
  70. package/public/shell-navigation-resize.mjs +107 -0
  71. package/public/shell-navigation.mjs +127 -0
  72. package/public/shell-preferences.mjs +108 -0
  73. package/public/shell-surfaces.mjs +99 -0
  74. package/public/shell-tools-launcher.css +97 -0
  75. package/public/shell-tools-launcher.mjs +222 -0
  76. package/public/shell-view-host.mjs +115 -0
  77. package/public/shell-workspace.css +266 -0
  78. package/public/shell-workspace.mjs +402 -0
  79. package/public/sidebar-update-menu.css +190 -0
  80. package/public/sidebar-update-menu.mjs +512 -0
  81. package/public/styles.css +320 -169
  82. package/public/subagent-launch-slot-state.mjs +2 -2
  83. package/public/themed-selects.css +156 -0
  84. package/public/workspace-choices.mjs +104 -0
  85. package/tests/activity-agents-controller.test.mjs +652 -0
  86. package/tests/activity-events-controller.test.mjs +511 -0
  87. package/tests/app-runner-terminal-static.test.mjs +3 -3
  88. package/tests/append-system-http.test.mjs +7 -6
  89. package/tests/append-system-selector-static.test.mjs +6 -6
  90. package/tests/aur-review-widget-static.test.mjs +11 -3
  91. package/tests/boot-failure-diagnostics.test.mjs +8 -3
  92. package/tests/browser/activity-intercom-controller.spec.mjs +457 -0
  93. package/tests/browser/activity-navigation-integration.spec.mjs +45 -0
  94. package/tests/browser/activity-owner-integration.spec.mjs +201 -0
  95. package/tests/browser/activity-queue-controller.spec.mjs +405 -0
  96. package/tests/browser/activity-section-picker.spec.mjs +294 -0
  97. package/tests/browser/activity-todo-view.spec.mjs +60 -0
  98. package/tests/browser/activity-usage-controller.spec.mjs +416 -0
  99. package/tests/browser/compact-layout-integration.spec.mjs +58 -0
  100. package/tests/browser/composer-action-grid-reorder.spec.mjs +16 -0
  101. package/tests/browser/composer-disclosure.spec.mjs +389 -0
  102. package/tests/browser/composer-toolbar-polish.spec.mjs +343 -0
  103. package/tests/browser/control-deck-component-updates.spec.mjs +5 -14
  104. package/tests/browser/control-deck-side-panels.spec.mjs +27 -12
  105. package/tests/browser/control-visibility.spec.mjs +5 -0
  106. package/tests/browser/controls-layout.spec.mjs +21 -2
  107. package/tests/browser/conversation-overview.spec.mjs +251 -0
  108. package/tests/browser/draft-conversations.spec.mjs +276 -0
  109. package/tests/browser/empty-startup-fixture.mjs +135 -0
  110. package/tests/browser/empty-startup.spec.mjs +282 -0
  111. package/tests/browser/events-tree-context-menu.spec.mjs +1 -0
  112. package/tests/browser/focused-composer-more.spec.mjs +127 -0
  113. package/tests/browser/focused-model-shortcuts.spec.mjs +120 -0
  114. package/tests/browser/focused-navigation-fixture.mjs +32 -0
  115. package/tests/browser/focused-project-activity.spec.mjs +111 -0
  116. package/tests/browser/focused-shell-integration.spec.mjs +208 -0
  117. package/tests/browser/folder-picker-layout-fixture.mjs +13 -0
  118. package/tests/browser/folder-picker-layout.spec.mjs +302 -0
  119. package/tests/browser/footer-model-advanced-layout.spec.mjs +27 -14
  120. package/tests/browser/git-setup-cancel.spec.mjs +251 -0
  121. package/tests/browser/guided-git-fallback.spec.mjs +40 -21
  122. package/tests/browser/inspector-header.spec.mjs +221 -0
  123. package/tests/browser/inspector-overview-integration.spec.mjs +104 -0
  124. package/tests/browser/inspector-resize.spec.mjs +227 -0
  125. package/tests/browser/inspector-topic-overflow.spec.mjs +56 -0
  126. package/tests/browser/interaction-action-discovery.spec.mjs +332 -0
  127. package/tests/browser/interaction-attention-inbox.spec.mjs +364 -0
  128. package/tests/browser/interaction-attention-integration.spec.mjs +53 -0
  129. package/tests/browser/interaction-composer-controls.spec.mjs +267 -0
  130. package/tests/browser/interaction-composer-integration.spec.mjs +175 -0
  131. package/tests/browser/interaction-continuity.spec.mjs +50 -26
  132. package/tests/browser/interaction-model-picker.spec.mjs +86 -0
  133. package/tests/browser/last-terminal-close.spec.mjs +280 -0
  134. package/tests/browser/mobile-foundation.spec.mjs +172 -20
  135. package/tests/browser/model-catalog-groups.spec.mjs +104 -0
  136. package/tests/browser/more-row-menus.spec.mjs +258 -0
  137. package/tests/browser/new-tab-hit-target.spec.mjs +228 -0
  138. package/tests/browser/no-project-conversation.spec.mjs +113 -0
  139. package/tests/browser/optimistic-prompt.spec.mjs +3 -0
  140. package/tests/browser/overhaul-w7-accessibility.spec.mjs +227 -0
  141. package/tests/browser/overhaul-w7-capabilities.spec.mjs +298 -0
  142. package/tests/browser/overhaul-w7-companions.spec.mjs +290 -0
  143. package/tests/browser/overhaul-w7-focus-repairs.spec.mjs +210 -0
  144. package/tests/browser/overhaul-w7-layout-repairs.spec.mjs +183 -0
  145. package/tests/browser/overhaul-w7-parent-repairs.spec.mjs +97 -0
  146. package/tests/browser/overhaul-w7-pwa.spec.mjs +247 -0
  147. package/tests/browser/overhaul-w7-responsive.spec.mjs +450 -0
  148. package/tests/browser/overhaul-w7-update.spec.mjs +215 -0
  149. package/tests/browser/overhaul-w7-zoom.spec.mjs +243 -0
  150. package/tests/browser/overview-agent-ownership.spec.mjs +45 -0
  151. package/tests/browser/persistent-ui-layout.spec.mjs +3 -0
  152. package/tests/browser/project-artifact-controller.spec.mjs +324 -0
  153. package/tests/browser/project-artifact-integration.spec.mjs +103 -0
  154. package/tests/browser/project-file-buffer-integration.spec.mjs +230 -0
  155. package/tests/browser/project-git-tools-integration.spec.mjs +369 -0
  156. package/tests/browser/session-fixture.mjs +118 -0
  157. package/tests/browser/session-ownership.spec.mjs +175 -0
  158. package/tests/browser/session-shelves-integration.spec.mjs +219 -0
  159. package/tests/browser/session-shelves.spec.mjs +340 -0
  160. package/tests/browser/session-sidebar-resize.spec.mjs +138 -0
  161. package/tests/browser/session-summary.spec.mjs +17 -12
  162. package/tests/browser/settings-complete-home.spec.mjs +463 -0
  163. package/tests/browser/settings-entrypoint.spec.mjs +104 -0
  164. package/tests/browser/settings-navigation-controller.spec.mjs +383 -0
  165. package/tests/browser/settings-navigation-integration.spec.mjs +348 -0
  166. package/tests/browser/settings-resource-lifetime.spec.mjs +349 -0
  167. package/tests/browser/settings-workspace-integration.spec.mjs +132 -0
  168. package/tests/browser/shell-ownership.spec.mjs +398 -0
  169. package/tests/browser/shell-rollout-fixture.mjs +259 -0
  170. package/tests/browser/shell-rollout.spec.mjs +357 -0
  171. package/tests/browser/shell-tools-launcher.spec.mjs +260 -0
  172. package/tests/browser/shell-workspace.spec.mjs +433 -0
  173. package/tests/browser/side-panel-section-reorder.spec.mjs +3 -1
  174. package/tests/browser/sidebar-floating-tooltips.spec.mjs +37 -0
  175. package/tests/browser/sidebar-update-integration.spec.mjs +277 -0
  176. package/tests/browser/sidebar-update-menu.spec.mjs +402 -0
  177. package/tests/browser/sidebar-update-progress.spec.mjs +472 -0
  178. package/tests/browser/stream-output-isolation.spec.mjs +6 -3
  179. package/tests/browser/themed-selects.spec.mjs +297 -0
  180. package/tests/browser/thread-start-flow.spec.mjs +481 -0
  181. package/tests/browser/ui-polish-activity-controls.spec.mjs +177 -0
  182. package/tests/browser/ui-polish-features.spec.mjs +85 -0
  183. package/tests/browser/ui-polish-file-header.spec.mjs +43 -0
  184. package/tests/browser/ui-polish-local-git.spec.mjs +183 -0
  185. package/tests/browser/ui-polish-mobile-recovery.spec.mjs +36 -0
  186. package/tests/browser/ui-polish-navigation.spec.mjs +127 -0
  187. package/tests/browser/ui-polish-session-actions.spec.mjs +282 -0
  188. package/tests/browser/user-noticeable-fixes.spec.mjs +10 -3
  189. package/tests/browser/windows-drive-parent.spec.mjs +4 -2
  190. package/tests/browser/workspace-draft-selection.spec.mjs +266 -0
  191. package/tests/codex-fast-mode-static.test.mjs +16 -11
  192. package/tests/codex-speed-mode-browser-state.test.mjs +6 -3
  193. package/tests/codex-usage-source.test.mjs +4 -3
  194. package/tests/component-update-api-static.test.mjs +2 -2
  195. package/tests/composer-action-grid-reorder-static.test.mjs +3 -3
  196. package/tests/control-deck-component-updates-static.test.mjs +9 -4
  197. package/tests/control-visibility-static.test.mjs +3 -3
  198. package/tests/controls-layout-static.test.mjs +5 -5
  199. package/tests/conversation-overview-adapter.test.mjs +100 -0
  200. package/tests/document-artifact-static.test.mjs +2 -1
  201. package/tests/draft-conversations.test.mjs +205 -0
  202. package/tests/empty-startup-launcher.test.mjs +197 -0
  203. package/tests/events-tree-context-menu-static.test.mjs +22 -18
  204. package/tests/fake-pi-session-ownership.test.mjs +302 -0
  205. package/tests/fast-mode-client-static.test.mjs +1 -1
  206. package/tests/feature-decision-output-popup-static.test.mjs +2 -2
  207. package/tests/feature-system-prompt-optional-feature-static.test.mjs +1 -1
  208. package/tests/file-viewer-image-static.test.mjs +15 -11
  209. package/tests/file-viewer-search-static.test.mjs +5 -5
  210. package/tests/fixtures/fake-pi.mjs +41 -9
  211. package/tests/footer-provider-usage-visual-static.test.mjs +7 -6
  212. package/tests/foreground-transcript-catch-up-static.test.mjs +7 -4
  213. package/tests/git-footer-context-menu-static.test.mjs +1 -1
  214. package/tests/git-panel-file-preview-static.test.mjs +41 -48
  215. package/tests/git-panel-live-updates-static.test.mjs +2 -1
  216. package/tests/git-workflow-dispatch.test.mjs +66 -0
  217. package/tests/git-workflow-preferences.test.mjs +9 -1
  218. package/tests/guided-git-activation-static.test.mjs +14 -11
  219. package/tests/guided-git-fallback-ui-static.test.mjs +8 -5
  220. package/tests/guided-git-message-reliability-static.test.mjs +10 -6
  221. package/tests/guided-git-publication-backend-static.test.mjs +16 -11
  222. package/tests/http-endpoints-harness.test.mjs +21 -9
  223. package/tests/interaction-state-stability-static.test.mjs +4 -3
  224. package/tests/intercom-conversation-viewer-static.test.mjs +29 -17
  225. package/tests/intercom-tag-layout-static.test.mjs +13 -12
  226. package/tests/intercom-tag-overflow-layout.test.mjs +11 -10
  227. package/tests/main-output-loading-static.test.mjs +4 -4
  228. package/tests/middle-button-drag-scroll-static.test.mjs +3 -3
  229. package/tests/mobile-compact-layout.test.mjs +2 -2
  230. package/tests/mobile-continuity-tablet-static.test.mjs +3 -3
  231. package/tests/mobile-foundation-static.test.mjs +10 -7
  232. package/tests/mobile-phone-experience-static.test.mjs +3 -2
  233. package/tests/mobile-static.test.mjs +173 -135
  234. package/tests/model-catalog-order.test.mjs +103 -0
  235. package/tests/native-parity.test.mjs +6 -4
  236. package/tests/native-session-choice-owner.test.mjs +102 -0
  237. package/tests/native-update-lifecycle.test.mjs +10 -5
  238. package/tests/no-project-http-harness.test.mjs +328 -0
  239. package/tests/no-project-navigation.test.mjs +54 -0
  240. package/tests/no-project-workspaces.test.mjs +77 -0
  241. package/tests/open-issue-wizard-static.test.mjs +1 -1
  242. package/tests/optional-feature-migration-frontend.test.mjs +4 -4
  243. package/tests/optional-feature-prompt-impact-static.test.mjs +3 -3
  244. package/tests/package-updates.test.mjs +630 -0
  245. package/tests/persistent-ui-layout-static.test.mjs +3 -3
  246. package/tests/platform-event-stream.test.mjs +253 -0
  247. package/tests/project-file-viewer-controller.test.mjs +652 -0
  248. package/tests/project-git-tools-adapter.test.mjs +103 -0
  249. package/tests/project-git-tools-controller.test.mjs +577 -0
  250. package/tests/project-runners-controller.test.mjs +453 -0
  251. package/tests/prompt-input-vertical-scroll-static.test.mjs +3 -3
  252. package/tests/prompt-routing-input-clear-static.test.mjs +2 -2
  253. package/tests/questionnaire-dialog.test.mjs +6 -6
  254. package/tests/queue-edit-reorder-static.test.mjs +48 -51
  255. package/tests/rpc-supervisor-host.test.mjs +4 -1
  256. package/tests/safety-guard-auto-review-static.test.mjs +1 -1
  257. package/tests/sampling-native-controls-user-flow.test.mjs +7 -1
  258. package/tests/server-app-runners.test.mjs +536 -0
  259. package/tests/server-codex-fast-mode.test.mjs +346 -0
  260. package/tests/server-conversation-mode.test.mjs +386 -0
  261. package/tests/server-conversation-voice.test.mjs +426 -0
  262. package/tests/server-extension-ui-requests.test.mjs +357 -0
  263. package/tests/server-git-generation.test.mjs +558 -0
  264. package/tests/server-git-mutations.test.mjs +729 -0
  265. package/tests/server-git-reads.test.mjs +381 -0
  266. package/tests/server-git-worktree-sessions.test.mjs +502 -0
  267. package/tests/server-helper-requests.test.mjs +364 -0
  268. package/tests/server-rpc-transports.test.mjs +553 -0
  269. package/tests/server-settings-store.test.mjs +132 -0
  270. package/tests/server-shell-rollout.test.mjs +478 -0
  271. package/tests/server-sse-delivery.test.mjs +410 -0
  272. package/tests/server-workspace-files.test.mjs +546 -0
  273. package/tests/session-attention-scheduler.test.mjs +514 -0
  274. package/tests/session-composer-store.test.mjs +153 -0
  275. package/tests/session-context.test.mjs +134 -0
  276. package/tests/session-extension-requests.test.mjs +193 -0
  277. package/tests/session-ownership-adapter.test.mjs +130 -0
  278. package/tests/session-sampling-ui-static.test.mjs +10 -4
  279. package/tests/session-send-lifetime.test.mjs +140 -0
  280. package/tests/session-shelf-state.test.mjs +348 -0
  281. package/tests/session-shelves-adapter.test.mjs +79 -0
  282. package/tests/session-summary-lifetime.test.mjs +54 -0
  283. package/tests/settings-resource-controller.test.mjs +548 -0
  284. package/tests/shell-action-adapter.test.mjs +169 -0
  285. package/tests/shell-actions.test.mjs +182 -0
  286. package/tests/shell-navigation.test.mjs +348 -0
  287. package/tests/shell-preferences.test.mjs +367 -0
  288. package/tests/side-panel-section-height-removal-static.test.mjs +3 -3
  289. package/tests/stream-output-isolation-static.test.mjs +1 -1
  290. package/tests/subagent-gate-visibility.test.mjs +1 -1
  291. package/tests/subagent-launch-slot-state.test.mjs +2 -2
  292. package/tests/subagent-launch-slots-static.test.mjs +1 -1
  293. package/tests/subagent-observability-static.test.mjs +2 -2
  294. package/tests/subagent-reliability-integration.test.mjs +13 -30
  295. package/tests/syntax-highlighting-static.test.mjs +3 -3
  296. package/tests/tab-close-continuity-static.test.mjs +1 -1
  297. package/tests/tab-metadata-ordering.test.mjs +99 -0
  298. package/tests/terminal-cwd-grouping-static.test.mjs +2 -2
  299. package/tests/terminal-tab-prompt-tooltip-static.test.mjs +2 -0
  300. package/tests/terminal-tab-workspace-static.test.mjs +5 -4
  301. package/tests/terminal-tabs-resize-static.test.mjs +1 -1
  302. package/tests/theme-customizer-static.test.mjs +1 -1
  303. package/tests/thread-display-title.test.mjs +44 -0
  304. package/tests/update-api-harness.test.mjs +15 -14
  305. package/tests/update-tab-restore-harness.test.mjs +3 -0
  306. package/tests/webui-settings-locking.test.mjs +2 -2
  307. package/tests/windows-drive-cwd-picker-static.test.mjs +7 -9
  308. package/tests/workspace-choices.test.mjs +114 -0
  309. package/tests/workspace-save-load-static.test.mjs +7 -4
@@ -0,0 +1,652 @@
1
+ import assert from "node:assert/strict";
2
+ import { test } from "node:test";
3
+ import {
4
+ createProjectFileViewerController,
5
+ gitFileDiffCategory,
6
+ resolveFileViewerMode,
7
+ } from "../public/project-file-viewer-controller.mjs";
8
+
9
+ function deferred() {
10
+ let resolve;
11
+ let reject;
12
+ const promise = new Promise((accept, fail) => { resolve = accept; reject = fail; });
13
+ return { promise, resolve, reject };
14
+ }
15
+
16
+ const flush = async () => { await Promise.resolve(); await Promise.resolve(); };
17
+ const normalizePath = (value = "") => String(value || "").trim().replace(/\\/g, "/").replace(/^\.\/+/, "").replace(/^\/+/, "").replace(/\/+$/g, "");
18
+ const fileDisplayName = (path) => normalizePath(path).split("/").pop();
19
+ const fileExtensionForPath = (path) => {
20
+ const name = fileDisplayName(path);
21
+ const dot = name.lastIndexOf(".");
22
+ return dot > 0 ? name.slice(dot).toLowerCase() : "";
23
+ };
24
+ const fileLanguageForPath = (path) => [".md", ".markdown"].includes(fileExtensionForPath(path)) ? "markdown" : "text";
25
+
26
+ function fixture() {
27
+ const alive = new Map([["a", 1], ["b", 1]]);
28
+ let target = { tabId: "a", generation: 1, lifetime: 1 };
29
+ const calls = [];
30
+ const confirmations = [];
31
+ const changes = [];
32
+ const statuses = [];
33
+ const selected = [];
34
+ const refreshes = [];
35
+ const events = [];
36
+ let refresh = () => {};
37
+ const controller = createProjectFileViewerController({
38
+ api(path, options) {
39
+ const pending = deferred();
40
+ calls.push({ path, options, resolve: (data = {}) => pending.resolve({ ok: true, data }), reject: pending.reject });
41
+ return pending.promise;
42
+ },
43
+ captureTarget: () => target,
44
+ isTargetCurrent: (captured) => captured.tabId === target.tabId && captured.generation === target.generation
45
+ && captured.lifetime === alive.get(captured.tabId),
46
+ isTargetAlive: (captured) => captured.lifetime === alive.get(captured.tabId),
47
+ confirmDiscardEdits(options) {
48
+ const pending = deferred();
49
+ confirmations.push({ options, ...pending });
50
+ return pending.promise;
51
+ },
52
+ normalizePath,
53
+ fileApiPath: (base, path) => `${base}?${new URLSearchParams({ path: normalizePath(path) })}`,
54
+ fileDisplayName,
55
+ fileExtensionForPath,
56
+ fileLanguageForPath,
57
+ imageDataUrl: (data) => ["image/png", "image/jpeg", "image/gif", "image/webp", "image/avif"].includes(data.mimeType) && data.data
58
+ ? `data:${data.mimeType};base64,${data.data}` : "",
59
+ remapPath(path, source, destination) {
60
+ if (path === source) return destination;
61
+ return path.startsWith(`${source}/`) ? `${destination}/${path.slice(source.length + 1)}` : path;
62
+ },
63
+ onChange: (reason) => changes.push(reason),
64
+ onStatus: (...args) => statuses.push(args),
65
+ onSelectPath: (path) => selected.push(path),
66
+ onRefreshDirectory: (path, captured) => { refreshes.push({ path, target: captured }); return refresh(); },
67
+ addEvent: (...args) => events.push(args),
68
+ });
69
+ controller.activate("a");
70
+ return {
71
+ c: controller, calls, confirmations, changes, statuses, selected, refreshes, events, alive,
72
+ target: () => target,
73
+ select(tabId) {
74
+ target = { tabId, generation: target.generation + 1, lifetime: alive.get(tabId) };
75
+ controller.activate(tabId);
76
+ },
77
+ replace(tabId = target.tabId) {
78
+ alive.set(tabId, (alive.get(tabId) || 0) + 1);
79
+ if (target.tabId === tabId) target = { ...target, generation: target.generation + 1, lifetime: alive.get(tabId) };
80
+ },
81
+ refreshWith(callback) { refresh = callback; },
82
+ async seed(path = "src/file.txt", data = {}) {
83
+ const result = controller.open(path);
84
+ await flush();
85
+ calls.at(-1).resolve({ path, name: fileDisplayName(path), content: "original", size: 8, mtimeMs: 10, ...data });
86
+ assert.equal((await result).success, true);
87
+ return controller.viewer;
88
+ },
89
+ };
90
+ }
91
+
92
+ test("Git category and viewer mode helpers preserve the app rules", () => {
93
+ for (const [input, expected] of [["staged", "staged"], [" changes ", "unstaged"], ["conflicted", "conflicted"], ["untracked", "untracked"], ["unstaged", ""], ["unknown", ""], [null, ""]]) {
94
+ assert.equal(gitFileDiffCategory(input), expected);
95
+ }
96
+ assert.equal(resolveFileViewerMode(null), "source");
97
+ assert.equal(resolveFileViewerMode({ mode: "preview", language: "markdown" }), "preview");
98
+ assert.equal(resolveFileViewerMode({ mode: "preview", language: "text" }), "source");
99
+ assert.equal(resolveFileViewerMode({ mode: "changes" }), "source");
100
+ assert.equal(resolveFileViewerMode({ mode: "changes", kind: "image" }), "image");
101
+ assert.equal(resolveFileViewerMode({ mode: "changes", kind: "image", gitChanges: {} }), "changes");
102
+ assert.equal(resolveFileViewerMode({ mode: "source", kind: "image" }), "image");
103
+ assert.equal(resolveFileViewerMode({ sourceAvailable: false, gitChanges: {} }), "changes");
104
+ assert.equal(resolveFileViewerMode({ mode: "preview", sourceAvailable: false, language: "markdown" }), "source");
105
+ });
106
+
107
+ test("file open sends normalized paths and preserves the text viewer shape", async () => {
108
+ const f = fixture();
109
+ const pending = f.c.open("./docs\\read me.md");
110
+ await flush();
111
+ assert.equal(f.calls[0].path, "/api/files/content?path=docs%2Fread+me.md");
112
+ assert.deepEqual(f.calls[0].options, { tabId: "a" });
113
+ assert.deepEqual(f.selected, ["docs/read me.md"]);
114
+ f.calls[0].resolve({ path: "docs/read me.md", name: "read me.md", content: "# Hi", size: "4", mtimeMs: "20", extension: ".md", language: "markdown" });
115
+ const result = await pending;
116
+ assert.deepEqual(result, { success: true, viewer: f.c.viewer });
117
+ assert.deepEqual(f.c.viewer, {
118
+ path: "docs/read me.md", name: "read me.md", content: "# Hi", size: 4, mtimeMs: 20,
119
+ extension: ".md", language: "markdown", kind: "text", mimeType: "", imageUrl: "", mode: "preview",
120
+ dirty: false, readOnly: false, sourceAvailable: true, gitChanges: null,
121
+ });
122
+ assert.deepEqual(f.c.tabIds, ["a"]);
123
+ assert.deepEqual(f.statuses.at(-1), ["Opened in WebUI.", "success"]);
124
+ assert.deepEqual(f.changes, ["activate", "open"]);
125
+ });
126
+
127
+ test("content, mode, selection, continuity and cached buffers retain identity per tab", async () => {
128
+ const f = fixture();
129
+ const first = await f.seed("a.md", { language: "markdown" });
130
+ const selection = { path: "a.md", text: "original", start: 0, end: 8 };
131
+ const continuity = { path: "a.md", content: "original", scrollTop: 44, selectionStart: 2, selectionEnd: 4, focused: true };
132
+ f.c.setSelection(selection);
133
+ f.c.cache("a", continuity);
134
+ assert.equal(f.c.viewer, first);
135
+ assert.equal(first.editorContinuity, continuity);
136
+ f.c.setMode("preview");
137
+ assert.equal(f.c.selection, selection);
138
+ f.select("b");
139
+ assert.equal(f.c.viewer, null);
140
+ assert.equal(f.c.selection, null);
141
+ await f.seed("b.txt");
142
+ f.select("a");
143
+ assert.equal(f.c.viewer, first);
144
+ assert.equal(f.c.selection, selection);
145
+ assert.equal(f.c.viewer.editorContinuity, continuity);
146
+ f.c.setContent("é🙂");
147
+ assert.equal(first.content, "é🙂");
148
+ assert.equal(first.size, 6);
149
+ assert.equal(first.dirty, true);
150
+ assert.equal(f.changes.at(-1), "content");
151
+ f.c.setDirty(false);
152
+ assert.equal(first.dirty, false);
153
+ const nextContinuity = { ...continuity, content: "é🙂" };
154
+ f.c.setEditorContinuity(nextContinuity);
155
+ assert.equal(first.editorContinuity, nextContinuity);
156
+ assert.equal(f.changes.at(-1), "continuity");
157
+ f.c.cache("b", { path: "wrong-tab" });
158
+ assert.equal(first.editorContinuity, nextContinuity);
159
+ f.select("b");
160
+ assert.equal(f.c.viewer.path, "b.txt");
161
+ assert.deepEqual(f.c.tabIds, ["a", "b"]);
162
+ f.c.tabIds.length = 0;
163
+ assert.deepEqual(f.c.tabIds, ["a", "b"]);
164
+ });
165
+
166
+ test("reset clears presentation and pending opens while close discards only the active cache", async () => {
167
+ const f = fixture();
168
+ const first = await f.seed();
169
+ const selection = { text: "first" };
170
+ f.c.setSelection(selection);
171
+ const pending = f.c.open("next.txt");
172
+ await flush();
173
+ f.c.resetActive();
174
+ assert.equal(f.c.viewer, null);
175
+ assert.equal(f.c.selection, null);
176
+ assert.deepEqual(f.c.tabIds, ["a"]);
177
+ f.calls.at(-1).resolve({ content: "stale" });
178
+ assert.deepEqual(await pending, { success: false, reason: "stale" });
179
+ f.c.activate("a");
180
+ assert.equal(f.c.viewer, first);
181
+ assert.equal(f.c.selection, selection);
182
+ f.select("b");
183
+ await f.seed("b.txt");
184
+ f.c.close();
185
+ assert.deepEqual(f.c.tabIds, ["a"]);
186
+ f.c.activate("b");
187
+ assert.equal(f.c.viewer, null);
188
+ f.select("a");
189
+ assert.equal(f.c.viewer, first);
190
+ });
191
+
192
+ test("dirty file and report declines retain exact buffer, selection and status", async () => {
193
+ for (const report of [false, true]) {
194
+ const f = fixture();
195
+ const buffer = await f.seed();
196
+ const selection = { text: "kept" };
197
+ f.c.setSelection(selection);
198
+ f.c.setContent("unsaved");
199
+ const count = f.calls.length;
200
+ const statusCount = f.statuses.length;
201
+ const pending = report ? f.c.openReport("reports/review.md", "/repo") : f.c.open("new.txt");
202
+ assert.deepEqual(f.confirmations[0].options, { what: "unsaved file edits", target: "src/file.txt" });
203
+ f.confirmations[0].resolve(false);
204
+ assert.deepEqual(await pending, { success: false, reason: "declined" });
205
+ assert.equal(f.calls.length, count);
206
+ assert.equal(f.statuses.length, statusCount);
207
+ assert.equal(f.c.viewer, buffer);
208
+ assert.equal(f.c.selection, selection);
209
+ }
210
+ });
211
+
212
+ test("accepting a captured discard prompt cannot open after replacement, navigation or retirement", async () => {
213
+ for (const operation of ["file", "report"]) {
214
+ for (const invalidate of ["replace", "navigate", "retire", "edit", "close"]) {
215
+ const f = fixture();
216
+ await f.seed();
217
+ f.c.setContent("unsaved");
218
+ const pending = operation === "file" ? f.c.open("new.txt") : f.c.openReport("report.md", "/repo");
219
+ if (invalidate === "replace") f.replace();
220
+ else if (invalidate === "navigate") f.select("b");
221
+ else if (invalidate === "retire") { f.c.forgetTab("a"); f.c.activate("a"); }
222
+ else if (invalidate === "edit") f.c.setContent("newer unsaved");
223
+ else f.c.close();
224
+ f.confirmations[0].resolve(true);
225
+ assert.deepEqual(await pending, { success: false, reason: "stale" }, `${operation}/${invalidate}`);
226
+ assert.equal(f.calls.length, 1);
227
+ }
228
+ }
229
+ });
230
+
231
+ test("a newer open supersedes an older prompt even when confirmations resolve in reverse", async () => {
232
+ const f = fixture();
233
+ await f.seed();
234
+ f.c.setContent("dirty");
235
+ const first = f.c.open("first.txt");
236
+ const second = f.c.openReport("second.md", "/repo");
237
+ f.confirmations[1].resolve(true);
238
+ await flush();
239
+ f.calls[1].resolve({ path: "second.md", content: "report" });
240
+ assert.equal((await second).success, true);
241
+ f.confirmations[0].resolve(true);
242
+ assert.deepEqual(await first, { success: false, reason: "stale" });
243
+ assert.equal(f.calls.length, 2);
244
+ assert.equal(f.c.viewer.path, "second.md");
245
+ });
246
+
247
+ test("reverse-order file and report responses cannot replace a newer open", async () => {
248
+ for (const oldReport of [false, true]) {
249
+ const f = fixture();
250
+ const first = oldReport ? f.c.openReport("old.md", "/repo") : f.c.open("old.txt");
251
+ await flush();
252
+ const second = oldReport ? f.c.open("new.txt") : f.c.openReport("new.md", "/repo");
253
+ await flush();
254
+ f.calls[1].resolve({ content: "new" });
255
+ assert.equal((await second).success, true);
256
+ const current = f.c.viewer;
257
+ const changes = f.changes.length;
258
+ f.calls[0].resolve({ content: "old" });
259
+ assert.deepEqual(await first, { success: false, reason: "stale" });
260
+ assert.equal(f.c.viewer, current);
261
+ assert.equal(f.c.viewer.content, "new");
262
+ assert.equal(f.changes.length, changes);
263
+ }
264
+ });
265
+
266
+ test("file and report reads reject late success after target replacement or new editor changes", async () => {
267
+ for (const report of [false, true]) {
268
+ for (const invalidate of ["session", "navigate", "edit", "move"]) {
269
+ const f = fixture();
270
+ const original = await f.seed();
271
+ const pending = report ? f.c.openReport("report.md", "/repo") : f.c.open("next.txt");
272
+ await flush();
273
+ if (invalidate === "session") f.replace();
274
+ else if (invalidate === "navigate") { f.select("b"); f.select("a"); }
275
+ else if (invalidate === "edit") f.c.setContent("new edits while opening");
276
+ else f.c.remapPath("src", "moved");
277
+ const statuses = f.statuses.length;
278
+ const changes = f.changes.length;
279
+ f.calls[1].resolve({ content: "stale response" });
280
+ assert.deepEqual(await pending, { success: false, reason: "stale" }, `${report}/${invalidate}`);
281
+ assert.equal(f.c.viewer, original);
282
+ assert.equal(f.statuses.length, statuses);
283
+ assert.equal(f.changes.length, changes);
284
+ }
285
+ }
286
+ });
287
+
288
+ test("report requests preserve root/path encoding, read-only data and failure wording", async () => {
289
+ const f = fixture();
290
+ const pending = f.c.openReport("./reports/a b.md", "/repo with spaces");
291
+ await flush();
292
+ assert.equal(f.calls[0].path, "/api/aur-review/report-content?path=reports%2Fa+b.md&repoRoot=%2Frepo+with+spaces");
293
+ assert.deepEqual(f.calls[0].options, { tabId: "a" });
294
+ f.calls[0].resolve({ path: "reports/a b.md", name: "a b.md", content: "# Review", size: 8, mtimeMs: 15, extension: ".md", language: "markdown" });
295
+ assert.equal((await pending).success, true);
296
+ assert.deepEqual(f.c.viewer, { path: "reports/a b.md", name: "a b.md", content: "# Review", size: 8, mtimeMs: 15, extension: ".md", language: "markdown", mode: "preview", dirty: false, readOnly: true });
297
+ assert.deepEqual(f.selected, []);
298
+ f.c.setContent("should not edit");
299
+ f.c.setDirty(true);
300
+ assert.equal(f.c.viewer.content, "# Review");
301
+ assert.equal(f.c.viewer.dirty, false);
302
+ assert.deepEqual(await f.c.save("no"), { success: false, reason: "read-only" });
303
+ const failure = f.c.openReport("missing.md", "/repo");
304
+ await flush();
305
+ const error = new Error("Report path not found");
306
+ f.calls[1].reject(error);
307
+ assert.deepEqual(await failure, { success: false, reason: "error", error });
308
+ assert.equal(f.c.viewer.content, "# Review");
309
+ assert.deepEqual(f.events, [["review report open failed: Report path not found", "error"]]);
310
+ });
311
+
312
+ test("all panel categories send the actual Git diff route and preserve snapshot metadata", async () => {
313
+ for (const [panel, category] of [["changes", "unstaged"], ["staged", "staged"], ["conflicted", "conflicted"], ["untracked", "untracked"]]) {
314
+ const f = fixture();
315
+ const pending = f.c.open("src/local.txt", { gitCategory: panel, gitPath: "repo/path space.txt" });
316
+ await flush();
317
+ assert.equal(f.calls[0].path, `/api/git-file-diff?path=repo%2Fpath+space.txt&category=${category}`);
318
+ assert.deepEqual(f.calls[0].options, { tabId: "a" });
319
+ f.calls[1].resolve({ content: "file" });
320
+ const result = await pending;
321
+ const buffer = result.viewer;
322
+ assert.equal(buffer.gitChanges.loading, true);
323
+ assert.equal(buffer.gitChanges.requestSerial, 1);
324
+ assert.equal(buffer.mode, "changes");
325
+ const selection = { text: "clear on changes" };
326
+ f.c.setMode("source");
327
+ f.c.setSelection(selection);
328
+ f.c.setMode("changes");
329
+ assert.equal(f.c.selection, null);
330
+ f.calls[0].resolve({ path: "canonical.txt", label: "Working tree", command: "git diff", diff: 42, truncated: true, capBytes: "500", error: "untracked content missing", entries: [{ path: "canonical.txt", contentMissing: true }] });
331
+ await flush();
332
+ assert.equal(f.c.viewer, buffer);
333
+ assert.deepEqual(buffer.gitChanges, { category, path: "canonical.txt", label: "Working tree", command: "git diff", diff: "42", truncated: true, capBytes: 500, error: "", contentError: "untracked content missing", entries: [{ path: "canonical.txt", contentMissing: true }] });
334
+ assert.equal(f.changes.at(-1), "diff");
335
+ }
336
+ });
337
+
338
+ test("late Git diff updates its exact live background buffer without repainting the foreground", async () => {
339
+ const f = fixture();
340
+ const pending = f.c.open("first.txt", { gitCategory: "staged" });
341
+ await flush();
342
+ f.calls[1].resolve({ content: "first" });
343
+ const buffer = (await pending).viewer;
344
+ f.select("b");
345
+ const second = await f.seed("second.txt");
346
+ const changes = f.changes.length;
347
+ f.calls[0].resolve({ diff: "first diff", label: "Staged" });
348
+ await flush();
349
+ assert.equal(buffer.gitChanges.diff, "first diff");
350
+ assert.equal(f.c.viewer, second);
351
+ assert.equal(f.changes.length, changes);
352
+ f.select("a");
353
+ assert.equal(f.c.viewer, buffer);
354
+ assert.equal(f.c.viewer.gitChanges.diff, "first diff");
355
+ });
356
+
357
+ test("same-path replacement, session replacement and retired/reused IDs reject old diffs", async () => {
358
+ for (const invalidate of ["buffer", "session", "retire", "close"]) {
359
+ const f = fixture();
360
+ const pending = f.c.open("same.txt", { gitCategory: "staged" });
361
+ await flush();
362
+ f.calls[1].resolve({ content: "old" });
363
+ const old = (await pending).viewer;
364
+ if (invalidate === "session") f.replace();
365
+ else if (invalidate === "retire") { f.c.forgetTab("a"); f.c.activate("a"); }
366
+ else if (invalidate === "close") f.c.close();
367
+ if (invalidate !== "session") await f.seed("same.txt", { content: "replacement" });
368
+ f.calls[0].resolve({ diff: "stale diff" });
369
+ await flush();
370
+ assert.equal(old.gitChanges.loading, true, invalidate);
371
+ assert.notEqual(f.c.viewer?.gitChanges?.diff, "stale diff");
372
+ }
373
+ });
374
+
375
+ test("a live diff repaints its original buffer after navigating away and back", async () => {
376
+ const f = fixture();
377
+ const pending = f.c.open("first.txt", { gitCategory: "staged" });
378
+ await flush();
379
+ f.calls[1].resolve({ content: "first" });
380
+ const buffer = (await pending).viewer;
381
+ f.select("b");
382
+ f.select("a");
383
+ assert.equal(f.c.viewer, buffer);
384
+ f.calls[0].resolve({ diff: "live diff" });
385
+ await flush();
386
+ assert.equal(f.c.viewer.gitChanges.diff, "live diff");
387
+ assert.equal(f.changes.at(-1), "diff");
388
+ });
389
+
390
+ test("diff failures are visible snapshots and do not reject a successful source open", async () => {
391
+ const f = fixture();
392
+ const pending = f.c.open("file.txt", { gitCategory: "changes" });
393
+ await flush();
394
+ f.calls[1].resolve({ content: "file" });
395
+ assert.equal((await pending).success, true);
396
+ f.calls[0].reject(new Error("Git unavailable"));
397
+ await flush();
398
+ assert.equal(f.c.viewer.gitChanges.error, "Git unavailable");
399
+ assert.deepEqual(f.events, []);
400
+ });
401
+
402
+ test("deleted-source Git fallback remains read-only and cannot beat a newer open", async () => {
403
+ const f = fixture();
404
+ const pending = f.c.open("gone.md", { gitCategory: "changes" });
405
+ await flush();
406
+ f.calls[1].reject(new Error("Path not found"));
407
+ await flush();
408
+ f.calls[0].resolve({ diff: "-deleted", label: "Working tree" });
409
+ const result = await pending;
410
+ assert.equal(result.success, true);
411
+ assert.equal(result.viewer.sourceAvailable, false);
412
+ assert.equal(result.viewer.readOnly, true);
413
+ assert.equal(result.viewer.content, "");
414
+ assert.equal(result.viewer.extension, ".md");
415
+ assert.equal(result.viewer.language, "markdown");
416
+ f.c.setMode("source");
417
+ assert.equal(f.c.viewer.mode, "changes");
418
+ assert.deepEqual(f.statuses.at(-1), ["File content unavailable; showing Git changes (read-only).", "warn"]);
419
+ const old = f.c.open("another.txt", { gitCategory: "staged" });
420
+ await flush();
421
+ f.calls[3].reject(new Error("Deleted"));
422
+ await flush();
423
+ await f.seed("current.txt");
424
+ f.calls[2].resolve({ diff: "too late" });
425
+ assert.deepEqual(await old, { success: false, reason: "stale" });
426
+ assert.equal(f.c.viewer.path, "current.txt");
427
+ });
428
+
429
+ test("file and diff failures preserve the previous buffer and emit the original open error", async () => {
430
+ const f = fixture();
431
+ const previous = await f.seed();
432
+ const pending = f.c.open("missing.txt", { gitCategory: "staged" });
433
+ await flush();
434
+ const error = new Error("Missing source");
435
+ f.calls[2].reject(error);
436
+ f.calls[1].reject(new Error("Git also missing"));
437
+ assert.deepEqual(await pending, { success: false, reason: "error", error });
438
+ assert.equal(f.c.viewer, previous);
439
+ assert.deepEqual(f.events, [["file open failed: Missing source", "error"]]);
440
+ });
441
+
442
+ test("images are read-only, retain their URL/mime metadata and reject unsupported data", async () => {
443
+ const f = fixture();
444
+ await f.seed("photo.png", { kind: "image", mimeType: "image/png", data: "aW1hZ2U=", content: "ignored" });
445
+ const image = f.c.viewer;
446
+ assert.equal(image.content, "");
447
+ assert.equal(image.imageUrl, "data:image/png;base64,aW1hZ2U=");
448
+ assert.equal(image.mimeType, "image/png");
449
+ assert.equal(image.language, "image");
450
+ assert.equal(image.mode, "image");
451
+ assert.equal(image.readOnly, true);
452
+ assert.equal(image.sourceAvailable, false);
453
+ f.c.setMode("source");
454
+ f.c.setContent("no");
455
+ assert.equal(image.mode, "image");
456
+ assert.equal(image.content, "");
457
+ const invalid = f.c.open("unsafe.svg");
458
+ await flush();
459
+ f.calls.at(-1).resolve({ kind: "image", mimeType: "image/svg+xml", data: "unsafe" });
460
+ const result = await invalid;
461
+ assert.equal(result.reason, "error");
462
+ assert.equal(result.error.message, "Image preview data is invalid or unsupported");
463
+ assert.equal(f.c.viewer, image);
464
+ });
465
+
466
+ test("save sends submitted content and mtime, refreshes with the saved file path and preserves identity", async () => {
467
+ const f = fixture();
468
+ const buffer = await f.seed();
469
+ f.c.setContent("submitted");
470
+ const captured = f.target();
471
+ const pending = f.c.save("submitted");
472
+ assert.equal(f.calls[1].path, "/api/files/content");
473
+ assert.deepEqual(f.calls[1].options, { method: "POST", body: { path: "src/file.txt", content: "submitted", mtimeMs: 10 }, tabId: "a" });
474
+ f.calls[1].resolve({ path: "src/file.txt", name: "file.txt", size: 9, mtimeMs: 55, extension: ".txt", language: "text" });
475
+ assert.deepEqual(await pending, { success: true, viewer: buffer });
476
+ assert.equal(f.c.viewer, buffer);
477
+ assert.equal(buffer.mtimeMs, 55);
478
+ assert.equal(buffer.dirty, false);
479
+ assert.deepEqual(f.refreshes, [{ path: "src/file.txt", target: captured }]);
480
+ assert.deepEqual(f.statuses.at(-1), ["Saved.", "success"]);
481
+ f.c.resetActive();
482
+ f.c.activate("a");
483
+ assert.equal(f.c.viewer, buffer);
484
+ });
485
+
486
+ test("save retains edits typed during the POST while advancing saved mtime for the next save", async () => {
487
+ const f = fixture();
488
+ const buffer = await f.seed();
489
+ f.c.setContent("submitted");
490
+ const pending = f.c.save("submitted");
491
+ f.c.setContent("newer é🙂");
492
+ f.calls[1].resolve({ mtimeMs: 55, size: 9 });
493
+ assert.equal((await pending).success, true);
494
+ assert.equal(buffer.content, "newer é🙂");
495
+ assert.equal(buffer.size, new Blob(["newer é🙂"]).size);
496
+ assert.equal(buffer.mtimeMs, 55);
497
+ assert.equal(buffer.dirty, true);
498
+ assert.deepEqual(f.statuses.at(-1), ["Unsaved file edits.", "warn"]);
499
+ const next = f.c.save(buffer.content);
500
+ assert.equal(f.calls[2].options.body.mtimeMs, 55);
501
+ f.calls[2].resolve({ mtimeMs: 66, size: buffer.size });
502
+ assert.equal((await next).success, true);
503
+ assert.equal(buffer.dirty, false);
504
+ });
505
+
506
+ test("save uses explicit submitted text when the editor value has not been mirrored yet", async () => {
507
+ const f = fixture();
508
+ await f.seed();
509
+ const pending = f.c.save("unmirrored editor value");
510
+ f.calls[1].resolve({ size: 22, mtimeMs: 40 });
511
+ assert.equal((await pending).success, true);
512
+ assert.equal(f.c.viewer.content, "unmirrored editor value");
513
+ assert.equal(f.c.viewer.dirty, false);
514
+ });
515
+
516
+ test("save conflicts preserve edits and metadata without refreshing or clearing controls", async () => {
517
+ const f = fixture();
518
+ const buffer = await f.seed();
519
+ f.c.setContent("keep");
520
+ const pending = f.c.save("keep");
521
+ const error = Object.assign(new Error("File changed on disk after it was opened. Reopen it before saving."), { statusCode: 409 });
522
+ f.calls[1].reject(error);
523
+ assert.deepEqual(await pending, { success: false, reason: "error", error });
524
+ assert.equal(f.c.viewer, buffer);
525
+ assert.equal(buffer.content, "keep");
526
+ assert.equal(buffer.dirty, true);
527
+ assert.equal(buffer.mtimeMs, 10);
528
+ assert.deepEqual(f.refreshes, []);
529
+ assert.deepEqual(f.statuses.at(-1), [error.message, "error"]);
530
+ });
531
+
532
+ test("late save success or failure cannot affect a replacement buffer, session or foreground", async () => {
533
+ for (const invalidate of ["buffer", "session", "navigate", "retire", "reset", "close"]) {
534
+ for (const fail of [false, true]) {
535
+ const f = fixture();
536
+ const old = await f.seed();
537
+ const pending = f.c.save("submitted");
538
+ if (invalidate === "session") f.replace();
539
+ else if (invalidate === "navigate") { f.select("b"); await f.seed("b.txt"); }
540
+ else if (invalidate === "retire") { f.c.forgetTab("a"); f.c.activate("a"); await f.seed(); }
541
+ else if (invalidate === "reset") f.c.resetActive();
542
+ else if (invalidate === "close") f.c.close();
543
+ else await f.seed();
544
+ const current = f.c.viewer;
545
+ const changes = f.changes.length;
546
+ const statuses = f.statuses.length;
547
+ if (fail) f.calls[1].reject(new Error("stale save conflict"));
548
+ else f.calls[1].resolve({ mtimeMs: 999, content: "submitted" });
549
+ assert.deepEqual(await pending, { success: false, reason: "stale" }, `${invalidate}/${fail}`);
550
+ assert.equal(f.c.viewer, current);
551
+ assert.equal(old.mtimeMs, 10);
552
+ assert.equal(old.content, "original");
553
+ assert.equal(f.changes.length, changes);
554
+ assert.equal(f.statuses.length, statuses);
555
+ assert.deepEqual(f.refreshes, []);
556
+ }
557
+ }
558
+ });
559
+
560
+ test("reverse-order saves cannot regress metadata or clear later edits", async () => {
561
+ const f = fixture();
562
+ await f.seed();
563
+ f.c.setContent("first");
564
+ const first = f.c.save("first");
565
+ f.c.setContent("second");
566
+ const second = f.c.save("second");
567
+ f.calls[2].resolve({ size: 6, mtimeMs: 30 });
568
+ assert.equal((await second).success, true);
569
+ f.calls[1].resolve({ size: 5, mtimeMs: 20 });
570
+ assert.deepEqual(await first, { success: false, reason: "stale" });
571
+ assert.equal(f.c.viewer.content, "second");
572
+ assert.equal(f.c.viewer.mtimeMs, 30);
573
+ assert.equal(f.c.viewer.dirty, false);
574
+ assert.equal(f.refreshes.length, 1);
575
+ });
576
+
577
+ test("save refresh completion rechecks foreground ownership before reporting success", async () => {
578
+ const f = fixture();
579
+ const refresh = deferred();
580
+ f.refreshWith(() => refresh.promise);
581
+ const buffer = await f.seed();
582
+ const pending = f.c.save("saved");
583
+ f.calls[1].resolve({ size: 5, mtimeMs: 20 });
584
+ await flush();
585
+ assert.equal(buffer.mtimeMs, 20);
586
+ f.select("b");
587
+ const statuses = f.statuses.length;
588
+ refresh.resolve();
589
+ assert.deepEqual(await pending, { success: false, reason: "stale" });
590
+ assert.equal(f.statuses.length, statuses);
591
+ });
592
+
593
+ test("move preserves buffer content, remaps selection and invalidates the old diff and save", async () => {
594
+ const f = fixture();
595
+ const pending = f.c.open("dir/file.md", { gitCategory: "staged" });
596
+ await flush();
597
+ f.calls[1].resolve({ content: "original", language: "markdown", mtimeMs: 10 });
598
+ const buffer = (await pending).viewer;
599
+ f.c.setContent("edited");
600
+ f.c.setSelection({ text: "edited", path: "dir/file.md", fileName: "file.md", start: 0 });
601
+ const saving = f.c.save("edited");
602
+ f.c.remapPath("dir", "new-dir");
603
+ assert.equal(f.c.viewer, buffer);
604
+ assert.equal(buffer.path, "new-dir/file.md");
605
+ assert.equal(buffer.content, "edited");
606
+ assert.equal(buffer.dirty, true);
607
+ assert.equal(buffer.gitChanges, null);
608
+ assert.equal(buffer.mode, "source");
609
+ assert.deepEqual(f.c.selection, { text: "edited", path: "new-dir/file.md", fileName: "file.md", start: 0 });
610
+ assert.equal(f.changes.at(-1), "move");
611
+ f.calls[0].resolve({ diff: "obsolete diff" });
612
+ f.calls[2].resolve({ path: "dir/file.md", mtimeMs: 99 });
613
+ assert.deepEqual(await saving, { success: false, reason: "stale" });
614
+ assert.equal(buffer.gitChanges, null);
615
+ assert.equal(buffer.path, "new-dir/file.md");
616
+ assert.equal(buffer.mtimeMs, 10);
617
+ f.c.setMode("preview");
618
+ f.c.remapPath("new-dir/file.md", "new-dir/renamed.markdown");
619
+ assert.equal(buffer.mode, "preview");
620
+ assert.equal(buffer.extension, ".markdown");
621
+ assert.equal(f.c.selection.fileName, "renamed.markdown");
622
+ f.c.remapPath("new-dir/renamed.markdown", "file.txt");
623
+ assert.equal(buffer.mode, "source");
624
+ assert.equal(buffer.language, "text");
625
+ });
626
+
627
+ test("missing inputs and mismatched targets never issue requests", async () => {
628
+ const f = fixture();
629
+ assert.deepEqual(await f.c.open(""), { success: false, reason: "path" });
630
+ assert.deepEqual(await f.c.openReport("report.md", ""), { success: false, reason: "path" });
631
+ assert.deepEqual(await f.c.openReport("", "/repo"), { success: false, reason: "path" });
632
+ assert.deepEqual(await f.c.save(""), { success: false, reason: "empty" });
633
+ f.c.activate("b");
634
+ assert.deepEqual(await f.c.open("file.txt"), { success: false, reason: "target" });
635
+ assert.deepEqual(await f.c.openReport("report.md", "/repo"), { success: false, reason: "target" });
636
+ assert.equal(f.calls.length, 0);
637
+ });
638
+
639
+ test("late open and report errors after navigation cannot emit foreground status or events", async () => {
640
+ for (const report of [false, true]) {
641
+ const f = fixture();
642
+ const pending = report ? f.c.openReport("report.md", "/repo") : f.c.open("file.txt");
643
+ await flush();
644
+ f.select("b");
645
+ const statuses = f.statuses.length;
646
+ f.calls[0].reject(new Error("old target failure"));
647
+ assert.deepEqual(await pending, { success: false, reason: "stale" });
648
+ assert.equal(f.statuses.length, statuses);
649
+ assert.deepEqual(f.events, []);
650
+ assert.equal(f.c.viewer, null);
651
+ }
652
+ });