@openchamber/web 1.23.2 → 1.24.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 (179) hide show
  1. package/dist/assets/{CodeMirrorCommentWidgets-DdNGwbBw.js → CodeMirrorCommentWidgets-C9kV-n7r.js} +1 -1
  2. package/dist/assets/DiffView-B7YkbY-u.js +4 -0
  3. package/dist/assets/DirtyBranchSwitchDialog-CU0A93Zx.js +1 -0
  4. package/dist/assets/FilesView-vjJx7VaM.js +13 -0
  5. package/dist/assets/GitView-BLhlMYEY.js +6 -0
  6. package/dist/assets/GuestAttachDialog-B-fwE8r3.js +3 -0
  7. package/dist/assets/{JsonTreeView-Chzx5Y6N.js → JsonTreeView-D_4G5X6-.js} +1 -1
  8. package/dist/assets/LinearIssuesView-1uKhePfH.js +1 -0
  9. package/dist/assets/{MarkdownImageGallery-CugnrZ6f.js → MarkdownImageGallery-Ix7N_2wp.js} +1 -1
  10. package/dist/assets/MarkdownRendererImpl-Deel_y5S.js +12 -0
  11. package/dist/assets/{OnboardingScreen-hH9xcTjA.js → OnboardingScreen-ToIakxb_.js} +1 -1
  12. package/dist/assets/{PatchDiff-DRgNYc9l.js → PatchDiff-BjMgezr1.js} +1 -1
  13. package/dist/assets/{PierreDiffViewer-ZT2CKuzJ.js → PierreDiffViewer-53Ghfnlr.js} +5 -5
  14. package/dist/assets/PlanView-BGAzxDwD.js +5 -0
  15. package/dist/assets/SettingsView-n2X-jfBB.js +29 -0
  16. package/dist/assets/SettingsWindow-Bi8LE9XI.js +1 -0
  17. package/dist/assets/SubmoduleDiffSummary--YFqK6Ih.js +1 -0
  18. package/dist/assets/ToolOutputDialog-DRWBvdaJ.js +15 -0
  19. package/dist/assets/{ToolPartDiffPreview-oWxWCu-4.js → ToolPartDiffPreview-BxZJCU28.js} +2 -2
  20. package/dist/assets/{VirtualizedFileDiff-CiyrAq_I.js → VirtualizedFileDiff-DCXjBGRf.js} +3 -3
  21. package/dist/assets/{WalkthroughView-CKTevaea.js → WalkthroughView-CfIPvWWa.js} +2 -2
  22. package/dist/assets/appThemeRegistry-DDROzcUO.js +1 -0
  23. package/dist/assets/{computeVirtualFileMetrics-SUWoTyPe.js → computeVirtualFileMetrics-DHgQIkUq.js} +1 -1
  24. package/dist/assets/de-CX_f-T5U.js +10 -0
  25. package/dist/assets/es-BERmWNn_.js +16 -0
  26. package/dist/assets/fr-BjYYE_-z.js +14 -0
  27. package/dist/assets/index-DpzTz2UH.css +1 -0
  28. package/dist/assets/index-Dy8xgawx.js +40 -0
  29. package/dist/assets/{index-C4jJ3Ur5.js → index-WjufPkIV.js} +1 -1
  30. package/dist/assets/{isDiffPlainText-DfG0cZxr.js → isDiffPlainText-D9faP70y.js} +1 -1
  31. package/dist/assets/{isFilePlainText-CyNivOCU.js → isFilePlainText-6qOcz5Ky.js} +1 -1
  32. package/dist/assets/ja-DxqeAk9N.js +16 -0
  33. package/dist/assets/ko-Dty1aUod.js +16 -0
  34. package/dist/assets/main-BOYSXfYn.js +896 -0
  35. package/dist/assets/{main-B8cGozKr.js → main-Bjcre_GE.js} +2 -2
  36. package/dist/assets/{markdown-shiki.worker-D5fBU-lw.js → markdown-shiki.worker-CahLvHIq.js} +5 -5
  37. package/dist/assets/{markdownCore-kyGrWGsn.js → markdownCore-DPrwM1rU.js} +5 -5
  38. package/dist/assets/{miniChat-Cay99IxR.js → miniChat-Dz_bPgMc.js} +2 -2
  39. package/dist/assets/{mobile-Zfhix7nz.js → mobile-BbZPBJrK.js} +2 -2
  40. package/dist/assets/{patchFileDiff-CJ5AN9Ql.js → patchFileDiff-BvU6KOQC.js} +3 -3
  41. package/dist/assets/pl-CX-_ROkr.js +16 -0
  42. package/dist/assets/{pt-BR-CLosVawq.js → pt-BR-DhRR1arx.js} +4 -4
  43. package/dist/assets/renderElectronMiniChatApp-uNEUkeAI.js +2 -0
  44. package/dist/assets/renderMobileApp-BK1kFlcJ.js +25 -0
  45. package/dist/assets/runtimeEndpointReset-BULHvOwb.js +25 -0
  46. package/dist/assets/{scheduledTasksApi-CGFV4Pap.js → scheduledTasksApi-DW3xqMQ8.js} +1 -1
  47. package/dist/assets/shikiHighlight-DsxD9QEa.js +1 -0
  48. package/dist/assets/textMateThemeFromAppTheme-Da7ed8qM.js +1 -0
  49. package/dist/assets/tr-cIQtYBpg.js +7 -0
  50. package/dist/assets/uk-CD21CXFV.js +16 -0
  51. package/dist/assets/{useAppFontEffects-DgCHy6Mr.js → useAppFontEffects-hfv2s6mZ.js} +198 -147
  52. package/dist/assets/useInlineCommentController-BmtfVbo8.js +2 -0
  53. package/dist/assets/useRootScrollLock-Cwc4kSl2.js +1 -0
  54. package/dist/assets/{vendor-base-ui-BY_5qla_.js → vendor-base-ui-CsXYnR5c.js} +1 -1
  55. package/dist/assets/{vendor-codemirror-language-data-ES33GhKK.js → vendor-codemirror-language-data-BPMJpOpT.js} +2 -2
  56. package/dist/assets/vendor-framer-motion-Bq3iTZ6r.js +1 -0
  57. package/dist/assets/{vendor-motion-dom-D_o3BTI3.js → vendor-motion-dom-DcCgeG69.js} +1 -1
  58. package/dist/assets/vendor-strip-json-comments-5dJlOkP1.js +4 -0
  59. package/dist/assets/{vendor-zod-B6551dwG.js → vendor-zod-fZppILAM.js} +4 -4
  60. package/dist/assets/{web-components-CJZwV5BE.js → web-components-U-hWbgJo.js} +2 -2
  61. package/dist/assets/zh-CN-BWvlPHY-.js +16 -0
  62. package/dist/assets/zh-TW-CRp5dJU2.js +16 -0
  63. package/dist/index.html +4 -4
  64. package/dist/mini-chat.html +4 -4
  65. package/dist/mobile.html +4 -4
  66. package/package.json +7 -5
  67. package/server/built-in-extensions/registry.json +4 -0
  68. package/server/index.d.ts +2 -0
  69. package/server/index.js +15 -3
  70. package/server/lib/fs/DOCUMENTATION.md +8 -0
  71. package/server/lib/fs/git-process.test.js +77 -0
  72. package/server/lib/fs/routes.js +2 -1
  73. package/server/lib/fs/search.js +2 -1
  74. package/server/lib/git/DOCUMENTATION.md +3 -1
  75. package/server/lib/git/routes.js +16 -3
  76. package/server/lib/git/service.js +112 -8
  77. package/server/lib/git/service.test.js +127 -0
  78. package/server/lib/guests/DOCUMENTATION.md +66 -0
  79. package/server/lib/guests/auth-store.js +184 -0
  80. package/server/lib/guests/auth-store.test.js +112 -0
  81. package/server/lib/guests/builtins.js +34 -0
  82. package/server/lib/guests/builtins.test.js +174 -0
  83. package/server/lib/guests/catalog.js +466 -0
  84. package/server/lib/guests/catalog.test.js +390 -0
  85. package/server/lib/guests/clone.js +297 -0
  86. package/server/lib/guests/extract-zip.js +136 -0
  87. package/server/lib/guests/extract-zip.test.js +109 -0
  88. package/server/lib/guests/files.js +308 -0
  89. package/server/lib/guests/files.test.js +262 -0
  90. package/server/lib/guests/grant-scope.js +81 -0
  91. package/server/lib/guests/grant-scope.test.js +75 -0
  92. package/server/lib/guests/host-session.js +73 -0
  93. package/server/lib/guests/host-session.test.js +30 -0
  94. package/server/lib/guests/html-styles.js +6 -0
  95. package/server/lib/guests/html-styles.test.js +45 -0
  96. package/server/lib/guests/html-tokens.js +73 -0
  97. package/server/lib/guests/html-tokens.test.js +53 -0
  98. package/server/lib/guests/install.js +406 -0
  99. package/server/lib/guests/install.test.js +338 -0
  100. package/server/lib/guests/oauth.js +501 -0
  101. package/server/lib/guests/oauth.test.js +481 -0
  102. package/server/lib/guests/persist.js +336 -0
  103. package/server/lib/guests/persist.test.js +288 -0
  104. package/server/lib/guests/request.js +112 -0
  105. package/server/lib/guests/request.test.js +206 -0
  106. package/server/lib/guests/routes.js +838 -0
  107. package/server/lib/guests/service.js +538 -0
  108. package/server/lib/guests/service.test.js +277 -0
  109. package/server/lib/guests/sockets.js +129 -0
  110. package/server/lib/guests/sockets.test.js +110 -0
  111. package/server/lib/guests/ssh-install.test.js +188 -0
  112. package/server/lib/guests/storage.js +68 -0
  113. package/server/lib/guests/storage.test.js +59 -0
  114. package/server/lib/guests/updates.js +294 -0
  115. package/server/lib/guests/updates.test.js +334 -0
  116. package/server/lib/guests/upload.js +50 -0
  117. package/server/lib/guests/upload.test.js +131 -0
  118. package/server/lib/linear/DOCUMENTATION.md +1 -1
  119. package/server/lib/openchamber-control/DOCUMENTATION.md +6 -0
  120. package/server/lib/openchamber-sessions/routes.js +44 -11
  121. package/server/lib/openchamber-sessions/routes.test.js +47 -0
  122. package/server/lib/opencode/DOCUMENTATION.md +40 -0
  123. package/server/lib/opencode/core-routes.js +39 -1
  124. package/server/lib/opencode/feature-routes-runtime.js +10 -0
  125. package/server/lib/opencode/lifecycle-process.test.js +84 -0
  126. package/server/lib/opencode/lifecycle.js +54 -67
  127. package/server/lib/opencode/settings-normalization-runtime.js +2 -0
  128. package/server/lib/opencode/settings-normalization-runtime.test.js +13 -0
  129. package/server/lib/opencode/settings-registry.json +4 -0
  130. package/server/lib/opencode/shutdown-runtime.js +3 -0
  131. package/server/lib/opencode/shutdown-runtime.test.js +24 -0
  132. package/server/lib/opencode/theme-archive.js +53 -0
  133. package/server/lib/opencode/theme-catalog.js +171 -0
  134. package/server/lib/opencode/theme-catalog.test.js +125 -0
  135. package/server/lib/opencode/theme-import.test.js +144 -0
  136. package/server/lib/opencode/theme-runtime.js +100 -28
  137. package/server/lib/opencode/theme-runtime.test.js +15 -0
  138. package/server/lib/quota/DOCUMENTATION.md +0 -1
  139. package/server/lib/quota/providers/index.js +0 -7
  140. package/server/lib/terminal/DOCUMENTATION.md +2 -0
  141. package/server/lib/terminal/runtime.js +17 -2
  142. package/server/lib/terminal/runtime.test.js +45 -0
  143. package/server/lib/ui-auth/ui-auth.js +43 -8
  144. package/server/lib/ui-auth/ui-auth.test.js +40 -0
  145. package/dist/assets/DiffView-GYriC0Wk.js +0 -4
  146. package/dist/assets/DirtyBranchSwitchDialog-Bv0C99hc.js +0 -1
  147. package/dist/assets/FilesView-2Lin13wx.js +0 -13
  148. package/dist/assets/GitView-9wKba3UO.js +0 -6
  149. package/dist/assets/LinearIssuesView-DXS9QJiR.js +0 -1
  150. package/dist/assets/MarkdownRendererImpl-CaMyct2Q.js +0 -12
  151. package/dist/assets/PlanView-DiTBm2BD.js +0 -5
  152. package/dist/assets/SettingsView-CN8VeM99.js +0 -28
  153. package/dist/assets/SettingsWindow-DisOghhD.js +0 -1
  154. package/dist/assets/ToolOutputDialog-BqiKydxR.js +0 -15
  155. package/dist/assets/appThemeRegistry-BK0dHKa6.js +0 -1
  156. package/dist/assets/de-DQWC94VK.js +0 -10
  157. package/dist/assets/es-DixFUD3m.js +0 -16
  158. package/dist/assets/fr-D6u8tvfA.js +0 -14
  159. package/dist/assets/index-BvpFzhLs.js +0 -40
  160. package/dist/assets/index-n5p3SnES.css +0 -1
  161. package/dist/assets/ja-4u4pCorB.js +0 -16
  162. package/dist/assets/ko-C27-LW_b.js +0 -16
  163. package/dist/assets/main-tEA-fFPg.js +0 -930
  164. package/dist/assets/pl-FtCjsNdW.js +0 -16
  165. package/dist/assets/registerCustomTheme-BBXUCRnG.js +0 -1
  166. package/dist/assets/renderElectronMiniChatApp-CuHOGlF4.js +0 -2
  167. package/dist/assets/renderMobileApp-C4X_KUH8.js +0 -25
  168. package/dist/assets/runtimeEndpointReset-CMg-KUx3.js +0 -41
  169. package/dist/assets/shikiHighlight-DdKWYWrV.js +0 -1
  170. package/dist/assets/tr-D2j6wUez.js +0 -7
  171. package/dist/assets/uk-C6IzmwM_.js +0 -16
  172. package/dist/assets/useGitComparison-BIaI0WZI.js +0 -1
  173. package/dist/assets/useInlineCommentController-2wcCtpM3.js +0 -2
  174. package/dist/assets/useRootScrollLock-CbWA03xR.js +0 -1
  175. package/dist/assets/vendor-framer-motion-livSKyyV.js +0 -1
  176. package/dist/assets/zh-CN-Wl6tIDz1.js +0 -16
  177. package/dist/assets/zh-TW-hgphoWSN.js +0 -16
  178. package/server/lib/quota/providers/crof.js +0 -96
  179. package/server/lib/quota/providers/crof.test.js +0 -98
@@ -0,0 +1,112 @@
1
+ import {
2
+ GUEST_REQUEST_RESPONSE_MAX,
3
+ GUEST_REQUEST_TIMEOUT_MS,
4
+ isGuestRequestPath,
5
+ resolveIntegrationApi,
6
+ } from '@openchamber/sdk';
7
+
8
+ import { dropGuestTokens } from './auth-store.js';
9
+ import { resolveHostAccessToken } from './host-session.js';
10
+ import { GuestOAuthError, guestAuthorizationHeader, refreshGuestAccessToken, takeUsableGuestAuth } from './oauth.js';
11
+
12
+ const METHODS = new Set(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']);
13
+
14
+ export const joinGuestRequestUrl = (apiOrigin, path, query) => {
15
+ if (!isGuestRequestPath(path)) {
16
+ return null;
17
+ }
18
+ let url;
19
+ try {
20
+ url = new URL(path, `${apiOrigin}/`);
21
+ } catch {
22
+ return null;
23
+ }
24
+ if (url.origin !== apiOrigin) {
25
+ return null;
26
+ }
27
+ if (query) {
28
+ for (const [key, value] of Object.entries(query)) {
29
+ if (typeof key !== 'string' || typeof value !== 'string') {
30
+ return null;
31
+ }
32
+ url.searchParams.set(key, value);
33
+ }
34
+ }
35
+ return url;
36
+ };
37
+
38
+ const readCappedBody = async (response) => {
39
+ const text = await response.text();
40
+ if (text.length <= GUEST_REQUEST_RESPONSE_MAX) {
41
+ return text;
42
+ }
43
+ return text.slice(0, GUEST_REQUEST_RESPONSE_MAX);
44
+ };
45
+
46
+ const sendAuthorized = async (url, method, body, accessToken, authorization) => {
47
+ const headers = {
48
+ Accept: 'application/json',
49
+ Authorization: guestAuthorizationHeader(accessToken, authorization),
50
+ };
51
+ if (body !== undefined && method !== 'GET') {
52
+ headers['Content-Type'] = 'application/json';
53
+ }
54
+ return fetch(url, {
55
+ method,
56
+ headers,
57
+ body: method === 'GET' || body === undefined ? undefined : body,
58
+ redirect: 'manual',
59
+ signal: AbortSignal.timeout(GUEST_REQUEST_TIMEOUT_MS),
60
+ });
61
+ };
62
+
63
+ export const proxyGuestRequest = async ({ guest, persistPath, method, path, query, body }) => {
64
+ const api = resolveIntegrationApi(guest.integration ?? {});
65
+ if (!api) {
66
+ throw new GuestOAuthError('This guest does not declare an API origin.', 'NO_INTEGRATION');
67
+ }
68
+ if (!METHODS.has(method)) {
69
+ throw new GuestOAuthError('Unsupported request method.', 'BAD_METHOD');
70
+ }
71
+ const url = joinGuestRequestUrl(api.apiOrigin, path, query);
72
+ if (!url) {
73
+ throw new GuestOAuthError('Request path must stay on the declared apiOrigin.', 'BAD_PATH');
74
+ }
75
+ const hostToken = await resolveHostAccessToken(guest.integration);
76
+ const stored = hostToken ? null : await takeUsableGuestAuth(guest, persistPath);
77
+ let accessToken = hostToken ?? stored?.accessToken;
78
+ if (!accessToken) {
79
+ throw new GuestOAuthError('Not connected.', 'DISCONNECTED');
80
+ }
81
+
82
+ let response = await sendAuthorized(url, method, body, accessToken, api.authorization);
83
+ if (response.status === 401) {
84
+ const refreshed = hostToken
85
+ ? await resolveHostAccessToken(guest.integration)
86
+ : await refreshGuestAccessToken({ guest, persistPath }).catch(() => null);
87
+ // Only the tokens this request was refused with are dropped. A refresh
88
+ // that came back null because the user connected again meanwhile (new
89
+ // tokens, maybe a new target) must leave that new connection alone.
90
+ const refusedWith = accessToken;
91
+ const dropRefused = () => dropGuestTokens(guest.id, persistPath, (current) => current?.accessToken === refusedWith);
92
+ if (!refreshed || refreshed === accessToken) {
93
+ if (!hostToken) {
94
+ await dropRefused();
95
+ }
96
+ throw new GuestOAuthError('Not connected.', 'DISCONNECTED');
97
+ }
98
+ accessToken = refreshed;
99
+ response = await sendAuthorized(url, method, body, accessToken, api.authorization);
100
+ if (response.status === 401) {
101
+ if (!hostToken) {
102
+ await dropGuestTokens(guest.id, persistPath, (current) => current?.accessToken === refreshed);
103
+ }
104
+ throw new GuestOAuthError('Not connected.', 'DISCONNECTED');
105
+ }
106
+ }
107
+
108
+ return {
109
+ status: response.status,
110
+ body: await readCappedBody(response),
111
+ };
112
+ };
@@ -0,0 +1,206 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import fs from 'node:fs/promises';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+
6
+ import { getGuestAuth, guestAuthPersistPath, patchGuestAuth } from './auth-store.js';
7
+ import { credentialTarget } from './oauth.js';
8
+ import { joinGuestRequestUrl, proxyGuestRequest } from './request.js';
9
+
10
+ const clickupGuest = {
11
+ id: 'clickup',
12
+ integration: {
13
+ name: 'ClickUp',
14
+ description: 'Tasks',
15
+ oauth: {
16
+ authorizeUrl: 'https://app.clickup.com/api',
17
+ tokenUrl: 'https://api.clickup.com/api/v2/oauth/token',
18
+ apiOrigin: 'https://api.clickup.com',
19
+ },
20
+ },
21
+ };
22
+
23
+ describe('joinGuestRequestUrl', () => {
24
+ test('stays on the declared origin and rejects escapes', () => {
25
+ expect(joinGuestRequestUrl('https://api.clickup.com', '/api/v2/user')?.href)
26
+ .toBe('https://api.clickup.com/api/v2/user');
27
+ expect(joinGuestRequestUrl('https://api.clickup.com', '/api/../secret')).toBeNull();
28
+ expect(joinGuestRequestUrl('https://api.clickup.com', 'https://evil.example/x')).toBeNull();
29
+ expect(joinGuestRequestUrl('https://api.clickup.com', '//evil.example/x')).toBeNull();
30
+ });
31
+ });
32
+
33
+ describe('proxyGuestRequest', () => {
34
+ test('attaches the stored bearer and returns the capped body', async () => {
35
+ const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'oc-guest-req-'));
36
+ const persistPath = guestAuthPersistPath(dir);
37
+ await patchGuestAuth('clickup', { accessToken: 'tok-1', target: credentialTarget(clickupGuest.integration) }, persistPath);
38
+ const originalFetch = globalThis.fetch;
39
+ const seen = [];
40
+ globalThis.fetch = async (url, init) => {
41
+ seen.push({ href: String(url), auth: init.headers.Authorization });
42
+ return new Response('{"ok":true}', { status: 200 });
43
+ };
44
+ try {
45
+ const result = await proxyGuestRequest({
46
+ guest: clickupGuest,
47
+ persistPath,
48
+ method: 'GET',
49
+ path: '/api/v2/list/1/task',
50
+ });
51
+ expect(result).toEqual({ status: 200, body: '{"ok":true}' });
52
+ expect(seen).toEqual([{
53
+ href: 'https://api.clickup.com/api/v2/list/1/task',
54
+ auth: 'Bearer tok-1',
55
+ }]);
56
+ } finally {
57
+ globalThis.fetch = originalFetch;
58
+ await fs.rm(dir, { recursive: true, force: true });
59
+ }
60
+ });
61
+
62
+ test('sends a pasted token as the Authorization header', async () => {
63
+ const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'oc-guest-req-'));
64
+ const persistPath = guestAuthPersistPath(dir);
65
+ await patchGuestAuth('clickup', { accessToken: 'pk_tok', target: { apiOrigin: 'https://api.clickup.com' } }, persistPath);
66
+ const originalFetch = globalThis.fetch;
67
+ const seen = [];
68
+ globalThis.fetch = async (url, init) => {
69
+ seen.push({ href: String(url), auth: init.headers.Authorization });
70
+ return new Response('{"ok":true}', { status: 200 });
71
+ };
72
+ try {
73
+ const result = await proxyGuestRequest({
74
+ guest: {
75
+ id: 'clickup',
76
+ integration: {
77
+ name: 'ClickUp',
78
+ description: 'Tasks',
79
+ token: { apiOrigin: 'https://api.clickup.com' },
80
+ },
81
+ },
82
+ persistPath,
83
+ method: 'GET',
84
+ path: '/api/v2/user',
85
+ });
86
+ expect(result).toEqual({ status: 200, body: '{"ok":true}' });
87
+ expect(seen).toEqual([{
88
+ href: 'https://api.clickup.com/api/v2/user',
89
+ auth: 'pk_tok',
90
+ }]);
91
+ } finally {
92
+ globalThis.fetch = originalFetch;
93
+ await fs.rm(dir, { recursive: true, force: true });
94
+ }
95
+ });
96
+
97
+ test('drops tokens when a 401 cannot refresh', async () => {
98
+ const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'oc-guest-req-'));
99
+ const persistPath = guestAuthPersistPath(dir);
100
+ await patchGuestAuth('clickup', { accessToken: 'tok-old', target: credentialTarget(clickupGuest.integration) }, persistPath);
101
+ const originalFetch = globalThis.fetch;
102
+ globalThis.fetch = async () => new Response('nope', { status: 401 });
103
+ try {
104
+ await proxyGuestRequest({
105
+ guest: clickupGuest,
106
+ persistPath,
107
+ method: 'GET',
108
+ path: '/api/v2/user',
109
+ });
110
+ throw new Error('should have thrown');
111
+ } catch (error) {
112
+ expect(error.code).toBe('DISCONNECTED');
113
+ }
114
+ expect((await getGuestAuth('clickup', persistPath))?.accessToken).toBeUndefined();
115
+ globalThis.fetch = originalFetch;
116
+ await fs.rm(dir, { recursive: true, force: true });
117
+ });
118
+
119
+ test('refuses a disconnected guest', async () => {
120
+ const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'oc-guest-req-'));
121
+ const persistPath = guestAuthPersistPath(dir);
122
+ try {
123
+ await proxyGuestRequest({
124
+ guest: clickupGuest,
125
+ persistPath,
126
+ method: 'GET',
127
+ path: '/api/v2/user',
128
+ });
129
+ throw new Error('should have thrown');
130
+ } catch (error) {
131
+ expect(error.code).toBe('DISCONNECTED');
132
+ }
133
+ await fs.rm(dir, { recursive: true, force: true });
134
+ });
135
+ });
136
+
137
+ describe('credential target', () => {
138
+ test('drops a token minted for another API origin instead of sending it', async () => {
139
+ const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'oc-guest-req-'));
140
+ const persistPath = guestAuthPersistPath(dir);
141
+ await patchGuestAuth('clickup', { accessToken: 'tok-1', target: { apiOrigin: 'https://api.old.example' } }, persistPath);
142
+ const originalFetch = globalThis.fetch;
143
+ let requested = 0;
144
+ globalThis.fetch = async () => {
145
+ requested += 1;
146
+ return new Response('{"ok":true}', { status: 200 });
147
+ };
148
+ try {
149
+ await expect(proxyGuestRequest({ guest: clickupGuest, persistPath, method: 'GET', path: '/api/v2/user' }))
150
+ .rejects.toMatchObject({ code: 'DISCONNECTED' });
151
+ expect(requested).toBe(0);
152
+ expect((await getGuestAuth('clickup', persistPath))?.accessToken).toBeUndefined();
153
+ } finally {
154
+ globalThis.fetch = originalFetch;
155
+ await fs.rm(dir, { recursive: true, force: true });
156
+ }
157
+ });
158
+
159
+ test('a token without a recorded target never counts', async () => {
160
+ const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'oc-guest-req-'));
161
+ const persistPath = guestAuthPersistPath(dir);
162
+ await patchGuestAuth('clickup', { accessToken: 'tok-1' }, persistPath);
163
+ try {
164
+ await expect(proxyGuestRequest({ guest: clickupGuest, persistPath, method: 'GET', path: '/api/v2/user' }))
165
+ .rejects.toMatchObject({ code: 'DISCONNECTED' });
166
+ } finally {
167
+ await fs.rm(dir, { recursive: true, force: true });
168
+ }
169
+ });
170
+ });
171
+
172
+ describe('a 401 while the user reconnects', () => {
173
+ test('drops only the tokens the request was refused with, not the new connection', async () => {
174
+ const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'oc-guest-req-'));
175
+ const persistPath = guestAuthPersistPath(dir);
176
+ const target = credentialTarget(clickupGuest.integration);
177
+ await patchGuestAuth('clickup', {
178
+ clientId: 'app-id', clientSecret: 'app-secret', clientTarget: target,
179
+ accessToken: 'access-a', refreshToken: 'refresh-a', target,
180
+ }, persistPath);
181
+ const originalFetch = globalThis.fetch;
182
+ let releaseRefresh;
183
+ const refreshGate = new Promise((resolve) => { releaseRefresh = resolve; });
184
+ globalThis.fetch = async (url) => {
185
+ if (String(url) === 'https://api.clickup.com/api/v2/oauth/token') {
186
+ await refreshGate;
187
+ return new Response(JSON.stringify({ access_token: 'access-a2', refresh_token: 'refresh-a2', token_type: 'Bearer', expires_in: 3600 }), { status: 200, headers: { 'Content-Type': 'application/json' } });
188
+ }
189
+ return new Response('nope', { status: 401 });
190
+ };
191
+ try {
192
+ const request = proxyGuestRequest({ guest: clickupGuest, persistPath, method: 'GET', path: '/api/v2/user' });
193
+ // The user connects again while the refresh is in flight.
194
+ await new Promise((resolve) => setTimeout(resolve, 20));
195
+ await patchGuestAuth('clickup', { accessToken: 'access-b', refreshToken: 'refresh-b', target }, persistPath);
196
+ releaseRefresh();
197
+ await expect(request).rejects.toMatchObject({ code: 'DISCONNECTED' });
198
+ const stored = await getGuestAuth('clickup', persistPath);
199
+ expect(stored.accessToken).toBe('access-b');
200
+ expect(stored.refreshToken).toBe('refresh-b');
201
+ } finally {
202
+ globalThis.fetch = originalFetch;
203
+ await fs.rm(dir, { recursive: true, force: true });
204
+ }
205
+ });
206
+ });