@superblocksteam/sdk 2.0.157-next.5 → 2.0.157

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 (67) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/cli-replacement/automatic-upgrades.d.ts.map +1 -1
  3. package/dist/cli-replacement/automatic-upgrades.js +8 -4
  4. package/dist/cli-replacement/automatic-upgrades.js.map +1 -1
  5. package/dist/cli-replacement/automatic-upgrades.test.js +1 -2
  6. package/dist/cli-replacement/automatic-upgrades.test.js.map +1 -1
  7. package/dist/cli-replacement/dev-s3-restore.test.mjs +2 -6
  8. package/dist/cli-replacement/dev-s3-restore.test.mjs.map +1 -1
  9. package/dist/cli-replacement/dev-startup-git-before-dbfs-order.test.mjs +2 -6
  10. package/dist/cli-replacement/dev-startup-git-before-dbfs-order.test.mjs.map +1 -1
  11. package/dist/cli-replacement/dev.d.mts +6 -6
  12. package/dist/cli-replacement/dev.d.mts.map +1 -1
  13. package/dist/cli-replacement/dev.interception.test.mjs +12 -12
  14. package/dist/cli-replacement/dev.interception.test.mjs.map +1 -1
  15. package/dist/cli-replacement/dev.mjs +42 -100
  16. package/dist/cli-replacement/dev.mjs.map +1 -1
  17. package/dist/dev-utils/dev-server.d.mts +1 -36
  18. package/dist/dev-utils/dev-server.d.mts.map +1 -1
  19. package/dist/dev-utils/dev-server.mjs +73 -80
  20. package/dist/dev-utils/dev-server.mjs.map +1 -1
  21. package/dist/telemetry/logging.js +1 -1
  22. package/dist/telemetry/logging.js.map +1 -1
  23. package/dist/telemetry/safe-stringify.d.ts.map +1 -1
  24. package/dist/telemetry/safe-stringify.js +9 -25
  25. package/dist/telemetry/safe-stringify.js.map +1 -1
  26. package/package.json +6 -10
  27. package/src/cli-replacement/automatic-upgrades.test.ts +1 -2
  28. package/src/cli-replacement/automatic-upgrades.ts +11 -4
  29. package/src/cli-replacement/dev-s3-restore.test.mts +2 -6
  30. package/src/cli-replacement/dev-startup-git-before-dbfs-order.test.mts +2 -6
  31. package/src/cli-replacement/dev.interception.test.mts +12 -12
  32. package/src/cli-replacement/dev.mts +56 -112
  33. package/src/dev-utils/dev-server.mts +93 -110
  34. package/src/telemetry/logging.ts +1 -1
  35. package/src/telemetry/safe-stringify.ts +10 -25
  36. package/test/safe-stringify.test.mts +0 -25
  37. package/tsconfig.tsbuildinfo +1 -1
  38. package/dist/cli-replacement/dev-startup-lock-acquisition.test.d.mts +0 -2
  39. package/dist/cli-replacement/dev-startup-lock-acquisition.test.d.mts.map +0 -1
  40. package/dist/cli-replacement/dev-startup-lock-acquisition.test.mjs +0 -487
  41. package/dist/cli-replacement/dev-startup-lock-acquisition.test.mjs.map +0 -1
  42. package/dist/dev-utils/dev-server-json-parser.test.d.mts +0 -2
  43. package/dist/dev-utils/dev-server-json-parser.test.d.mts.map +0 -1
  44. package/dist/dev-utils/dev-server-json-parser.test.mjs +0 -60
  45. package/dist/dev-utils/dev-server-json-parser.test.mjs.map +0 -1
  46. package/dist/dev-utils/dev-server.shutdown-lock.test.d.mts +0 -2
  47. package/dist/dev-utils/dev-server.shutdown-lock.test.d.mts.map +0 -1
  48. package/dist/dev-utils/dev-server.shutdown-lock.test.mjs +0 -48
  49. package/dist/dev-utils/dev-server.shutdown-lock.test.mjs.map +0 -1
  50. package/dist/dev-utils/fatal-process-barrier.d.mts +0 -71
  51. package/dist/dev-utils/fatal-process-barrier.d.mts.map +0 -1
  52. package/dist/dev-utils/fatal-process-barrier.mjs +0 -161
  53. package/dist/dev-utils/fatal-process-barrier.mjs.map +0 -1
  54. package/dist/dev-utils/fatal-process-barrier.spawn-fixture.d.mts +0 -2
  55. package/dist/dev-utils/fatal-process-barrier.spawn-fixture.d.mts.map +0 -1
  56. package/dist/dev-utils/fatal-process-barrier.spawn-fixture.mjs +0 -9
  57. package/dist/dev-utils/fatal-process-barrier.spawn-fixture.mjs.map +0 -1
  58. package/dist/dev-utils/fatal-process-barrier.test.d.mts +0 -2
  59. package/dist/dev-utils/fatal-process-barrier.test.d.mts.map +0 -1
  60. package/dist/dev-utils/fatal-process-barrier.test.mjs +0 -281
  61. package/dist/dev-utils/fatal-process-barrier.test.mjs.map +0 -1
  62. package/src/cli-replacement/dev-startup-lock-acquisition.test.mts +0 -573
  63. package/src/dev-utils/dev-server-json-parser.test.mts +0 -89
  64. package/src/dev-utils/dev-server.shutdown-lock.test.mts +0 -58
  65. package/src/dev-utils/fatal-process-barrier.mts +0 -262
  66. package/src/dev-utils/fatal-process-barrier.spawn-fixture.mts +0 -12
  67. package/src/dev-utils/fatal-process-barrier.test.mts +0 -346
@@ -1,573 +0,0 @@
1
- import * as fs from "node:fs/promises";
2
- import * as http from "node:http";
3
- import * as os from "node:os";
4
- import * as path from "node:path";
5
-
6
- import {
7
- afterEach,
8
- beforeEach,
9
- describe,
10
- expect,
11
- it,
12
- vi,
13
- type Mock,
14
- } from "vitest";
15
-
16
- const execMock = vi.fn();
17
- const execFileMock = vi.fn();
18
- vi.mock("node:child_process", () => ({
19
- default: { exec: execMock, execFile: execFileMock },
20
- exec: execMock,
21
- execFile: execFileMock,
22
- }));
23
-
24
- vi.mock("node:readline", () => ({
25
- default: {
26
- createInterface: vi.fn(() => ({ question: vi.fn(), close: vi.fn() })),
27
- },
28
- createInterface: vi.fn(() => ({ question: vi.fn(), close: vi.fn() })),
29
- }));
30
-
31
- vi.mock("fs-extra", () => ({
32
- default: {
33
- existsSync: vi.fn(() => true),
34
- readFileSync: vi.fn(() => "{}"),
35
- },
36
- }));
37
-
38
- vi.mock("package-manager-detector/detect", () => ({
39
- detect: vi.fn(async () => ({
40
- name: "npm",
41
- agent: "npm",
42
- version: "10.0.0",
43
- })),
44
- }));
45
-
46
- vi.mock("package-manager-detector", () => ({
47
- resolveCommand: vi.fn(() => ({
48
- command: "npm",
49
- args: ["install", "--fund=false", "--audit=false"],
50
- })),
51
- }));
52
-
53
- vi.mock("read-pkg", () => ({
54
- readPackage: vi.fn(async () => ({
55
- name: "test-app",
56
- dependencies: { "@superblocksteam/library": "1.0.0" },
57
- })),
58
- }));
59
-
60
- const mockLogger = {
61
- info: vi.fn(),
62
- warn: vi.fn(),
63
- error: vi.fn(),
64
- debug: vi.fn(),
65
- };
66
-
67
- vi.mock("../telemetry/logging.js", () => ({
68
- getLogger: () => mockLogger,
69
- getErrorMeta: vi.fn(() => ({})),
70
- }));
71
-
72
- vi.mock("../telemetry/index.js", () => ({
73
- getTracer: () => ({
74
- startActiveSpan: vi.fn(
75
- async (
76
- _name: string,
77
- fn: (span: {
78
- end: () => void;
79
- setStatus: () => void;
80
- }) => Promise<unknown>,
81
- ) => fn({ end: vi.fn(), setStatus: vi.fn() }),
82
- ),
83
- }),
84
- isCloudPrem: false,
85
- shutdownTelemetry: vi.fn(async () => true),
86
- }));
87
-
88
- vi.mock("@opentelemetry/api", () => ({
89
- SpanStatusCode: { ERROR: 2 },
90
- }));
91
-
92
- vi.mock("colorette", () => ({
93
- green: (s: string) => s,
94
- }));
95
-
96
- vi.mock("@superblocksteam/shared", async (importOriginal) => ({
97
- // dev.mts now transitively loads @superblocksteam/shared (via the npm-error
98
- // classifier re-exported from vite-plugin-file-sync/npm-registry — APPS-4450),
99
- // pulling in many named exports (DeploymentTypeEnum, ISocket, …). Spread the
100
- // real module so the closed overrides below don't have to enumerate them.
101
- ...((await importOriginal()) as Record<string, unknown>),
102
- buildGithubSuperblocksSyncWorkflow: vi.fn(),
103
- buildGithubSuperblocksSyncWorkflowFromBaseUrl: vi.fn(),
104
- ConflictError: class ConflictError extends Error {},
105
- DEFAULT_NON_GIT_BRANCH: "__non_git__",
106
- isGitHubRemoteUrl: vi.fn(() => false),
107
- NotFoundError: class NotFoundError extends Error {},
108
- SUPERBLOCKS_LIVE_GIT_BRANCH: "__live__",
109
- }));
110
-
111
- vi.mock("@superblocksteam/util", () => ({
112
- maskUnixSignals: async function maskUnixSignals(fn: () => Promise<void>) {
113
- return fn();
114
- },
115
- }));
116
-
117
- vi.mock("@superblocksteam/vite-plugin-file-sync/ai-service", () => ({
118
- AiService: class {
119
- initialize = vi.fn(async () => undefined);
120
- notifyMigrationAppArtifactsReady = vi.fn();
121
- removeIntegrationCache = vi.fn(async () => undefined);
122
- remediateDeclaredDependencies = vi.fn(async () => undefined);
123
- chatSessionStore = { invalidateCache: vi.fn() };
124
- getNpmRegistryClient = mockGetNpmRegistryClient;
125
- },
126
- AiServiceFeatureFlags: class {
127
- static create = vi.fn(() => ({}));
128
- },
129
- SnapshotManager: class {
130
- executePendingRestore = vi.fn(async () => false);
131
- },
132
- hasEditLifecycleCapability: vi.fn(() => false),
133
- isSdkApiTemplate: vi.fn(() => false),
134
- stripResolvedFromLockfile: vi.fn(async () => undefined),
135
- }));
136
-
137
- vi.mock("@superblocksteam/vite-plugin-file-sync/npm-registry", () => ({
138
- shouldIgnoreInstallScripts: vi.fn(() => false),
139
- maybeWriteNpmrcForDir: vi.fn(async () => {
140
- // Delay before the log push so this test fails if syncHomeNpmrc is
141
- // fire-and-forget (initiation order alone would still look correct).
142
- await new Promise((resolve) => setTimeout(resolve, 5));
143
- startupOpLog.push("project-npmrc:maybeWriteNpmrcForDir");
144
- }),
145
- }));
146
-
147
- const { startupOpLog, buildMockGitService, mockGetNpmRegistryClient } =
148
- vi.hoisted(() => {
149
- const startupOpLog: string[] = [];
150
- const mockGetNpmRegistryClient = vi.fn(
151
- ():
152
- | {
153
- getConfig: ReturnType<typeof vi.fn>;
154
- organizationId: string;
155
- }
156
- | undefined => ({
157
- organizationId: "org-1",
158
- getConfig: vi.fn(async () => ({
159
- source: "not-configured",
160
- config: { configured: false },
161
- })),
162
- }),
163
- );
164
- function buildMockGitService(cwd: string) {
165
- return {
166
- workDir: cwd,
167
- configure: vi.fn(async () => {
168
- startupOpLog.push("git:configure");
169
- }),
170
- init: vi.fn(async () => {
171
- startupOpLog.push("git:init");
172
- }),
173
- addRemote: vi.fn(async () => {
174
- startupOpLog.push("git:addRemote");
175
- }),
176
- fetch: vi.fn(async () => {
177
- startupOpLog.push("git:fetch");
178
- }),
179
- raw: vi.fn(async (args: string | string[]) => {
180
- const argv = Array.isArray(args) ? args : [args];
181
- startupOpLog.push(`git:raw:${argv.join(" ")}`);
182
- if (argv[0] === "ls-remote" && argv.includes("__live__")) {
183
- return "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef\trefs/heads/__live__\n";
184
- }
185
- if (argv[0] === "branch" && argv.includes("--show-current")) {
186
- return "__live__\n";
187
- }
188
- if (argv[0] === "commit") {
189
- return "";
190
- }
191
- return "";
192
- }),
193
- push: vi.fn(async () => {
194
- startupOpLog.push("git:push");
195
- }),
196
- getDefaultBranch: vi.fn(async () => "main"),
197
- getRemotes: vi.fn(async () => [
198
- { name: "origin", refs: { fetch: "https://example.test/repo.git" } },
199
- ]),
200
- remote: vi.fn(async () => {
201
- startupOpLog.push("git:remote");
202
- }),
203
- revparse: vi.fn(async (ref: string) => {
204
- startupOpLog.push(`git:revparse:${ref}`);
205
- return "abc123\n";
206
- }),
207
- checkout: vi.fn(async () => {
208
- startupOpLog.push("git:checkout");
209
- }),
210
- checkoutOrCreate: vi.fn(async () => {
211
- startupOpLog.push("git:checkoutOrCreate");
212
- }),
213
- };
214
- }
215
- return { startupOpLog, buildMockGitService, mockGetNpmRegistryClient };
216
- });
217
-
218
- vi.mock("@superblocksteam/vite-plugin-file-sync/git-service", () => ({
219
- createGitService: vi.fn((cwd: string) => {
220
- startupOpLog.push("createGitService");
221
- return buildMockGitService(cwd);
222
- }),
223
- }));
224
-
225
- const mockLockService = {
226
- acquireLock: vi.fn(async () => undefined),
227
- forceTakeover: vi.fn(async () => undefined),
228
- inspectLock: vi.fn(async () => ({
229
- ownerName: "someone@example.com",
230
- canForceTakeover: false,
231
- lockLifetimeMs: 60_000,
232
- timeSinceLastActivityMs: 0,
233
- })),
234
- setOnLockConflict: vi.fn(),
235
- relinquishLock: vi.fn(async () => undefined),
236
- notifyClosedByServer: vi.fn(async () => undefined),
237
- shutdownAndExit: vi.fn(async () => undefined),
238
- setIsRestarting: vi.fn(),
239
- isLocked: false,
240
- // The git-service mock below simulates a live/target branch mismatch, which
241
- // is what drives `ensureRuntimeDbfsBranchConsistency`. Both of the methods it
242
- // calls have to exist here or the first one throws a TypeError that an outer
243
- // catch swallows — leaving the suite green without ever having run the
244
- // interaction.
245
- isLockedWhenSettled: vi.fn(async () => false),
246
- switchBranch: vi.fn(async () => undefined),
247
- connectedUsers: [],
248
- wasRecentlyActive: false,
249
- timeSinceLastActivity: 0,
250
- };
251
-
252
- vi.mock("@superblocksteam/vite-plugin-file-sync/lock-service", () => ({
253
- LockService: class {
254
- acquireLock = mockLockService.acquireLock;
255
- forceTakeover = mockLockService.forceTakeover;
256
- inspectLock = mockLockService.inspectLock;
257
- setOnLockConflict = mockLockService.setOnLockConflict;
258
- relinquishLock = mockLockService.relinquishLock;
259
- notifyClosedByServer = mockLockService.notifyClosedByServer;
260
- shutdownAndExit = mockLockService.shutdownAndExit;
261
- setIsRestarting = mockLockService.setIsRestarting;
262
- isLocked = mockLockService.isLocked;
263
- isLockedWhenSettled = mockLockService.isLockedWhenSettled;
264
- switchBranch = mockLockService.switchBranch;
265
- connectedUsers = mockLockService.connectedUsers;
266
- wasRecentlyActive = mockLockService.wasRecentlyActive;
267
- timeSinceLastActivity = mockLockService.timeSinceLastActivity;
268
- },
269
- LockType: { CSB: "CSB", LOCAL: "LOCAL" },
270
- }));
271
-
272
- vi.mock("@superblocksteam/vite-plugin-file-sync/operation-queue", () => ({
273
- OperationQueue: class {
274
- enqueue = vi.fn(async (fn: () => Promise<unknown>) => fn());
275
- },
276
- }));
277
-
278
- vi.mock("@superblocksteam/vite-plugin-file-sync/server-rpc", () => ({
279
- AutoConnectingRpcClient: class {},
280
- }));
281
-
282
- const mockSyncService = {
283
- downloadDirectory: vi.fn(async () => {
284
- startupOpLog.push("dbfs:downloadDirectory");
285
- }),
286
- uploadDirectory: vi.fn(async () => undefined),
287
- uploadDirectoryNowIfNeeded: vi.fn(async () => undefined),
288
- cancelPendingSync: vi.fn(),
289
- };
290
-
291
- vi.mock("@superblocksteam/vite-plugin-file-sync/sync-service", () => ({
292
- SyncService: class {
293
- downloadDirectory = mockSyncService.downloadDirectory;
294
- uploadDirectory = mockSyncService.uploadDirectory;
295
- uploadDirectoryNowIfNeeded = mockSyncService.uploadDirectoryNowIfNeeded;
296
- cancelPendingSync = mockSyncService.cancelPendingSync;
297
- },
298
- }));
299
-
300
- vi.mock("@superblocksteam/vite-plugin-file-sync/draft-interface", () => ({}));
301
-
302
- vi.mock("./version-detection.js", () => ({
303
- getCurrentCliVersion: vi.fn(async () => ({
304
- version: "2.0.0",
305
- alias: undefined,
306
- })),
307
- }));
308
-
309
- const mockCheckVersions = vi.fn(async () => ({
310
- cliUpdated: false,
311
- upgradePromises: [] as Promise<void>[],
312
- }));
313
- vi.mock("./automatic-upgrades.js", () => ({
314
- checkVersionsAndWritePackageJson: (
315
- ...args: Parameters<typeof mockCheckVersions>
316
- ) => mockCheckVersions(...args),
317
- buildInstallEnv: (userconfigPath: string) => ({
318
- ...process.env,
319
- NPM_CONFIG_USERCONFIG: userconfigPath,
320
- PNPM_CONFIG_USERCONFIG: userconfigPath,
321
- }),
322
- }));
323
-
324
- // The home `~/.npmrc` writer (APPS-4231) is a best-effort step that runs
325
- // before auto-upgrade. Mock it out here so this test stays focused on the
326
- // git-before-DBFS ordering. The `AiService` mock above exposes a minimal
327
- // `getNpmRegistryClient()` stub so the call site doesn't `TypeError`
328
- // and silently swallow this mock.
329
- vi.mock("./home-npmrc.mjs", () => ({
330
- syncHomeNpmrc: vi.fn(async () => {
331
- // Longer delay than maybeWriteNpmrcForDir: if both run without await,
332
- // project pushes first and the order assertion fails.
333
- await new Promise((resolve) => setTimeout(resolve, 30));
334
- startupOpLog.push("home-npmrc:syncHomeNpmrc");
335
- return {
336
- outcome: "skipped-not-configured",
337
- path: "/tmp/.superblocks/npmrc",
338
- };
339
- }),
340
- superblocksNpmrcPath: vi.fn(() => "/tmp/.superblocks/npmrc"),
341
- superblocksLogsPath: vi.fn((appDir: string) => `${appDir}/.superblocks/logs`),
342
- }));
343
-
344
- vi.mock("./git-repo-setup.mjs", () => ({
345
- ensureRemoteHasDefaultBranch: vi.fn(async () => undefined),
346
- getGitErrorFields: vi.fn(() => ({})),
347
- }));
348
-
349
- const mockCreateDevServer = vi.fn(async () => http.createServer());
350
- vi.mock("../dev-utils/dev-server.mjs", () => ({
351
- createDevServer: (...args: Parameters<typeof mockCreateDevServer>) =>
352
- mockCreateDevServer(...args),
353
- }));
354
-
355
- vi.mock("../index.js", () => ({
356
- AUTO_UPGRADE_EXIT_CODE: 99,
357
- }));
358
-
359
- const SERVER_DBFS_HASH = "dbfs-head-after-clark";
360
-
361
- function buildMockSdk() {
362
- const hashLocalDirectory = vi
363
- .fn()
364
- .mockImplementationOnce(async () => {
365
- startupOpLog.push("sdk:hashLocalDirectory:before-dbfs");
366
- return { hash: "git-tree-without-uncommitted-clark-change" };
367
- })
368
- .mockImplementationOnce(async () => {
369
- startupOpLog.push("sdk:hashLocalDirectory:after-dbfs");
370
- return { hash: SERVER_DBFS_HASH };
371
- });
372
-
373
- return {
374
- hashLocalDirectory,
375
- dbfsGetApplicationDirectoryHash: vi.fn(async () => SERVER_DBFS_HASH),
376
- getApplicationGitConfig: vi.fn(async () => ({
377
- gitRemoteUrl: "https://github.com/example/superblocks-app.git",
378
- hasCredential: false,
379
- })),
380
- fetchCurrentUser: vi.fn(async () => ({
381
- user: {
382
- id: "user-1",
383
- name: "Test User",
384
- email: "test@example.com",
385
- currentOrganizationId: "org-1",
386
- },
387
- organizations: [{ id: "org-1", pluginExecutionVersions: {} }],
388
- flagBootstrap: {},
389
- })),
390
- getFeatureFlagsForCurrentUser: vi.fn(async () => ({
391
- devServerCloudInactivityTimeoutMinutes: () => 30,
392
- devServerLocalInactivityTimeoutMinutes: () => 30,
393
- enableSessionRecording: () => false,
394
- removeManualCommitsEnabled: () => false,
395
- })),
396
- getFeatureFlagsForUser: vi.fn(() => ({
397
- devServerCloudInactivityTimeoutMinutes: () => 30,
398
- devServerLocalInactivityTimeoutMinutes: () => 30,
399
- })),
400
- };
401
- }
402
-
403
- function buildDevOptions(cwd: string, overrides: Record<string, unknown> = {}) {
404
- return {
405
- cwd,
406
- downloadFirst: true,
407
- autoUpgradeMode: "skip-upgrade",
408
- applicationConfig: {
409
- id: "app-test",
410
- branchName: "main",
411
- },
412
- tokenConfig: {
413
- superblocksBaseUrl: "https://example.test",
414
- token: "test-token",
415
- },
416
- tokenManager: {
417
- getToken: vi.fn(() => "test-token"),
418
- onTokenRefresh: vi.fn(),
419
- updateToken: vi.fn(),
420
- getCurrentToken: vi.fn(() => "test-token"),
421
- },
422
- getCurrentToken: vi.fn(() => "test-token"),
423
- sdk: buildMockSdk(),
424
- ...overrides,
425
- };
426
- }
427
-
428
- function setupExecMock() {
429
- execMock.mockImplementation(
430
- (
431
- _cmd: string,
432
- _opts: unknown,
433
- cb?: (err: null, result: { stdout: string }) => void,
434
- ) => {
435
- startupOpLog.push("installPackages:exec");
436
- if (typeof _opts === "function") {
437
- (_opts as (err: null, result: { stdout: string }) => void)(null, {
438
- stdout: "installed",
439
- });
440
- } else if (cb) {
441
- cb(null, { stdout: "installed" });
442
- }
443
- },
444
- );
445
- }
446
-
447
- describe("dev startup: who takes the live-edit lock, and when (ENG-6090)", () => {
448
- let testCwd: string;
449
-
450
- beforeEach(async () => {
451
- vi.clearAllMocks();
452
- delete process.env.SUPERBLOCKS_IS_CSB;
453
- mockGetNpmRegistryClient.mockImplementation(() => ({
454
- organizationId: "org-1",
455
- getConfig: vi.fn(async () => ({
456
- source: "not-configured",
457
- config: { configured: false },
458
- })),
459
- }));
460
- startupOpLog.length = 0;
461
- setupExecMock();
462
- mockCheckVersions.mockResolvedValue({
463
- cliUpdated: false,
464
- upgradePromises: [],
465
- });
466
- testCwd = await fs.mkdtemp(path.join(os.tmpdir(), "sb-dev-lock-acquire-"));
467
- const { readPackage } = await import("read-pkg");
468
- (readPackage as Mock).mockResolvedValue({
469
- name: "test-app",
470
- dependencies: { "@superblocksteam/library": "1.0.0" },
471
- });
472
- });
473
-
474
- afterEach(async () => {
475
- await fs.rm(testCwd, { recursive: true, force: true });
476
- });
477
-
478
- it.each([
479
- ["cloud", "true"],
480
- ["local", undefined],
481
- ])("does not take the lock at boot on the %s path", async (_name, isCsb) => {
482
- if (isCsb) {
483
- process.env.SUPERBLOCKS_IS_CSB = isCsb;
484
- }
485
- const { dev } = await import("./dev.mjs");
486
-
487
- await dev(buildDevOptions(testCwd) as any);
488
-
489
- // Nothing startup does is gated on holding the lock — not the DBFS
490
- // download, not the package install, not the hash upload. Taking it before
491
- // any client exists is holding it on behalf of nobody, which is the state
492
- // the startup no-socket watchdog existed to clean up. A local dev server
493
- // has no more claim to it at boot than a sandbox does: the browser tab
494
- // that has not opened yet is the client, not the process.
495
- expect(mockLockService.acquireLock).not.toHaveBeenCalled();
496
- expect(mockLockService.forceTakeover).not.toHaveBeenCalled();
497
- // ...and it still boots and serves.
498
- expect(mockSyncService.downloadDirectory).toHaveBeenCalled();
499
- expect(mockCreateDevServer).toHaveBeenCalled();
500
- });
501
-
502
- it("hands the conflict handler to the lock service instead of racing it at boot", async () => {
503
- const { dev } = await import("./dev.mjs");
504
-
505
- await dev(buildDevOptions(testCwd) as any);
506
-
507
- // The reporting that used to wrap the boot-time acquire still exists; it is
508
- // wired to where acquisition actually happens now.
509
- expect(mockLockService.setOnLockConflict).toHaveBeenCalledWith(
510
- expect.any(Function),
511
- );
512
- });
513
-
514
- it("reports a conflict without stopping the dev server", async () => {
515
- const { dev } = await import("./dev.mjs");
516
- mockLockService.inspectLock.mockResolvedValueOnce({
517
- ownerName: "someone@example.com",
518
- canForceTakeover: false,
519
- lockLifetimeMs: 120_000,
520
- timeSinceLastActivityMs: 0,
521
- });
522
-
523
- await dev(buildDevOptions(testCwd) as any);
524
- const onConflict = mockLockService.setOnLockConflict.mock.calls[0]![0];
525
- await onConflict(new Error("Another user is currently editing this app."));
526
-
527
- // Someone else holds it, so this client cannot edit — but the sandbox is
528
- // still serving preview and the lock does not decide how long it lives.
529
- expect(mockLockService.relinquishLock).not.toHaveBeenCalled();
530
- expect(mockLockService.shutdownAndExit).not.toHaveBeenCalled();
531
- expect(mockLogger.warn).toHaveBeenCalledWith(
532
- expect.stringContaining("Could not take the live-edit lock"),
533
- );
534
- });
535
-
536
- it("stops on an unresolvable conflict under the VS Code extension", async () => {
537
- process.env.SUPERBLOCKS_VSCODE = "true";
538
- const { shutdownTelemetry } = await import("../telemetry/index.js");
539
- const exit = vi
540
- .spyOn(process, "exit")
541
- .mockImplementation(() => undefined as never);
542
- const { dev } = await import("./dev.mjs");
543
-
544
- await dev(buildDevOptions(testCwd) as any);
545
- const onConflict = mockLockService.setOnLockConflict.mock.calls[0]![0];
546
- await onConflict(new Error("Another user is currently editing this app."));
547
-
548
- // The extension answers a takeover prompt by restarting this command with
549
- // --force-takeover, and that hand-off has always ended with this process
550
- // gone. Staying up would leave the replacement fighting us for the port.
551
- expect(exit).toHaveBeenCalledWith(1);
552
- // Through the same teardown as the other two fatal exits, not
553
- // `shutdownAndExit`: that one exits synchronously from its own `finally`,
554
- // which made the span end and the telemetry flush below it unreachable —
555
- // so the trace for the conflict never left the process.
556
- expect(mockLockService.shutdownAndExit).not.toHaveBeenCalled();
557
- expect(mockLockService.relinquishLock).toHaveBeenCalled();
558
- expect(shutdownTelemetry).toHaveBeenCalled();
559
- exit.mockRestore();
560
- });
561
-
562
- it("honours an explicit force takeover at boot", async () => {
563
- process.env.SUPERBLOCKS_IS_CSB = "true";
564
- const { dev } = await import("./dev.mjs");
565
-
566
- // The one thing that still acquires at boot, and it is not lock-type
567
- // specific: someone asked for it by name.
568
- await dev(buildDevOptions(testCwd, { forceTakeover: true }) as any);
569
-
570
- expect(mockLockService.forceTakeover).toHaveBeenCalled();
571
- expect(mockLockService.acquireLock).not.toHaveBeenCalled();
572
- });
573
- });
@@ -1,89 +0,0 @@
1
- import { createServer, type Server } from "node:http";
2
-
3
- import express from "express";
4
- import { afterEach, describe, expect, it } from "vitest";
5
-
6
- import { mountDevServerJsonParsers } from "./dev-server.mjs";
7
-
8
- describe("dev server JSON parser", () => {
9
- let server: Server | undefined;
10
-
11
- afterEach(
12
- () =>
13
- new Promise<void>((resolve, reject) => {
14
- if (!server) {
15
- resolve();
16
- return;
17
- }
18
- server.close((error) => (error ? reject(error) : resolve()));
19
- }),
20
- );
21
-
22
- async function listenWithParsers(
23
- registerRoute: (app: express.Express) => void,
24
- ): Promise<number> {
25
- const app = express();
26
- mountDevServerJsonParsers(app);
27
- registerRoute(app);
28
-
29
- server = createServer(app);
30
- await new Promise<void>((resolve) => server?.listen(0, resolve));
31
- const address = server.address();
32
- if (!address || typeof address === "string") {
33
- throw new Error("Expected a TCP listener");
34
- }
35
- return address.port;
36
- }
37
-
38
- it("accepts policy gate assignment bodies larger than 100 KiB", async () => {
39
- const port = await listenWithParsers((app) => {
40
- app.post("/_sb_policy_gates/v1/execute", (_req, res) =>
41
- res.status(204).end(),
42
- );
43
- });
44
-
45
- const response = await fetch(
46
- `http://127.0.0.1:${port}/_sb_policy_gates/v1/execute`,
47
- {
48
- body: JSON.stringify({ scanScopeJson: "x".repeat(256 * 1024) }),
49
- headers: { "Content-Type": "application/json" },
50
- method: "POST",
51
- },
52
- );
53
-
54
- expect(response.status).toBe(204);
55
- });
56
-
57
- it("rejects policy gate JSON bodies larger than 1 MiB", async () => {
58
- const port = await listenWithParsers((app) => {
59
- app.post("/_sb_policy_gates/v1/execute", (_req, res) =>
60
- res.status(204).end(),
61
- );
62
- });
63
-
64
- const response = await fetch(
65
- `http://127.0.0.1:${port}/_sb_policy_gates/v1/execute`,
66
- {
67
- body: JSON.stringify({ scanScopeJson: "x".repeat(1024 * 1024) }),
68
- headers: { "Content-Type": "application/json" },
69
- method: "POST",
70
- },
71
- );
72
-
73
- expect(response.status).toBe(413);
74
- });
75
-
76
- it("keeps the 100 KiB default limit on non-policy-gate JSON posts", async () => {
77
- const port = await listenWithParsers((app) => {
78
- app.post("/_sb_ai/v1/chat", (_req, res) => res.status(204).end());
79
- });
80
-
81
- const response = await fetch(`http://127.0.0.1:${port}/_sb_ai/v1/chat`, {
82
- body: JSON.stringify({ prompt: "x".repeat(256 * 1024) }),
83
- headers: { "Content-Type": "application/json" },
84
- method: "POST",
85
- });
86
-
87
- expect(response.status).toBe(413);
88
- });
89
- });