@bevel-software/platform-core-frontend 0.1.0

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 (203) hide show
  1. package/LICENSE +202 -0
  2. package/package.json +70 -0
  3. package/src/core/CoreAppShell.tsx +444 -0
  4. package/src/core/__tests__/ShellRoutes.test.tsx +95 -0
  5. package/src/core/events.ts +29 -0
  6. package/src/core/registry.ts +336 -0
  7. package/src/css-side-effect-imports.d.ts +7 -0
  8. package/src/html-extensions.ts +12 -0
  9. package/src/index.css +49 -0
  10. package/src/index.ts +23 -0
  11. package/src/lib/api.ts +57 -0
  12. package/src/lib/utils.ts +138 -0
  13. package/src/modules/access/__tests__/ManageAccessDialog.test.tsx +130 -0
  14. package/src/modules/access/__tests__/useFileAccess.test.tsx +253 -0
  15. package/src/modules/access/api.ts +205 -0
  16. package/src/modules/access/components/AccessRestrictedBanner.tsx +31 -0
  17. package/src/modules/access/components/ManageAccessDialog.tsx +1232 -0
  18. package/src/modules/access/components/NodeOwnersBanner.tsx +30 -0
  19. package/src/modules/access/hooks/useFileAccess.ts +151 -0
  20. package/src/modules/admin/components/AdminRolesPage.tsx +943 -0
  21. package/src/modules/admin/components/RolesCorruptedBanner.tsx +74 -0
  22. package/src/modules/admin/services/admin.api.ts +92 -0
  23. package/src/modules/admin/services/roles.api.ts +136 -0
  24. package/src/modules/admin/state/admin.context.tsx +223 -0
  25. package/src/modules/auth/components/LoginScreen.tsx +135 -0
  26. package/src/modules/auth/hooks/useAuthState.ts +59 -0
  27. package/src/modules/auth/services/auth.api.ts +24 -0
  28. package/src/modules/auth/services/microsoft-oauth.ts +57 -0
  29. package/src/modules/auth/state/auth.context.ts +18 -0
  30. package/src/modules/git/__tests__/BranchSwitcher.test.tsx +347 -0
  31. package/src/modules/git/__tests__/FileHistoryPanel.test.tsx +201 -0
  32. package/src/modules/git/__tests__/ProtectedBranchBanner.test.tsx +82 -0
  33. package/src/modules/git/__tests__/PullNeededBanner.test.tsx +441 -0
  34. package/src/modules/git/__tests__/git.api.test.ts +113 -0
  35. package/src/modules/git/__tests__/pr.api.test.ts +44 -0
  36. package/src/modules/git/__tests__/useAutoPullUpdates.test.tsx +212 -0
  37. package/src/modules/git/components/BranchSwitcher.tsx +527 -0
  38. package/src/modules/git/components/DiffView.tsx +30 -0
  39. package/src/modules/git/components/FileComparisonPanel.tsx +377 -0
  40. package/src/modules/git/components/FileHistoryPanel.tsx +320 -0
  41. package/src/modules/git/components/ProtectedBranchBanner.tsx +42 -0
  42. package/src/modules/git/components/PullNeededBanner.tsx +204 -0
  43. package/src/modules/git/components/PullRequestsForMe.tsx +244 -0
  44. package/src/modules/git/components/UnifiedDiffView.tsx +37 -0
  45. package/src/modules/git/hooks/__tests__/useGitState.deleteBranch.test.tsx +99 -0
  46. package/src/modules/git/hooks/useAutoPullUpdates.ts +121 -0
  47. package/src/modules/git/hooks/useGitState.ts +335 -0
  48. package/src/modules/git/services/__tests__/error-messages.test.ts +134 -0
  49. package/src/modules/git/services/error-messages.ts +257 -0
  50. package/src/modules/git/services/git.api.ts +163 -0
  51. package/src/modules/git/services/pr-prompts.ts +159 -0
  52. package/src/modules/git/services/pr.api.ts +39 -0
  53. package/src/modules/git/state/auto-update.context.ts +21 -0
  54. package/src/modules/git/state/git.context.ts +82 -0
  55. package/src/modules/layout/components/AppLayout.tsx +40 -0
  56. package/src/modules/layout/components/DemoBanner.tsx +57 -0
  57. package/src/modules/layout/components/MaintenanceOverlay.tsx +104 -0
  58. package/src/modules/layout/components/MobileChatLayout.tsx +153 -0
  59. package/src/modules/layout/components/ResizableThreePaneLayout.tsx +203 -0
  60. package/src/modules/layout/components/SlideOverlay.tsx +224 -0
  61. package/src/modules/layout/components/__tests__/MobileChatLayout.test.tsx +77 -0
  62. package/src/modules/layout/components/__tests__/ResizableThreePaneLayout.test.tsx +57 -0
  63. package/src/modules/layout/hooks/useMediaQuery.ts +23 -0
  64. package/src/modules/layout/state/__tests__/layout.context.test.tsx +39 -0
  65. package/src/modules/layout/state/layout.context.ts +46 -0
  66. package/src/modules/library/__tests__/DetailDialog.test.tsx +200 -0
  67. package/src/modules/library/__tests__/diff.test.ts +62 -0
  68. package/src/modules/library/__tests__/loadout.test.tsx +72 -0
  69. package/src/modules/library/__tests__/status.test.ts +117 -0
  70. package/src/modules/library/components/ChangeRequestDock.tsx +47 -0
  71. package/src/modules/library/components/CompareView.tsx +357 -0
  72. package/src/modules/library/components/DetailDialog.tsx +566 -0
  73. package/src/modules/library/components/GearIcon.tsx +12 -0
  74. package/src/modules/library/components/LibraryCard.tsx +132 -0
  75. package/src/modules/library/components/LibraryPage.tsx +254 -0
  76. package/src/modules/library/components/LoadoutSidebar.tsx +156 -0
  77. package/src/modules/library/components/StatusGem.tsx +21 -0
  78. package/src/modules/library/components/SuggestChange.tsx +171 -0
  79. package/src/modules/library/components/particles.ts +50 -0
  80. package/src/modules/library/hooks/useLibraryData.ts +119 -0
  81. package/src/modules/library/hooks/useSkillDetail.ts +74 -0
  82. package/src/modules/library/library.css +320 -0
  83. package/src/modules/library/services/library.api.ts +164 -0
  84. package/src/modules/library/state/loadout.tsx +129 -0
  85. package/src/modules/library/state/toast.tsx +42 -0
  86. package/src/modules/library/utils/diff.ts +102 -0
  87. package/src/modules/library/utils/status.ts +116 -0
  88. package/src/modules/pr/__tests__/PrApprovalBadge.test.tsx +93 -0
  89. package/src/modules/pr/__tests__/PrCancelButton.test.tsx +158 -0
  90. package/src/modules/pr/__tests__/PrCancelConfirmDialog.test.tsx +44 -0
  91. package/src/modules/pr/__tests__/PrMergeButton.test.tsx +360 -0
  92. package/src/modules/pr/__tests__/bucketize-comments.test.ts +85 -0
  93. package/src/modules/pr/__tests__/parsePatch.test.ts +104 -0
  94. package/src/modules/pr/__tests__/usePrViewerState.test.ts +72 -0
  95. package/src/modules/pr/components/OpenChangeRequestDialog.tsx +193 -0
  96. package/src/modules/pr/components/PrApprovalBadge.tsx +80 -0
  97. package/src/modules/pr/components/PrCancelButton.tsx +99 -0
  98. package/src/modules/pr/components/PrCancelConfirmDialog.tsx +119 -0
  99. package/src/modules/pr/components/PrCommentComposer.tsx +92 -0
  100. package/src/modules/pr/components/PrCommentThread.tsx +168 -0
  101. package/src/modules/pr/components/PrCommentsPanel.tsx +143 -0
  102. package/src/modules/pr/components/PrDiffPane.tsx +425 -0
  103. package/src/modules/pr/components/PrFileRow.tsx +164 -0
  104. package/src/modules/pr/components/PrFilesList.tsx +53 -0
  105. package/src/modules/pr/components/PrHeader.tsx +45 -0
  106. package/src/modules/pr/components/PrHeaderOverflowMenu.tsx +58 -0
  107. package/src/modules/pr/components/PrMergeButton.tsx +268 -0
  108. package/src/modules/pr/components/PrMergeConfirmDialog.tsx +112 -0
  109. package/src/modules/pr/components/PrRefreshFromTargetButton.tsx +110 -0
  110. package/src/modules/pr/components/PrViewer.tsx +364 -0
  111. package/src/modules/pr/hooks/usePrViewerState.ts +150 -0
  112. package/src/modules/pr/services/pr-approvals.api.ts +31 -0
  113. package/src/modules/pr/services/pr-cancel.api.ts +19 -0
  114. package/src/modules/pr/services/pr-comments.api.ts +45 -0
  115. package/src/modules/pr/services/pr-detail.api.ts +17 -0
  116. package/src/modules/pr/services/pr-merge.api.ts +53 -0
  117. package/src/modules/pr/services/pr-open.api.ts +30 -0
  118. package/src/modules/pr/state/pr-viewer.context.ts +34 -0
  119. package/src/modules/pr/utils/bucketize-comments.ts +55 -0
  120. package/src/modules/pr/utils/parsePatch.ts +57 -0
  121. package/src/modules/review/__tests__/review.api.test.ts +121 -0
  122. package/src/modules/review/components/BinaryChangePlaceholder.tsx +15 -0
  123. package/src/modules/review/components/DiffViewer.tsx +78 -0
  124. package/src/modules/review/components/MarkdownDiffViewer.tsx +263 -0
  125. package/src/modules/review/components/ReviewFileRow.tsx +114 -0
  126. package/src/modules/review/components/ReviewPanel.tsx +260 -0
  127. package/src/modules/review/components/ReviewPanelSurface.tsx +52 -0
  128. package/src/modules/review/components/__tests__/ReviewPanel.test.tsx +106 -0
  129. package/src/modules/review/hooks/__tests__/useReviewState.test.ts +88 -0
  130. package/src/modules/review/hooks/useReviewState.ts +256 -0
  131. package/src/modules/review/services/review.api.ts +72 -0
  132. package/src/modules/review/state/ReviewProvider.tsx +28 -0
  133. package/src/modules/review/state/review.context.ts +29 -0
  134. package/src/modules/secrets-vault/components/ConnectToolsPage.tsx +488 -0
  135. package/src/modules/secrets-vault/components/SecretsPage.tsx +276 -0
  136. package/src/modules/secrets-vault/components/ToolSecretsPanel.tsx +362 -0
  137. package/src/modules/secrets-vault/components/__tests__/SecretsPage.test.tsx +51 -0
  138. package/src/modules/secrets-vault/services/connect.api.ts +113 -0
  139. package/src/modules/secrets-vault/services/secrets.api.ts +82 -0
  140. package/src/modules/secrets-vault/services/tool-secrets.api.ts +116 -0
  141. package/src/modules/toolbar/components/AdminMenu.tsx +250 -0
  142. package/src/modules/toolbar/components/AppSwitcher.tsx +108 -0
  143. package/src/modules/toolbar/components/ExternalAgentAccessPage.tsx +548 -0
  144. package/src/modules/toolbar/components/Toolbar.tsx +123 -0
  145. package/src/modules/toolbar/components/__tests__/AppSwitcher.test.tsx +122 -0
  146. package/src/modules/toolbar/components/__tests__/Toolbar.test.tsx +353 -0
  147. package/src/modules/toolbar/services/external-api-keys.api.ts +65 -0
  148. package/src/modules/tools/ToolsExplorerPage.tsx +380 -0
  149. package/src/modules/workflow/hooks/useEventBusSubscription.ts +24 -0
  150. package/src/modules/workflow/hooks/useFileLock.ts +430 -0
  151. package/src/modules/workflow/services/lock.api.ts +139 -0
  152. package/src/modules/workflow/state/EventBusFocusBinder.tsx +25 -0
  153. package/src/modules/workflow/state/EventBusProvider.tsx +232 -0
  154. package/src/modules/workflow/state/event-bus.context.ts +75 -0
  155. package/src/modules/workspace/__tests__/testFixtures.ts +54 -0
  156. package/src/modules/workspace/__tests__/useWorkspaceState.test.ts +91 -0
  157. package/src/modules/workspace/components/EditorTabs.tsx +384 -0
  158. package/src/modules/workspace/components/FileExplorer.tsx +1068 -0
  159. package/src/modules/workspace/components/FileRoute.tsx +392 -0
  160. package/src/modules/workspace/components/FileViewer.tsx +906 -0
  161. package/src/modules/workspace/components/__tests__/EditorTabs.test.tsx +241 -0
  162. package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +666 -0
  163. package/src/modules/workspace/components/__tests__/FileRoute.test.tsx +330 -0
  164. package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +492 -0
  165. package/src/modules/workspace/components/renderers/CsvRenderer.tsx +174 -0
  166. package/src/modules/workspace/components/renderers/DocxRenderer.tsx +87 -0
  167. package/src/modules/workspace/components/renderers/HtmlRenderer.tsx +231 -0
  168. package/src/modules/workspace/components/renderers/ImageRenderer.tsx +50 -0
  169. package/src/modules/workspace/components/renderers/KbMarkdownView.tsx +287 -0
  170. package/src/modules/workspace/components/renderers/MarkdownRenderer.tsx +252 -0
  171. package/src/modules/workspace/components/renderers/MermaidDiagram.tsx +268 -0
  172. package/src/modules/workspace/components/renderers/PdfRenderer.tsx +81 -0
  173. package/src/modules/workspace/components/renderers/TextRenderer.tsx +86 -0
  174. package/src/modules/workspace/components/renderers/ToolForm.tsx +404 -0
  175. package/src/modules/workspace/components/renderers/ToolRenderer.tsx +417 -0
  176. package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +157 -0
  177. package/src/modules/workspace/components/renderers/__tests__/CsvRenderer.test.ts +57 -0
  178. package/src/modules/workspace/components/renderers/__tests__/KbMarkdownView.test.tsx +95 -0
  179. package/src/modules/workspace/components/renderers/__tests__/MarkdownRenderer.test.tsx +328 -0
  180. package/src/modules/workspace/components/renderers/__tests__/PdfRenderer.test.tsx +167 -0
  181. package/src/modules/workspace/components/renderers/__tests__/htmlSandbox.test.ts +318 -0
  182. package/src/modules/workspace/components/renderers/csvUtils.ts +76 -0
  183. package/src/modules/workspace/components/renderers/htmlSandbox.ts +332 -0
  184. package/src/modules/workspace/components/renderers/index.ts +39 -0
  185. package/src/modules/workspace/components/renderers/types.ts +27 -0
  186. package/src/modules/workspace/hooks/__tests__/useWorkspaceState.test.tsx +298 -0
  187. package/src/modules/workspace/hooks/useWorkspaceState.ts +1315 -0
  188. package/src/modules/workspace/routing/__tests__/kb-routes.test.tsx +99 -0
  189. package/src/modules/workspace/routing/kb-routes.ts +266 -0
  190. package/src/modules/workspace/services/__tests__/workspace.api.test.ts +72 -0
  191. package/src/modules/workspace/services/workspace.api.ts +185 -0
  192. package/src/modules/workspace/state/workspace.context.ts +235 -0
  193. package/src/modules/workspace/utils/__tests__/frontmatter.test.ts +107 -0
  194. package/src/modules/workspace/utils/diff.ts +53 -0
  195. package/src/modules/workspace/utils/fileTree.ts +179 -0
  196. package/src/modules/workspace/utils/frontmatter.ts +80 -0
  197. package/src/modules/workspace/utils/readDroppedEntries.ts +119 -0
  198. package/src/modules/workspace/utils/tab-persistence.ts +23 -0
  199. package/src/shared/components/Dialog.tsx +198 -0
  200. package/src/shared/components/PageShell.tsx +51 -0
  201. package/src/shared/components/useModalLayer.ts +35 -0
  202. package/src/shared/markdown/Markdown.tsx +62 -0
  203. package/src/test-setup.ts +59 -0
@@ -0,0 +1,441 @@
1
+ import { describe, it, expect, vi, afterEach } from 'vitest';
2
+ import { render, screen, fireEvent, waitFor } from '@testing-library/react';
3
+ import type { BranchInfo, FileTreeEntry, WorkingTreeStatus } from '@bevel-software/platform-shared';
4
+ import { PullNeededBanner } from '../components/PullNeededBanner';
5
+ import { GitContext, type GitContextValue } from '../state/git.context';
6
+ import {
7
+ AutoUpdateContext,
8
+ IDLE_AUTO_UPDATE,
9
+ type AutoUpdateState,
10
+ } from '../state/auto-update.context';
11
+ // The banner doesn't seed chat directly — it calls the change-request port
12
+ // (`resolvePullIssue`). In the enterprise build a registry provider binds that
13
+ // port to chat seeding; core tests mount a stub port and assert the port
14
+ // itself is invoked with the classified, user-safe payload.
15
+ import { CrCreationPortContext, type CrCreationPort } from '../../../core/registry';
16
+ import {
17
+ WorkspaceContext,
18
+ type WorkspaceContextValue,
19
+ } from '../../workspace/state/workspace.context';
20
+ import { makeWorkspaceFixture } from '../../workspace/__tests__/testFixtures';
21
+
22
+ function makeStatus(overrides: Partial<WorkingTreeStatus> = {}): WorkingTreeStatus {
23
+ return {
24
+ branch: 'current-company-state',
25
+ hasUpstream: true,
26
+ unmergedFromUpstream: true,
27
+ ...overrides,
28
+ };
29
+ }
30
+
31
+ function makeTree(paths: string[] = ['Knowledge/Foo.md']): FileTreeEntry {
32
+ return {
33
+ name: '',
34
+ relativePath: '',
35
+ type: 'directory',
36
+ children: paths.map((p) => ({
37
+ name: p.split('/').pop() ?? p,
38
+ relativePath: p,
39
+ type: 'file' as const,
40
+ })),
41
+ };
42
+ }
43
+
44
+ function makeGit(
45
+ overrides: Partial<GitContextValue> & { status?: WorkingTreeStatus | null } = {},
46
+ ): GitContextValue {
47
+ const status: WorkingTreeStatus | null =
48
+ overrides.status === undefined ? makeStatus() : overrides.status;
49
+ const branches: BranchInfo[] = [];
50
+ return {
51
+ status,
52
+ branches,
53
+ availability: 'ready',
54
+ lastError: null,
55
+ refreshStatus: async () => null,
56
+ refreshBranches: async () => {},
57
+ createBranch: async () => {},
58
+ deleteBranch: async () => {},
59
+ pull: async () => {},
60
+ fetchForkBase: async () => null,
61
+ revert: async () => ({ authorName: '', authorEmail: '', sha: '', subject: '', committedAt: '' }),
62
+ fetchFileHistory: async () => [],
63
+ fetchFileDiff: async () => '',
64
+ fetchFileComparison: async () => '',
65
+ ...overrides,
66
+ };
67
+ }
68
+
69
+ interface WorkspaceFixture extends WorkspaceContextValue {
70
+ refreshFileTree: ReturnType<typeof vi.fn>;
71
+ bumpFsRevision: ReturnType<typeof vi.fn>;
72
+ }
73
+
74
+ function makeWorkspace(
75
+ overrides: Partial<WorkspaceContextValue> & {
76
+ refreshFileTreeResult?: FileTreeEntry | null;
77
+ } = {},
78
+ ): WorkspaceFixture {
79
+ const refreshFileTree = vi.fn(
80
+ async (): Promise<FileTreeEntry | null> =>
81
+ overrides.refreshFileTreeResult === undefined
82
+ ? makeTree()
83
+ : overrides.refreshFileTreeResult,
84
+ );
85
+ const bumpFsRevision = vi.fn(() => {});
86
+ return makeWorkspaceFixture({
87
+ ...overrides,
88
+ refreshFileTree,
89
+ bumpFsRevision,
90
+ }) as WorkspaceFixture;
91
+ }
92
+
93
+ function renderWith(
94
+ git: GitContextValue,
95
+ options: {
96
+ autoUpdate?: AutoUpdateState;
97
+ resolvePullIssue?: ReturnType<typeof vi.fn>;
98
+ workspace?: WorkspaceFixture;
99
+ } = {},
100
+ ) {
101
+ const resolvePullIssue = options.resolvePullIssue ?? vi.fn();
102
+ const port: CrCreationPort = { start: vi.fn(), resolvePullIssue };
103
+ const workspace = options.workspace ?? makeWorkspace();
104
+ const autoUpdate = options.autoUpdate ?? IDLE_AUTO_UPDATE;
105
+ const result = render(
106
+ <WorkspaceContext.Provider value={workspace}>
107
+ <GitContext.Provider value={git}>
108
+ <AutoUpdateContext.Provider value={autoUpdate}>
109
+ <CrCreationPortContext.Provider value={port}>
110
+ <PullNeededBanner />
111
+ </CrCreationPortContext.Provider>
112
+ </AutoUpdateContext.Provider>
113
+ </GitContext.Provider>
114
+ </WorkspaceContext.Provider>,
115
+ );
116
+ return { ...result, workspace, resolvePullIssue };
117
+ }
118
+
119
+ function failedAutoUpdate(branch = 'current-company-state'): AutoUpdateState {
120
+ return { status: 'failed', branch, reason: 'Network unreachable' };
121
+ }
122
+
123
+ // Wrap addEventListener so callers can register the listener inline and
124
+ // guarantee removal via the returned cleanup, even if the test body throws
125
+ // before the manual removeEventListener line is reached.
126
+ function attachPrStaleListener(handler: EventListener): () => void {
127
+ window.addEventListener('bevel:pr-stale', handler);
128
+ return () => window.removeEventListener('bevel:pr-stale', handler);
129
+ }
130
+
131
+ afterEach(() => {
132
+ vi.restoreAllMocks();
133
+ });
134
+
135
+ describe('PullNeededBanner — gating', () => {
136
+ it('renders nothing when availability is not ready', () => {
137
+ const { container } = renderWith(makeGit({ availability: 'loading', status: null }));
138
+ expect(container.firstChild).toBeNull();
139
+ });
140
+
141
+ it('renders nothing when unmergedFromUpstream is false', () => {
142
+ const { container } = renderWith(
143
+ makeGit({ status: makeStatus({ unmergedFromUpstream: false }) }),
144
+ );
145
+ expect(container.firstChild).toBeNull();
146
+ });
147
+
148
+ it('renders nothing for clean protected branches because auto-update owns them', () => {
149
+ const { container } = renderWith(
150
+ makeGit({ status: makeStatus({ branch: 'current-company-state' }) }),
151
+ );
152
+ expect(container.firstChild).toBeNull();
153
+ });
154
+
155
+ it('shows failed auto-update state with Retry', () => {
156
+ renderWith(makeGit({ status: makeStatus({ branch: 'current-company-state' }) }), {
157
+ autoUpdate: failedAutoUpdate(),
158
+ });
159
+
160
+ expect(screen.getByText(/Couldn’t update automatically/)).toBeInTheDocument();
161
+ expect(screen.getByText('Network unreachable')).toBeInTheDocument();
162
+ expect(screen.getByRole('button', { name: 'Retry' })).toBeEnabled();
163
+ });
164
+
165
+ // Removed: "shows passive guidance for dirty protected branches without a
166
+ // button" — the dirty-tree case is structurally impossible under save=share
167
+ // (every write auto-commits on lock release), so the status no longer
168
+ // carries an `isDirty` field and the banner no longer emits the
169
+ // "discard your local changes" fallback. The "unsaved editor changes"
170
+ // case below remains because in-memory tab edits ARE still a real signal.
171
+
172
+ it('shows passive guidance for unsaved editor changes without a button', () => {
173
+ renderWith(makeGit({ status: makeStatus({ branch: 'current-company-state' }) }), {
174
+ workspace: makeWorkspace({ hasUnsavedFileChanges: true }),
175
+ });
176
+
177
+ expect(screen.getByText(/Updates are waiting/)).toBeInTheDocument();
178
+ expect(screen.getByText(/Finish or save your open file/)).toBeInTheDocument();
179
+ expect(screen.queryByRole('button')).toBeNull();
180
+ });
181
+ });
182
+
183
+ describe('PullNeededBanner — protected branch (auto-update failed retry)', () => {
184
+ it('calls git.pull() and does NOT invoke the change-request port on retry', async () => {
185
+ const pull = vi.fn(async () => {});
186
+ const { resolvePullIssue } = renderWith(
187
+ makeGit({ status: makeStatus({ branch: 'current-company-state' }), pull }),
188
+ { autoUpdate: failedAutoUpdate() },
189
+ );
190
+
191
+ fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
192
+
193
+ await waitFor(() => expect(pull).toHaveBeenCalledTimes(1));
194
+ expect(resolvePullIssue).not.toHaveBeenCalled();
195
+ });
196
+
197
+ it('also retries direct pull on target-company-state after auto-update fails', async () => {
198
+ const pull = vi.fn(async () => {});
199
+ renderWith(makeGit({ status: makeStatus({ branch: 'target-company-state' }), pull }), {
200
+ autoUpdate: failedAutoUpdate('target-company-state'),
201
+ });
202
+
203
+ fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
204
+
205
+ await waitFor(() => expect(pull).toHaveBeenCalledTimes(1));
206
+ });
207
+
208
+ it('shows the Retrying… label and disables the button while retry is in flight', async () => {
209
+ let resolvePull: () => void = () => {};
210
+ const pull = vi.fn(
211
+ () =>
212
+ new Promise<void>((resolve) => {
213
+ resolvePull = resolve;
214
+ }),
215
+ );
216
+ renderWith(makeGit({ status: makeStatus({ branch: 'current-company-state' }), pull }), {
217
+ autoUpdate: failedAutoUpdate(),
218
+ });
219
+
220
+ fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
221
+
222
+ await waitFor(() =>
223
+ expect(screen.getByRole('button', { name: 'Retrying…' })).toBeDisabled(),
224
+ );
225
+
226
+ resolvePull();
227
+ await waitFor(() =>
228
+ expect(screen.getByRole('button', { name: 'Retry' })).toBeEnabled(),
229
+ );
230
+ });
231
+
232
+ it('surfaces the error message inline when pull fails, banner stays visible', async () => {
233
+ const pull = vi.fn(async () => {
234
+ throw new Error('Network unreachable');
235
+ });
236
+ renderWith(makeGit({ status: makeStatus({ branch: 'current-company-state' }), pull }), {
237
+ autoUpdate: failedAutoUpdate(),
238
+ });
239
+
240
+ fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
241
+
242
+ await waitFor(() =>
243
+ expect(screen.getByRole('alert')).toHaveTextContent('Network unreachable'),
244
+ );
245
+ expect(screen.getByRole('button', { name: 'Retry' })).toBeEnabled();
246
+ });
247
+
248
+ it('falls back to handing the failure to the change-request port when direct pull fails', async () => {
249
+ const pull = vi.fn(async () => {
250
+ throw new Error('working tree has uncommitted changes');
251
+ });
252
+ const resolvePullIssue = vi.fn();
253
+ renderWith(
254
+ makeGit({ status: makeStatus({ branch: 'current-company-state' }), pull }),
255
+ { autoUpdate: failedAutoUpdate(), resolvePullIssue },
256
+ );
257
+
258
+ fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
259
+
260
+ await waitFor(() => expect(resolvePullIssue).toHaveBeenCalledTimes(1));
261
+ // The classified reason handed to the port must NOT leak git vocabulary
262
+ // (no "uncommitted", "working tree", "merge conflict", "stash", "HEAD") —
263
+ // registries render it to the user verbatim. We assert the user-facing
264
+ // phrase the classifier produces for a dirty-tree-shaped error AND that
265
+ // the raw git terms aren't in the payload.
266
+ expect(resolvePullIssue).toHaveBeenCalledWith(
267
+ expect.objectContaining({
268
+ branch: 'current-company-state',
269
+ kind: 'pull-failed',
270
+ reason: expect.stringContaining('local changes that need to be sorted out'),
271
+ }),
272
+ );
273
+ const seededText = (resolvePullIssue.mock.calls[0]?.[0] as { reason?: string })?.reason ?? '';
274
+ expect(seededText).not.toMatch(/uncommitted|working tree|stash|HEAD|merge conflict/i);
275
+ });
276
+
277
+ it('hands off only a classified pull-failure reason without sensitive error details', async () => {
278
+ const pull = vi.fn(async () => {
279
+ throw new Error(
280
+ 'fatal: could not resolve host https://ghp_supersecrettoken1234567890abcdef@example.com/org/repo in /Users/alice/work/bevel token=ghp_supersecrettoken1234567890abcdef',
281
+ );
282
+ });
283
+ const resolvePullIssue = vi.fn();
284
+ renderWith(
285
+ makeGit({ status: makeStatus({ branch: 'current-company-state' }), pull }),
286
+ { autoUpdate: failedAutoUpdate(), resolvePullIssue },
287
+ );
288
+
289
+ fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
290
+
291
+ await waitFor(() => expect(resolvePullIssue).toHaveBeenCalledTimes(1));
292
+ const reason = (resolvePullIssue.mock.calls[0][0] as { reason: string }).reason;
293
+ // Plain-language phrase the classifier returns for network/auth-shaped
294
+ // errors. No git vocabulary (no "401", "credential", "fatal", etc.) and
295
+ // none of the sensitive details from the underlying error message.
296
+ expect(reason).toContain('connection or permission problem');
297
+ expect(reason).not.toContain('https://');
298
+ expect(reason).not.toContain('/Users/alice/work/bevel');
299
+ expect(reason).not.toContain('ghp_supersecrettoken');
300
+ });
301
+
302
+ it('shows a generic message when the thrown value is not an Error', async () => {
303
+ const pull = vi.fn(async () => {
304
+ throw 'oops';
305
+ });
306
+ renderWith(makeGit({ status: makeStatus({ branch: 'current-company-state' }), pull }), {
307
+ autoUpdate: failedAutoUpdate(),
308
+ });
309
+
310
+ fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
311
+
312
+ await waitFor(() =>
313
+ expect(screen.getByRole('alert')).toHaveTextContent(/Could not get updates/),
314
+ );
315
+ });
316
+
317
+ it('clears a stale error when the user retries successfully', async () => {
318
+ let shouldFail = true;
319
+ const pull = vi.fn(async () => {
320
+ if (shouldFail) throw new Error('boom');
321
+ });
322
+ renderWith(makeGit({ status: makeStatus({ branch: 'current-company-state' }), pull }), {
323
+ autoUpdate: failedAutoUpdate(),
324
+ });
325
+
326
+ fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
327
+ await waitFor(() => expect(screen.getByRole('alert')).toBeInTheDocument());
328
+
329
+ shouldFail = false;
330
+ fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
331
+ await waitFor(() => expect(screen.queryByRole('alert')).not.toBeInTheDocument());
332
+ expect(pull).toHaveBeenCalledTimes(2);
333
+ });
334
+ });
335
+
336
+ describe('PullNeededBanner — workspace refresh after successful pull', () => {
337
+ // Under the multi-tab model, the post-pull reconciliation moved from a
338
+ // single-file openFile/closeFile shuffle to a `bumpFsRevision()` call:
339
+ // the active tab eagerly refetches, inactive non-dirty tabs invalidate,
340
+ // and tabs whose files vanished get dropped by the eager refetch's 404
341
+ // handler in useWorkspaceState. Tests assert the new signal, not the
342
+ // legacy single-file moves.
343
+
344
+ it('refreshes the file tree and bumps fsRevision after a successful pull', async () => {
345
+ const pull = vi.fn(async () => {});
346
+ const workspace = makeWorkspace({
347
+ refreshFileTreeResult: makeTree(['Knowledge/Foo.md', 'Knowledge/New.md']),
348
+ });
349
+ renderWith(makeGit({ status: makeStatus({ branch: 'current-company-state' }), pull }), {
350
+ autoUpdate: failedAutoUpdate(),
351
+ workspace,
352
+ });
353
+
354
+ fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
355
+
356
+ await waitFor(() => expect(workspace.refreshFileTree).toHaveBeenCalledTimes(1));
357
+ await waitFor(() => expect(workspace.bumpFsRevision).toHaveBeenCalledTimes(1));
358
+ });
359
+
360
+ it('dispatches bevel:pr-stale exactly once per successful pull', async () => {
361
+ const pull = vi.fn(async () => {});
362
+ const onPrStale = vi.fn();
363
+ const detachPrStale = attachPrStaleListener(onPrStale);
364
+ try {
365
+ renderWith(makeGit({ status: makeStatus({ branch: 'current-company-state' }), pull }), {
366
+ autoUpdate: failedAutoUpdate(),
367
+ });
368
+
369
+ fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
370
+
371
+ await waitFor(() => expect(onPrStale).toHaveBeenCalledTimes(1));
372
+ } finally {
373
+ detachPrStale();
374
+ }
375
+ });
376
+
377
+ it('does NOT refresh workspace or fire pr-stale when pull fails', async () => {
378
+ const pull = vi.fn(async () => {
379
+ throw new Error('boom');
380
+ });
381
+ const workspace = makeWorkspace();
382
+ const onPrStale = vi.fn();
383
+ const detachPrStale = attachPrStaleListener(onPrStale);
384
+ try {
385
+ renderWith(makeGit({ status: makeStatus({ branch: 'current-company-state' }), pull }), {
386
+ autoUpdate: failedAutoUpdate(),
387
+ workspace,
388
+ });
389
+
390
+ fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
391
+
392
+ await waitFor(() => expect(screen.getByRole('alert')).toBeInTheDocument());
393
+ expect(workspace.refreshFileTree).not.toHaveBeenCalled();
394
+ expect(workspace.bumpFsRevision).not.toHaveBeenCalled();
395
+ expect(onPrStale).not.toHaveBeenCalled();
396
+ } finally {
397
+ detachPrStale();
398
+ }
399
+ });
400
+ });
401
+
402
+ describe('PullNeededBanner — feature branch (agent flow, REGRESSION)', () => {
403
+ // CRITICAL: feature-branch behavior must remain the port hand-off flow.
404
+ // Conflicts can happen when the user has local commits, and an interactive
405
+ // resolution (the enterprise agent) earns its keep there. Direct pull on
406
+ // feature branches would surface a raw rebase error with no path forward.
407
+
408
+ it('hands the behind-state to the port and does NOT call git.pull() or refresh the workspace', () => {
409
+ const pull = vi.fn(async () => {});
410
+ const workspace = makeWorkspace();
411
+ const onPrStale = vi.fn();
412
+ const detachPrStale = attachPrStaleListener(onPrStale);
413
+ try {
414
+ const { resolvePullIssue } = renderWith(
415
+ makeGit({ status: makeStatus({ branch: 'alice/draft' }), pull }),
416
+ { workspace },
417
+ );
418
+
419
+ fireEvent.click(screen.getByRole('button', { name: 'Ask assistant' }));
420
+
421
+ expect(pull).not.toHaveBeenCalled();
422
+ expect(workspace.refreshFileTree).not.toHaveBeenCalled();
423
+ expect(workspace.bumpFsRevision).not.toHaveBeenCalled();
424
+ expect(onPrStale).not.toHaveBeenCalled();
425
+ expect(resolvePullIssue).toHaveBeenCalledTimes(1);
426
+ expect(resolvePullIssue).toHaveBeenCalledWith(
427
+ expect.objectContaining({ branch: 'alice/draft', kind: 'behind' }),
428
+ );
429
+ } finally {
430
+ detachPrStale();
431
+ }
432
+ });
433
+
434
+ it('invokes the port for any non-protected branch name', () => {
435
+ const { resolvePullIssue } = renderWith(makeGit({ status: makeStatus({ branch: 'main' }) }));
436
+
437
+ fireEvent.click(screen.getByRole('button', { name: 'Ask assistant' }));
438
+
439
+ expect(resolvePullIssue).toHaveBeenCalledTimes(1);
440
+ });
441
+ });
@@ -0,0 +1,113 @@
1
+ import { describe, it, expect, beforeEach, vi } from 'vitest';
2
+ import {
3
+ createBranch,
4
+ fetchBranches,
5
+ fetchFileDiff,
6
+ fetchFileHistory,
7
+ fetchStatus,
8
+ } from '../services/git.api';
9
+
10
+ const fetchMock = vi.fn();
11
+
12
+ beforeEach(() => {
13
+ fetchMock.mockReset();
14
+ vi.stubGlobal('fetch', fetchMock);
15
+ // authFetch reads from localStorage; clear per-test.
16
+ localStorage.clear();
17
+ });
18
+
19
+ function ok(body: unknown): Response {
20
+ return new Response(JSON.stringify(body), {
21
+ status: 200,
22
+ headers: { 'Content-Type': 'application/json' },
23
+ });
24
+ }
25
+
26
+ describe('git.api', () => {
27
+ it('fetchStatus GETs the status route and returns JSON', async () => {
28
+ fetchMock.mockResolvedValueOnce(
29
+ ok({
30
+ branch: 'main',
31
+ isDirty: false,
32
+ hasUpstream: true,
33
+ unpushedCommits: 0,
34
+ conflicted: [],
35
+ unmergedFromUpstream: false,
36
+ }),
37
+ );
38
+ const s = await fetchStatus('ws-1');
39
+ expect(s.branch).toBe('main');
40
+ expect(s.hasUpstream).toBe(true);
41
+ const [url, init] = fetchMock.mock.calls[0];
42
+ expect(url).toBe('/api/workspace/ws-1/workflow/branch-status');
43
+ expect(init?.method).toBeUndefined();
44
+ });
45
+
46
+ it('fetchBranches returns the array body', async () => {
47
+ fetchMock.mockResolvedValueOnce(
48
+ ok([
49
+ { name: 'current-company-state', isCurrent: true, isProtected: true, ahead: 0, behind: 0 },
50
+ ]),
51
+ );
52
+ const bs = await fetchBranches('ws-1');
53
+ expect(bs).toHaveLength(1);
54
+ expect(bs[0].name).toBe('current-company-state');
55
+ });
56
+
57
+ it('createBranch POSTs JSON body', async () => {
58
+ fetchMock.mockResolvedValueOnce(
59
+ ok({ name: 'feature/x', isCurrent: false, isProtected: false, ahead: null, behind: null }),
60
+ );
61
+ await createBranch('ws-1', 'feature/x');
62
+ const [, init] = fetchMock.mock.calls[0];
63
+ expect(init?.method).toBe('POST');
64
+ expect(JSON.parse(init?.body as string)).toEqual({ name: 'feature/x', fromBase: undefined });
65
+ });
66
+
67
+ // `switchBranch` removed from the API surface: under the per-branch workspace
68
+ // model, branches are workspace identity. Switching is a URL navigation that
69
+ // triggers the workspace bootstrap (`getOrCreateForBranch`), not a POST
70
+ // against the source workspace's clone. The route + function have been
71
+ // deleted together.
72
+
73
+ it('fetchFileHistory unwraps the commits array and passes path + limit as query params', async () => {
74
+ fetchMock.mockResolvedValueOnce(
75
+ ok({
76
+ changes: [
77
+ {
78
+ sha: 'abcdef1234567890',
79
+ authorName: 'Ali',
80
+ authorEmail: 'ali@example.com',
81
+ subject: 'edit foo',
82
+ committedAt: '2026-04-18T00:00:00Z',
83
+ },
84
+ ],
85
+ }),
86
+ );
87
+ const history = await fetchFileHistory('ws-1', 'Knowledge/Foo.md', 5);
88
+ expect(history).toHaveLength(1);
89
+ expect(history[0].subject).toBe('edit foo');
90
+ const [url] = fetchMock.mock.calls[0];
91
+ expect(url).toContain('/api/workspace/ws-1/workflow/changes?');
92
+ expect(url).toContain('path=Knowledge%2FFoo.md');
93
+ expect(url).toContain('limit=5');
94
+ });
95
+
96
+ it('fetchFileDiff returns the raw unified diff string', async () => {
97
+ fetchMock.mockResolvedValueOnce(ok({ diff: '--- a\n+++ b\n@@ -1 +1 @@\n-foo\n+bar\n' }));
98
+ const diff = await fetchFileDiff('ws-1', 'Knowledge/Foo.md', 'abc1234');
99
+ expect(diff).toContain('+bar');
100
+ const [url] = fetchMock.mock.calls[0];
101
+ expect(url).toContain('/api/workspace/ws-1/workflow/show-file?');
102
+ expect(url).toContain('sha=abc1234');
103
+ expect(url).toContain('path=Knowledge%2FFoo.md');
104
+ });
105
+
106
+ it('falls back to HTTP <status> when the server returns a non-JSON body', async () => {
107
+ fetchMock.mockResolvedValueOnce(new Response('<html>boom</html>', { status: 500 }));
108
+ await expect(fetchStatus('ws-1')).rejects.toMatchObject({
109
+ status: 500,
110
+ message: 'HTTP 500',
111
+ });
112
+ });
113
+ });
@@ -0,0 +1,44 @@
1
+ import { describe, it, expect, beforeEach, vi } from 'vitest';
2
+ import {
3
+ listPullRequestsForMe,
4
+ listMyPullRequests,
5
+ getPullRequest,
6
+ } from '../services/pr.api';
7
+
8
+ const fetchMock = vi.fn();
9
+
10
+ beforeEach(() => {
11
+ fetchMock.mockReset();
12
+ vi.stubGlobal('fetch', fetchMock);
13
+ localStorage.clear();
14
+ });
15
+
16
+ function ok(body: unknown): Response {
17
+ return new Response(JSON.stringify(body), {
18
+ status: 200,
19
+ headers: { 'Content-Type': 'application/json' },
20
+ });
21
+ }
22
+
23
+ describe('pr.api', () => {
24
+ it('listPullRequestsForMe GETs the workflow for-me route', async () => {
25
+ fetchMock.mockResolvedValueOnce(ok([]));
26
+ const result = await listPullRequestsForMe();
27
+ expect(result).toEqual([]);
28
+ expect(fetchMock.mock.calls[0][0]).toBe('/api/workflow/change-requests/for-me');
29
+ });
30
+
31
+ it('listMyPullRequests GETs the workflow mine route and returns the parsed body', async () => {
32
+ fetchMock.mockResolvedValueOnce(ok([]));
33
+ const result = await listMyPullRequests();
34
+ expect(result).toEqual([]);
35
+ expect(fetchMock.mock.calls[0][0]).toBe('/api/workflow/change-requests/mine');
36
+ });
37
+
38
+ it('getPullRequest GETs the workflow change-request detail route', async () => {
39
+ fetchMock.mockResolvedValueOnce(ok({ number: 42, title: 't' }));
40
+ const pr = await getPullRequest(42);
41
+ expect(pr.number).toBe(42);
42
+ expect(fetchMock.mock.calls[0][0]).toBe('/api/workflow/change-requests/42');
43
+ });
44
+ });