@openchamber/web 1.21.1 → 1.22.1

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 (175) hide show
  1. package/dist/assets/{CodeMirrorCommentWidgets-BGj8TJxW.js → CodeMirrorCommentWidgets-BO_2OW4E.js} +1 -1
  2. package/dist/assets/DiffView-CNnDxRmO.js +4 -0
  3. package/dist/assets/DirtyBranchSwitchDialog-jADfXQnR.js +1 -0
  4. package/dist/assets/FilesView-8wdiG2mj.js +17 -0
  5. package/dist/assets/GitView-Cv2xH-S6.js +6 -0
  6. package/dist/assets/{JsonTreeView-CggOsLot.js → JsonTreeView-DUiIpERC.js} +1 -1
  7. package/dist/assets/LinearIssuesView-CoZ7pQN5.js +1 -0
  8. package/dist/assets/MarkdownImageGallery-DffjmciG.js +1 -0
  9. package/dist/assets/MarkdownRendererImpl-DxqdYDAx.js +12 -0
  10. package/dist/assets/OnboardingScreen-CTZe6HZn.js +1 -0
  11. package/dist/assets/{PatchDiff-DJYMUiXA.js → PatchDiff-BymkP9JA.js} +1 -1
  12. package/dist/assets/{PierreDiffViewer-CY2bx_SB.js → PierreDiffViewer-BvwtCxW4.js} +3 -3
  13. package/dist/assets/{PlanView-PkiOY_9H.js → PlanView-C1qvAm7m.js} +3 -3
  14. package/dist/assets/SettingsView-xGONHFGS.js +28 -0
  15. package/dist/assets/{SettingsWindow-oyOm2fHR.js → SettingsWindow-COzgfFxM.js} +1 -1
  16. package/dist/assets/{ToolOutputDialog-lKyeQTPL.js → ToolOutputDialog-Dlhh_-oe.js} +6 -6
  17. package/dist/assets/ToolPartDiffPreview-b9joNtXH.js +8 -0
  18. package/dist/assets/{VirtualizedFileDiff-BP7ebDFF.js → VirtualizedFileDiff-vqW3KKo5.js} +1 -1
  19. package/dist/assets/WalkthroughView-D-HFgdmM.js +4 -0
  20. package/dist/assets/{computeVirtualFileMetrics-CZ0xPeaT.js → computeVirtualFileMetrics-DUnyszhW.js} +1 -1
  21. package/dist/assets/{contextPanelEmbeddedChat-C9abMBTb.js → contextPanelEmbeddedChat-CV-DwAiX.js} +1 -1
  22. package/dist/assets/{de-CEoCz3j6.js → de-Cv_4FoXm.js} +6 -5
  23. package/dist/assets/es-BIWNEbnS.js +16 -0
  24. package/dist/assets/{fr-CS7Csln9.js → fr-SD3OfMHJ.js} +6 -5
  25. package/dist/assets/index-CHUT_bDS.css +1 -0
  26. package/dist/assets/index-DDInLQDd.js +40 -0
  27. package/dist/assets/{index-CaPAjwXQ.js → index-DLiD86ge.js} +1 -1
  28. package/dist/assets/{isDiffPlainText-ZBvDxhVB.js → isDiffPlainText-TNageFjy.js} +1 -1
  29. package/dist/assets/{isFilePlainText-DiLRrkJr.js → isFilePlainText-JPmZOYSp.js} +1 -1
  30. package/dist/assets/{ja-vnwqLcx6.js → ja-D7I9HofF.js} +7 -6
  31. package/dist/assets/{ko-CXuSfZv5.js → ko-BsVJM_PQ.js} +7 -6
  32. package/dist/assets/{main-D5RWr6sJ.js → main-DCB4JgVe.js} +2 -2
  33. package/dist/assets/main-De8Z5w49.js +928 -0
  34. package/dist/assets/markdownCore-BoQVG5-a.js +3 -0
  35. package/dist/assets/{miniChat-CqR3icsk.js → miniChat-BtV7N5OF.js} +2 -2
  36. package/dist/assets/{mobile-RN_UTTlB.js → mobile-DZV_sfX7.js} +2 -2
  37. package/dist/assets/{patchFileDiff-CjRR5ESs.js → patchFileDiff-Chmiz6hq.js} +4 -4
  38. package/dist/assets/{pl-BJxry0Mi.js → pl-XR9Bs8Be.js} +6 -5
  39. package/dist/assets/pt-BR-Due4be8g.js +16 -0
  40. package/dist/assets/{renderElectronMiniChatApp-1wC94fQK.js → renderElectronMiniChatApp-8tXADjfl.js} +2 -2
  41. package/dist/assets/renderMobileApp-Cji7Hma5.js +25 -0
  42. package/dist/assets/runtimeEndpointReset-gWt3Lb5D.js +16 -0
  43. package/dist/assets/scheduledTasksApi-9e-UX6im.js +1 -0
  44. package/dist/assets/{shikiHighlight-fGmFkxAM.js → shikiHighlight-CRpCl7UT.js} +1 -1
  45. package/dist/assets/tr-DBJcAv4y.js +7 -0
  46. package/dist/assets/uk-COYh2BaN.js +16 -0
  47. package/dist/assets/{useAppFontEffects-KP8_yiGW.js → useAppFontEffects-CCgvcQ6u.js} +178 -170
  48. package/dist/assets/useInlineCommentController-BkfXnjNZ.js +2 -0
  49. package/dist/assets/useRootScrollLock-KzOUytpn.js +1 -0
  50. package/dist/assets/{vendor-codemirror-language-data-DrTPkHl4.js → vendor-codemirror-language-data-BxpoC5Fp.js} +2 -2
  51. package/dist/assets/vendor-zod-DVWEh-IS.js +39 -0
  52. package/dist/assets/{web-components-DOXvU-tB.js → web-components-B6QgbmTo.js} +4 -4
  53. package/dist/assets/zh-CN-Cjk6lzhc.js +16 -0
  54. package/dist/assets/{zh-TW-Dv8yyhN1.js → zh-TW-Dk0dOg03.js} +6 -5
  55. package/dist/index.html +5 -5
  56. package/dist/mini-chat.html +4 -4
  57. package/dist/mobile.html +4 -4
  58. package/package.json +2 -2
  59. package/server/index.js +35 -4
  60. package/server/lib/agent-tool/runtime.js +2 -19
  61. package/server/lib/dev-tunnel/DOCUMENTATION.md +13 -4
  62. package/server/lib/dev-tunnel/runtime.js +4 -4
  63. package/server/lib/dev-tunnel/tunnel.test.js +20 -0
  64. package/server/lib/dictation/DOCUMENTATION.md +19 -6
  65. package/server/lib/dictation/local/model-catalog.js +220 -0
  66. package/server/lib/dictation/local/model-catalog.test.js +42 -0
  67. package/server/lib/dictation/local/sherpa-tts.js +41 -20
  68. package/server/lib/dictation/local/worker-process.js +2 -0
  69. package/server/lib/dictation/runtime.js +4 -0
  70. package/server/lib/dictation/service.js +27 -4
  71. package/server/lib/fs/DOCUMENTATION.md +4 -0
  72. package/server/lib/fs/routes.js +129 -0
  73. package/server/lib/fs/routes.test.js +219 -0
  74. package/server/lib/git/DOCUMENTATION.md +3 -1
  75. package/server/lib/git/routes.js +16 -0
  76. package/server/lib/git/service.js +97 -17
  77. package/server/lib/git/service.test.js +153 -0
  78. package/server/lib/linear/DOCUMENTATION.md +97 -0
  79. package/server/lib/linear/OAUTH-BROKER-HANDOFF.md +91 -0
  80. package/server/lib/linear/auth.js +436 -0
  81. package/server/lib/linear/auth.test.js +242 -0
  82. package/server/lib/linear/client.js +191 -0
  83. package/server/lib/linear/index.js +61 -0
  84. package/server/lib/linear/issues.js +499 -0
  85. package/server/lib/linear/issues.test.js +512 -0
  86. package/server/lib/linear/mapping.js +188 -0
  87. package/server/lib/linear/mapping.test.js +147 -0
  88. package/server/lib/linear/oauth.js +345 -0
  89. package/server/lib/linear/oauth.test.js +166 -0
  90. package/server/lib/linear/parse.js +23 -0
  91. package/server/lib/linear/routes.js +432 -0
  92. package/server/lib/linear/routes.test.js +661 -0
  93. package/server/lib/linear/status-runtime.js +64 -0
  94. package/server/lib/linear/status-runtime.test.js +167 -0
  95. package/server/lib/linear/status.js +280 -0
  96. package/server/lib/linear/status.test.js +271 -0
  97. package/server/lib/linear/teams.js +72 -0
  98. package/server/lib/linear/teams.test.js +94 -0
  99. package/server/lib/mcp-reconnect/DOCUMENTATION.md +55 -0
  100. package/server/lib/mcp-reconnect/runtime.js +122 -0
  101. package/server/lib/mcp-reconnect/runtime.test.js +140 -0
  102. package/server/lib/message-queue/DOCUMENTATION.md +148 -0
  103. package/server/lib/message-queue/runtime.js +818 -0
  104. package/server/lib/message-queue/runtime.test.js +426 -0
  105. package/server/lib/openchamber-sessions/routes.js +102 -0
  106. package/server/lib/openchamber-sessions/routes.test.js +88 -0
  107. package/server/lib/opencode/DOCUMENTATION.md +5 -1
  108. package/server/lib/opencode/core-routes.js +1 -0
  109. package/server/lib/opencode/feature-routes-runtime.js +5 -0
  110. package/server/lib/opencode/managed-plugin-config.js +35 -0
  111. package/server/lib/opencode/settings-runtime.js +2 -2
  112. package/server/lib/opencode/settings-runtime.test.js +29 -0
  113. package/server/lib/opencode/shutdown-runtime.js +2 -0
  114. package/server/lib/opencode/static-routes-runtime.js +3 -3
  115. package/server/lib/projects/project-config.js +33 -5
  116. package/server/lib/projects/project-config.test.js +85 -0
  117. package/server/lib/quota/DOCUMENTATION.md +2 -1
  118. package/server/lib/quota/credentials/providers.js +4 -0
  119. package/server/lib/quota/credentials/store.js +1 -1
  120. package/server/lib/quota/credentials/store.test.js +4 -4
  121. package/server/lib/quota/providers/exe-dev.js +75 -0
  122. package/server/lib/quota/providers/exe-dev.test.js +47 -0
  123. package/server/lib/quota/providers/index.js +7 -0
  124. package/server/lib/quota/providers/opencode-go.js +1 -0
  125. package/server/lib/quota/providers/opencode-go.test.js +5 -1
  126. package/server/lib/quota/routes.js +2 -0
  127. package/server/lib/relay/DOCUMENTATION.md +1 -1
  128. package/server/lib/relay/tunnel-host.js +3 -1
  129. package/server/lib/relay/tunnel-host.test.js +9 -1
  130. package/server/lib/scheduled-tasks/DOCUMENTATION.md +7 -0
  131. package/server/lib/security/request-security.js +21 -6
  132. package/server/lib/security/request-security.test.js +42 -8
  133. package/server/lib/session-assist/runtime.js +1 -0
  134. package/server/lib/session-goal/create.js +3 -1
  135. package/server/lib/session-goal/runtime.js +1 -0
  136. package/server/lib/small-model/DOCUMENTATION.md +4 -0
  137. package/server/lib/small-model/call.js +8 -2
  138. package/server/lib/small-model/call.test.js +23 -0
  139. package/server/lib/small-model/index.js +2 -1
  140. package/server/lib/small-model/routes.js +2 -1
  141. package/server/lib/system-prompt/runtime.js +2 -21
  142. package/server/lib/terminal/runtime.js +30 -7
  143. package/server/lib/tts/DOCUMENTATION.md +1 -0
  144. package/server/lib/tts/language-detect.js +210 -0
  145. package/server/lib/tts/language-detect.test.js +76 -0
  146. package/server/lib/tts/routes.js +23 -1
  147. package/server/lib/tts/routes.test.js +26 -0
  148. package/server/lib/ui-auth/ui-auth.js +4 -1
  149. package/server/lib/ui-auth/ui-auth.test.js +16 -0
  150. package/server/lib/walkthrough/index.js +1 -0
  151. package/dist/assets/CommitSection-DwGiXyjf.js +0 -1
  152. package/dist/assets/DiffView-Db6bjVv1.js +0 -4
  153. package/dist/assets/FilesView-DQOa7Lgz.js +0 -17
  154. package/dist/assets/GitView-CHzJxTi0.js +0 -6
  155. package/dist/assets/MarkdownImageGallery-DoZpaHev.js +0 -1
  156. package/dist/assets/MarkdownRendererImpl-B5Y5xCOz.js +0 -12
  157. package/dist/assets/OnboardingScreen-C98d69Fp.js +0 -1
  158. package/dist/assets/SettingsView-D-K3ELCM.js +0 -28
  159. package/dist/assets/ToolPartDiffPreview-C7kBQNAA.js +0 -8
  160. package/dist/assets/WalkthroughView-MuJodAHD.js +0 -4
  161. package/dist/assets/es-DwyXCqaL.js +0 -15
  162. package/dist/assets/index-CUKcHnUA.css +0 -1
  163. package/dist/assets/index-DIvDEKE-.js +0 -7
  164. package/dist/assets/main-DLlfDjQ_.js +0 -926
  165. package/dist/assets/markdownCore-CcTcLsQF.js +0 -3
  166. package/dist/assets/pt-BR-DxLmkhdv.js +0 -15
  167. package/dist/assets/renderMobileApp-ltva2Plg.js +0 -25
  168. package/dist/assets/runtimeEndpointReset-D6SmNZaP.js +0 -15
  169. package/dist/assets/scheduledTasksApi-mjMDet_H.js +0 -1
  170. package/dist/assets/tr-5Mk_hQ2Q.js +0 -6
  171. package/dist/assets/uk-ClgQjmlw.js +0 -15
  172. package/dist/assets/useInlineCommentController-HIjtuhxD.js +0 -2
  173. package/dist/assets/useRootScrollLock-BSiQ5F75.js +0 -1
  174. package/dist/assets/vendor-zod-BLfBSgBh.js +0 -39
  175. package/dist/assets/zh-CN-Ba6y8H-i.js +0 -15
@@ -0,0 +1,661 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
+ import express from 'express';
3
+ import request from 'supertest';
4
+ import fs from 'fs';
5
+ import os from 'os';
6
+ import path from 'path';
7
+ import { registerLinearRoutes } from './routes.js';
8
+ import { setLinearAuth, setLinearSessionCommentsEnabled } from './auth.js';
9
+
10
+ const makeTempDir = () => fs.mkdtempSync(path.join(os.tmpdir(), 'openchamber-linear-routes-'));
11
+
12
+ const createApp = () => {
13
+ const app = express();
14
+ registerLinearRoutes(app);
15
+ return app;
16
+ };
17
+
18
+ const jsonResponse = (payload, status = 200) => new Response(JSON.stringify(payload), {
19
+ status,
20
+ headers: { 'Content-Type': 'application/json' },
21
+ });
22
+
23
+ describe('Linear auth routes', () => {
24
+ let dataDir;
25
+ let previousDataDir;
26
+
27
+ beforeEach(() => {
28
+ previousDataDir = process.env.OPENCHAMBER_DATA_DIR;
29
+ dataDir = makeTempDir();
30
+ process.env.OPENCHAMBER_DATA_DIR = dataDir;
31
+ process.env.OPENCHAMBER_PORT = '3001';
32
+ process.env.OPENCHAMBER_LINEAR_REDIRECT_URI = 'http://127.0.0.1:3001/linear/oauth/callback';
33
+ delete process.env.OPENCHAMBER_LINEAR_CLIENT_ID;
34
+ });
35
+
36
+ afterEach(() => {
37
+ vi.unstubAllGlobals();
38
+ if (previousDataDir === undefined) {
39
+ delete process.env.OPENCHAMBER_DATA_DIR;
40
+ } else {
41
+ process.env.OPENCHAMBER_DATA_DIR = previousDataDir;
42
+ }
43
+ delete process.env.OPENCHAMBER_PORT;
44
+ delete process.env.OPENCHAMBER_LINEAR_REDIRECT_URI;
45
+ fs.rmSync(dataDir, { recursive: true, force: true });
46
+ });
47
+
48
+ it('starts authorization and completes it from the public callback', async () => {
49
+ const app = createApp();
50
+ const start = await request(app)
51
+ .post('/api/linear/auth/start')
52
+ .send({ origin: 'desktop' })
53
+ .expect(200);
54
+
55
+ expect(start.body.authorizationUrl).toContain('https://linear.app/oauth/authorize');
56
+ const state = new URL(start.body.authorizationUrl).searchParams.get('state');
57
+
58
+ vi.stubGlobal('fetch', vi.fn(async (url) => {
59
+ const target = String(url);
60
+ if (target === 'https://api.linear.app/oauth/token') {
61
+ return jsonResponse({
62
+ access_token: 'access-1',
63
+ refresh_token: 'refresh-1',
64
+ token_type: 'Bearer',
65
+ expires_in: 86399,
66
+ scope: 'read,write,comments:create',
67
+ });
68
+ }
69
+ if (target === 'https://api.linear.app/graphql') {
70
+ return jsonResponse({
71
+ data: {
72
+ viewer: {
73
+ id: 'user-1',
74
+ name: 'Ada',
75
+ displayName: 'Ada Lovelace',
76
+ email: 'ada@example.com',
77
+ avatarUrl: 'https://example.com/a.png',
78
+ },
79
+ organization: { id: 'org-1', name: 'OpenChamber', urlKey: 'openchamber' },
80
+ },
81
+ });
82
+ }
83
+ throw new Error(`unexpected fetch: ${target}`);
84
+ }));
85
+
86
+ const callback = await request(app)
87
+ .get('/linear/oauth/callback')
88
+ .query({ state, code: 'auth-code' })
89
+ .expect(200);
90
+
91
+ expect(callback.text).toContain('Authorization Complete');
92
+ expect(callback.text).toContain('openchamber://focus/linear-auth');
93
+
94
+ const status = await request(app).get('/api/linear/auth/status').expect(200);
95
+ expect(status.body.connected).toBe(true);
96
+ expect(status.body.user).toEqual({
97
+ id: 'user-1',
98
+ name: 'Ada',
99
+ displayName: 'Ada Lovelace',
100
+ email: 'ada@example.com',
101
+ avatarUrl: 'https://example.com/a.png',
102
+ });
103
+ expect(status.body.organization).toEqual({ id: 'org-1', name: 'OpenChamber', urlKey: 'openchamber' });
104
+ expect(status.body.scope).toBe('read,write,comments:create');
105
+ expect(status.body.workspaces).toEqual([{
106
+ id: 'org-1',
107
+ name: 'OpenChamber',
108
+ urlKey: 'openchamber',
109
+ current: true,
110
+ user: {
111
+ id: 'user-1',
112
+ name: 'Ada',
113
+ displayName: 'Ada Lovelace',
114
+ email: 'ada@example.com',
115
+ avatarUrl: 'https://example.com/a.png',
116
+ },
117
+ authorizedAt: expect.any(Number),
118
+ }]);
119
+ expect(JSON.stringify(status.body)).not.toContain('access-1');
120
+ expect(JSON.stringify(status.body)).not.toContain('refresh-1');
121
+
122
+ const again = await request(app).get('/api/linear/auth/status').expect(200);
123
+ expect(again.body.workspaces[0].authorizedAt).toBe(status.body.workspaces[0].authorizedAt);
124
+ });
125
+
126
+ it('never exchanges a code whose state is unknown', async () => {
127
+ const tokenFetch = vi.fn();
128
+ vi.stubGlobal('fetch', tokenFetch);
129
+ const app = createApp();
130
+ const response = await request(app)
131
+ .get('/linear/oauth/callback')
132
+ .query({ state: 'forged', code: 'attacker-code' })
133
+ .expect(400);
134
+ expect(tokenFetch).not.toHaveBeenCalled();
135
+ expect(response.text).toContain('Authorization Failed');
136
+ expect(response.text).not.toContain('openchamber://');
137
+ });
138
+
139
+ it('omits the desktop deep link for flows started outside the desktop shell', async () => {
140
+ const app = createApp();
141
+ const start = await request(app)
142
+ .post('/api/linear/auth/start')
143
+ .send({ origin: 'web' })
144
+ .expect(200);
145
+ const state = new URL(start.body.authorizationUrl).searchParams.get('state');
146
+
147
+ vi.stubGlobal('fetch', vi.fn(async (url) => {
148
+ const target = String(url);
149
+ if (target.includes('/oauth/token')) {
150
+ return jsonResponse({
151
+ access_token: 'access-1',
152
+ refresh_token: 'refresh-1',
153
+ expires_in: 86399,
154
+ });
155
+ }
156
+ return jsonResponse({
157
+ data: { viewer: { id: 'user-1', name: 'Ada' }, organization: null },
158
+ });
159
+ }));
160
+
161
+ const response = await request(app)
162
+ .get('/linear/oauth/callback')
163
+ .query({ state, code: 'auth-code' })
164
+ .expect(200);
165
+ expect(response.text).not.toContain('openchamber://');
166
+ });
167
+
168
+ it('disconnects and revokes the refresh token', async () => {
169
+ const app = createApp();
170
+ const start = await request(app).post('/api/linear/auth/start').send({}).expect(200);
171
+ const state = new URL(start.body.authorizationUrl).searchParams.get('state');
172
+ const fetchMock = vi.fn(async (url) => {
173
+ const target = String(url);
174
+ if (target.includes('/oauth/token')) {
175
+ return jsonResponse({
176
+ access_token: 'access-1',
177
+ refresh_token: 'refresh-1',
178
+ expires_in: 86399,
179
+ });
180
+ }
181
+ if (target.includes('/graphql')) {
182
+ return jsonResponse({ data: { viewer: { id: 'user-1', name: 'Ada' } } });
183
+ }
184
+ if (target.includes('/oauth/revoke')) {
185
+ return new Response('', { status: 200 });
186
+ }
187
+ throw new Error(`unexpected fetch: ${target}`);
188
+ });
189
+ vi.stubGlobal('fetch', fetchMock);
190
+
191
+ await request(app).get('/linear/oauth/callback').query({ state, code: 'auth-code' }).expect(200);
192
+ await request(app).delete('/api/linear/auth').expect(200);
193
+
194
+ const revokeCall = fetchMock.mock.calls.find(([url]) => String(url).includes('/oauth/revoke'));
195
+ expect(revokeCall).toBeTruthy();
196
+ const body = new URLSearchParams(revokeCall[1].body);
197
+ expect(body.get('token')).toBe('refresh-1');
198
+ expect(body.get('token_type_hint')).toBe('refresh_token');
199
+
200
+ const status = await request(app).get('/api/linear/auth/status').expect(200);
201
+ expect(status.body).toEqual({ connected: false });
202
+ });
203
+
204
+ it('stores a second workspace, switches current, and disconnects only that one', async () => {
205
+ const app = createApp();
206
+
207
+ const startA = await request(app).post('/api/linear/auth/start').send({}).expect(200);
208
+ const stateA = new URL(startA.body.authorizationUrl).searchParams.get('state');
209
+ vi.stubGlobal('fetch', vi.fn(async (url) => {
210
+ const target = String(url);
211
+ if (target.includes('/oauth/token')) {
212
+ return jsonResponse({
213
+ access_token: 'access-a',
214
+ refresh_token: 'refresh-a',
215
+ expires_in: 86399,
216
+ scope: 'read,write,comments:create',
217
+ });
218
+ }
219
+ if (target.includes('/graphql')) {
220
+ return jsonResponse({
221
+ data: {
222
+ viewer: { id: 'user-a', name: 'Ada' },
223
+ organization: { id: 'org-a', name: 'Alpha', urlKey: 'alpha' },
224
+ },
225
+ });
226
+ }
227
+ throw new Error(`unexpected fetch: ${target}`);
228
+ }));
229
+ await request(app).get('/linear/oauth/callback').query({ state: stateA, code: 'code-a' }).expect(200);
230
+
231
+ const startB = await request(app).post('/api/linear/auth/start').send({}).expect(200);
232
+ const stateB = new URL(startB.body.authorizationUrl).searchParams.get('state');
233
+ vi.stubGlobal('fetch', vi.fn(async (url) => {
234
+ const target = String(url);
235
+ if (target.includes('/oauth/token')) {
236
+ return jsonResponse({
237
+ access_token: 'access-b',
238
+ refresh_token: 'refresh-b',
239
+ expires_in: 86399,
240
+ scope: 'read,write,comments:create',
241
+ });
242
+ }
243
+ if (target.includes('/graphql')) {
244
+ return jsonResponse({
245
+ data: {
246
+ viewer: { id: 'user-b', name: 'Ben' },
247
+ organization: { id: 'org-b', name: 'Beta', urlKey: 'beta' },
248
+ },
249
+ });
250
+ }
251
+ if (target.includes('/oauth/revoke')) {
252
+ return new Response('', { status: 200 });
253
+ }
254
+ throw new Error(`unexpected fetch: ${target}`);
255
+ }));
256
+ await request(app).get('/linear/oauth/callback').query({ state: stateB, code: 'code-b' }).expect(200);
257
+
258
+ const both = await request(app).get('/api/linear/auth/status').expect(200);
259
+ expect(both.body.organization.id).toBe('org-b');
260
+ expect(both.body.workspaces).toHaveLength(2);
261
+
262
+ await request(app).post('/api/linear/auth/activate').send({}).expect(400);
263
+ await request(app).post('/api/linear/auth/activate').send({ organizationId: 'missing' }).expect(404);
264
+
265
+ const activated = await request(app)
266
+ .post('/api/linear/auth/activate')
267
+ .send({ organizationId: 'org-a' })
268
+ .expect(200);
269
+ expect(activated.body.organization.id).toBe('org-a');
270
+ expect(activated.body.workspaces.find((entry) => entry.id === 'org-a').current).toBe(true);
271
+ expect(activated.body.workspaces.find((entry) => entry.id === 'org-b').current).toBe(false);
272
+
273
+ await request(app).delete('/api/linear/auth').expect(200);
274
+ const remaining = await request(app).get('/api/linear/auth/status').expect(200);
275
+ expect(remaining.body.connected).toBe(true);
276
+ expect(remaining.body.organization.id).toBe('org-b');
277
+ expect(remaining.body.workspaces).toHaveLength(1);
278
+ expect(remaining.body.workspaces[0].id).toBe('org-b');
279
+ });
280
+
281
+ it('lists and gets issues through authenticated routes without leaking tokens', async () => {
282
+ setLinearAuth({
283
+ accessToken: 'access-1',
284
+ refreshToken: 'refresh-1',
285
+ tokenType: 'Bearer',
286
+ expiresAt: Date.now() + 86_400_000,
287
+ scope: 'read',
288
+ });
289
+ vi.stubGlobal('fetch', vi.fn(async (_url, options) => {
290
+ const body = JSON.parse(options.body);
291
+ if (body.query.includes('GetLinearIssue')) {
292
+ return jsonResponse({
293
+ data: {
294
+ issue: {
295
+ id: 'issue-1',
296
+ identifier: 'ENG-12',
297
+ title: 'Broken login',
298
+ url: 'https://linear.app/openchamber/issue/ENG-12',
299
+ state: { name: 'Todo', type: 'unstarted' },
300
+ assignee: null,
301
+ description: 'Users cannot sign in.',
302
+ comments: { nodes: [] },
303
+ },
304
+ },
305
+ });
306
+ }
307
+ return jsonResponse({
308
+ data: {
309
+ issues: {
310
+ nodes: [{
311
+ id: 'issue-1',
312
+ identifier: 'ENG-12',
313
+ title: 'Broken login',
314
+ url: 'https://linear.app/openchamber/issue/ENG-12',
315
+ state: { name: 'Todo', type: 'unstarted' },
316
+ assignee: null,
317
+ }],
318
+ pageInfo: { hasNextPage: false, endCursor: null },
319
+ },
320
+ },
321
+ });
322
+ }));
323
+
324
+ const app = createApp();
325
+ const list = await request(app).get('/api/linear/issues/list').expect(200);
326
+ expect(list.body.connected).toBe(true);
327
+ expect(list.body.issues).toHaveLength(1);
328
+ expect(JSON.stringify(list.body)).not.toContain('access-1');
329
+
330
+ const missing = await request(app).get('/api/linear/issues/get').expect(400);
331
+ expect(missing.body.error).toBe('id is required');
332
+
333
+ const got = await request(app).get('/api/linear/issues/get').query({ id: 'ENG-12' }).expect(200);
334
+ expect(got.body.issue.identifier).toBe('ENG-12');
335
+ expect(got.body.issue.description).toBe('Users cannot sign in.');
336
+ expect(got.body.issue.state).toEqual({ id: null, name: 'Todo', type: 'unstarted' });
337
+ });
338
+
339
+ it('passes list filters from query params to Linear', async () => {
340
+ setLinearAuth({
341
+ accessToken: 'access-1',
342
+ refreshToken: 'refresh-1',
343
+ tokenType: 'Bearer',
344
+ expiresAt: Date.now() + 86_400_000,
345
+ scope: 'read',
346
+ });
347
+ vi.stubGlobal('fetch', vi.fn(async (_url, options) => {
348
+ const body = JSON.parse(options.body);
349
+ expect(body.variables.filter).toEqual({
350
+ state: { type: { eq: 'completed' } },
351
+ assignee: { isMe: { eq: true } },
352
+ team: { id: { eq: 'team-eng' } },
353
+ priority: { eq: 1 },
354
+ });
355
+ return jsonResponse({
356
+ data: {
357
+ issues: {
358
+ nodes: [],
359
+ pageInfo: { hasNextPage: false, endCursor: null },
360
+ },
361
+ },
362
+ });
363
+ }));
364
+
365
+ const app = createApp();
366
+ const list = await request(app).get('/api/linear/issues/list').query({
367
+ status: 'completed',
368
+ assignee: 'me',
369
+ teamId: 'team-eng',
370
+ priority: 'urgent',
371
+ }).expect(200);
372
+ expect(list.body.connected).toBe(true);
373
+ expect(list.body.issues).toEqual([]);
374
+ });
375
+
376
+ it('lists workflow states and updates issue status without leaking tokens', async () => {
377
+ setLinearAuth({
378
+ accessToken: 'access-1',
379
+ refreshToken: 'refresh-1',
380
+ tokenType: 'Bearer',
381
+ expiresAt: Date.now() + 86_400_000,
382
+ scope: 'write',
383
+ });
384
+ vi.stubGlobal('fetch', vi.fn(async (_url, options) => {
385
+ const body = JSON.parse(options.body);
386
+ if (body.query.includes('TeamWorkflowStates')) {
387
+ expect(body.variables.id).toBe('team-eng');
388
+ expect(options.headers.Authorization).toBe('Bearer access-1');
389
+ return jsonResponse({
390
+ data: {
391
+ team: {
392
+ states: {
393
+ nodes: [
394
+ { id: 'state-todo', name: 'Todo', type: 'unstarted', position: 1 },
395
+ { id: 'state-done', name: 'Done', type: 'completed', position: 2 },
396
+ ],
397
+ },
398
+ },
399
+ },
400
+ });
401
+ }
402
+ expect(body.query).toContain('mutation IssueUpdate');
403
+ expect(body.variables).toEqual({
404
+ id: 'issue-uuid-1',
405
+ input: { stateId: 'state-done' },
406
+ });
407
+ return jsonResponse({
408
+ data: {
409
+ issueUpdate: {
410
+ success: true,
411
+ issue: {
412
+ id: 'issue-uuid-1',
413
+ identifier: 'ENG-12',
414
+ title: 'Broken login',
415
+ url: 'https://linear.app/openchamber/issue/ENG-12',
416
+ state: { id: 'state-done', name: 'Done', type: 'completed' },
417
+ assignee: null,
418
+ description: null,
419
+ comments: { nodes: [] },
420
+ },
421
+ },
422
+ },
423
+ });
424
+ }));
425
+
426
+ const app = createApp();
427
+ const missingTeam = await request(app).get('/api/linear/issues/states').expect(400);
428
+ expect(missingTeam.body.error).toBe('teamId is required');
429
+
430
+ const states = await request(app).get('/api/linear/issues/states').query({ teamId: 'team-eng' }).expect(200);
431
+ expect(states.body.connected).toBe(true);
432
+ expect(states.body.states).toEqual([
433
+ { id: 'state-todo', name: 'Todo', type: 'unstarted', position: 1 },
434
+ { id: 'state-done', name: 'Done', type: 'completed', position: 2 },
435
+ ]);
436
+ expect(JSON.stringify(states.body)).not.toContain('access-1');
437
+
438
+ const missingBody = await request(app).post('/api/linear/issues/update').send({}).expect(400);
439
+ expect(missingBody.body.error).toBe('id and stateId are required');
440
+
441
+ const updated = await request(app).post('/api/linear/issues/update').send({
442
+ id: 'issue-uuid-1',
443
+ stateId: 'state-done',
444
+ }).expect(200);
445
+ expect(updated.body.connected).toBe(true);
446
+ expect(updated.body.issue.identifier).toBe('ENG-12');
447
+ expect(updated.body.issue.state).toEqual({ id: 'state-done', name: 'Done', type: 'completed' });
448
+ expect(JSON.stringify(updated.body)).not.toContain('access-1');
449
+ });
450
+
451
+ it('returns 400 for Linear validation and not-found GraphQL errors', async () => {
452
+ setLinearAuth({
453
+ accessToken: 'access-1',
454
+ refreshToken: 'refresh-1',
455
+ tokenType: 'Bearer',
456
+ expiresAt: Date.now() + 86_400_000,
457
+ scope: 'write',
458
+ });
459
+ vi.stubGlobal('fetch', vi.fn(async (_url, options) => {
460
+ const body = JSON.parse(options.body);
461
+ if (body.query.includes('TeamWorkflowStates')) {
462
+ return jsonResponse({
463
+ data: null,
464
+ errors: [{
465
+ message: 'Entity not found: Team',
466
+ extensions: {
467
+ code: 'INPUT_ERROR',
468
+ userError: true,
469
+ userPresentableMessage: 'Could not find referenced Team.',
470
+ },
471
+ }],
472
+ });
473
+ }
474
+ return jsonResponse({
475
+ data: null,
476
+ errors: [{
477
+ message: 'Argument Validation Error',
478
+ extensions: {
479
+ code: 'INVALID_INPUT',
480
+ userError: true,
481
+ userPresentableMessage: 'stateId must be a UUID.',
482
+ },
483
+ }],
484
+ });
485
+ }));
486
+
487
+ const app = createApp();
488
+ const states = await request(app).get('/api/linear/issues/states').query({ teamId: 'missing-team' }).expect(400);
489
+ expect(states.body.error).toBe('Could not find referenced Team.');
490
+
491
+ const updated = await request(app).post('/api/linear/issues/update').send({
492
+ id: 'issue-uuid-1',
493
+ stateId: 'not-a-uuid',
494
+ }).expect(400);
495
+ expect(updated.body.error).toBe('stateId must be a UUID.');
496
+ });
497
+
498
+ it('returns disconnected for issue routes when Linear is not connected', async () => {
499
+ const app = createApp();
500
+ const list = await request(app).get('/api/linear/issues/list').expect(200);
501
+ expect(list.body).toEqual({ connected: false });
502
+ const got = await request(app).get('/api/linear/issues/get').query({ id: 'ENG-12' }).expect(200);
503
+ expect(got.body).toEqual({ connected: false });
504
+ const states = await request(app).get('/api/linear/issues/states').query({ teamId: 'team-eng' }).expect(200);
505
+ expect(states.body).toEqual({ connected: false });
506
+ const updated = await request(app).post('/api/linear/issues/update').send({
507
+ id: 'issue-1',
508
+ stateId: 'state-done',
509
+ }).expect(200);
510
+ expect(updated.body).toEqual({ connected: false });
511
+ });
512
+
513
+ it('returns disconnected mapping when Linear is not connected', async () => {
514
+ const app = createApp();
515
+ const mapping = await request(app).get('/api/linear/mapping').expect(200);
516
+ expect(mapping.body).toEqual({ connected: false });
517
+ const saved = await request(app).put('/api/linear/mapping').send({
518
+ defaultProjectPath: '/tmp/project',
519
+ teamProjectPaths: {},
520
+ }).expect(200);
521
+ expect(saved.body).toEqual({ connected: false });
522
+ });
523
+
524
+ it('saves and reads Linear team-to-project mapping without leaking tokens', async () => {
525
+ setLinearAuth({
526
+ accessToken: 'access-1',
527
+ refreshToken: 'refresh-1',
528
+ tokenType: 'Bearer',
529
+ expiresAt: Date.now() + 86_400_000,
530
+ scope: 'read',
531
+ });
532
+ vi.stubGlobal('fetch', vi.fn(async (_url, options) => {
533
+ const body = JSON.parse(options.body);
534
+ expect(body.query).toContain('query ListLinearTeams');
535
+ expect(options.headers.Authorization).toBe('Bearer access-1');
536
+ return jsonResponse({
537
+ data: {
538
+ teams: {
539
+ nodes: [
540
+ { id: 'team-eng', key: 'ENG', name: 'Engineering' },
541
+ { id: 'team-des', key: 'DES', name: 'Design' },
542
+ ],
543
+ pageInfo: { hasNextPage: false, endCursor: null },
544
+ },
545
+ },
546
+ });
547
+ }));
548
+
549
+ const app = createApp();
550
+ const empty = await request(app).get('/api/linear/mapping').expect(200);
551
+ expect(empty.body).toEqual({
552
+ connected: true,
553
+ defaultProjectPath: null,
554
+ teams: [
555
+ { id: 'team-eng', key: 'ENG', name: 'Engineering', projectPath: null },
556
+ { id: 'team-des', key: 'DES', name: 'Design', projectPath: null },
557
+ ],
558
+ });
559
+ expect(JSON.stringify(empty.body)).not.toContain('access-1');
560
+
561
+ const saved = await request(app).put('/api/linear/mapping').send({
562
+ defaultProjectPath: '/Users/ada/openchamber',
563
+ teamProjectPaths: { 'team-eng': '/Users/ada/eng' },
564
+ }).expect(200);
565
+ expect(saved.body).toEqual({
566
+ connected: true,
567
+ defaultProjectPath: '/Users/ada/openchamber',
568
+ teams: [
569
+ { id: 'team-eng', key: 'ENG', name: 'Engineering', projectPath: '/Users/ada/eng' },
570
+ { id: 'team-des', key: 'DES', name: 'Design', projectPath: null },
571
+ ],
572
+ });
573
+ expect(JSON.stringify(saved.body)).not.toContain('access-1');
574
+
575
+ const reread = await request(app).get('/api/linear/mapping').expect(200);
576
+ expect(reread.body.defaultProjectPath).toBe('/Users/ada/openchamber');
577
+ expect(reread.body.teams[0].projectPath).toBe('/Users/ada/eng');
578
+ });
579
+
580
+ it('posts a session status comment and never leaks the token', async () => {
581
+ setLinearSessionCommentsEnabled(true);
582
+ setLinearAuth({
583
+ accessToken: 'access-1',
584
+ refreshToken: 'refresh-1',
585
+ tokenType: 'Bearer',
586
+ expiresAt: Date.now() + 86_400_000,
587
+ scope: 'read,write,comments:create',
588
+ });
589
+ vi.stubGlobal('fetch', vi.fn(async (_url, options) => {
590
+ const body = JSON.parse(options.body);
591
+ if (body.query.includes('query GetLinearIssue')) {
592
+ return jsonResponse({
593
+ data: {
594
+ issue: {
595
+ id: 'issue-1',
596
+ identifier: 'ENG-12',
597
+ title: 'Broken login',
598
+ url: 'https://linear.app/openchamber/issue/ENG-12',
599
+ state: { name: 'Todo', type: 'unstarted' },
600
+ assignee: null,
601
+ description: null,
602
+ comments: { nodes: [] },
603
+ },
604
+ },
605
+ });
606
+ }
607
+ expect(body.query).toContain('mutation CommentCreate');
608
+ return jsonResponse({
609
+ data: {
610
+ commentCreate: {
611
+ success: true,
612
+ comment: { id: 'comment-1' },
613
+ },
614
+ },
615
+ });
616
+ }));
617
+
618
+ const app = createApp();
619
+ const missing = await request(app).post('/api/linear/session-status').send({
620
+ kind: 'started',
621
+ }).expect(400);
622
+ expect(missing.body.error).toBe('kind and sessionId are required');
623
+
624
+ const posted = await request(app).post('/api/linear/session-status').send({
625
+ kind: 'started',
626
+ sessionId: 'ses_1',
627
+ issueIdentifier: 'ENG-12',
628
+ sessionOrigin: 'https://app.example.com',
629
+ }).expect(200);
630
+ expect(posted.body).toEqual({
631
+ connected: true,
632
+ posted: true,
633
+ commentId: 'comment-1',
634
+ });
635
+ expect(JSON.stringify(posted.body)).not.toContain('access-1');
636
+ });
637
+
638
+ it('reads and writes the session-comment preference', async () => {
639
+ const app = createApp();
640
+ const initial = await request(app).get('/api/linear/preferences').expect(200);
641
+ expect(initial.body).toEqual({ sessionComments: false });
642
+
643
+ const invalid = await request(app).put('/api/linear/preferences').send({ sessionComments: 'yes' }).expect(400);
644
+ expect(invalid.body.error).toBe('sessionComments must be a boolean');
645
+
646
+ const enabled = await request(app).put('/api/linear/preferences').send({ sessionComments: true }).expect(200);
647
+ expect(enabled.body).toEqual({ sessionComments: true });
648
+ const reread = await request(app).get('/api/linear/preferences').expect(200);
649
+ expect(reread.body).toEqual({ sessionComments: true });
650
+ });
651
+
652
+ it('returns disconnected session-status when Linear is not connected', async () => {
653
+ const app = createApp();
654
+ const response = await request(app).post('/api/linear/session-status').send({
655
+ kind: 'started',
656
+ sessionId: 'ses_1',
657
+ issueIdentifier: 'ENG-12',
658
+ }).expect(200);
659
+ expect(response.body).toEqual({ connected: false });
660
+ });
661
+ });