-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary.test.ts
More file actions
771 lines (678 loc) · 28.4 KB
/
Copy pathbinary.test.ts
File metadata and controls
771 lines (678 loc) · 28.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
import assert from "node:assert/strict";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import test from "node:test";
import {
assessPatchloomCompatibility,
comparePatchloomVersions,
configuredBinaryPathFromSetting,
describePatchloomCompatibility,
describePatchloomSource,
findOnPath,
MINIMUM_SUPPORTED_PATCHLOOM_VERSION,
patchloomNeedsUpgrade,
parsePatchloomVersion,
resolvePatchloomStatusWithInputs
} from "../../src/binary/patchloom.js";
import {
assertTrustedManagedInstallDownloadUrl,
buildManagedInstallReleaseAssets,
isTrustedManagedInstallDownloadUrl,
calculateSha256Hex,
clearManagedInstallFailure,
clearManagedInstallFailureRecord,
detectManagedInstallTarget,
inspectManagedInstallStatus,
loadManagedInstallFailure,
ManagedInstallVerificationError,
normalizeReleaseVersion,
parseManagedInstallChecksumFile,
PATCHLOOM_MANAGED_INSTALL_DIR,
PATCHLOOM_MANAGED_INSTALL_FAILURE_FILE,
clearManagedInstallStaging,
persistManagedInstallFailure,
promoteManagedInstallBinary,
resolveManagedInstallChecksum,
resolveManagedInstallPaths,
resolveManagedInstallTransactionPaths,
setManagedInstallFailure,
verifyManagedInstallArchiveChecksum
} from "../../src/install/managed.js";
import { resolveMcpTargets } from "../../src/mcp/config.js";
import { defaultWorkspaceFolderIndex, describeWorkspaceEnvironment } from "../../src/workspace/readiness.js";
test("resolvePatchloomStatusWithInputs prefers patchloom.path over PATH", async () => {
const status = await resolvePatchloomStatusWithInputs({
configuredPath: " /custom/patchloom ",
pathValue: "/usr/local/bin:/bin",
canExecute: async (candidate) => candidate === "/custom/patchloom",
getVersion: async () => "patchloom 0.1.0"
});
assert.equal(status.ready, true);
assert.equal(status.source, "setting");
assert.equal(status.binaryPath, "/custom/patchloom");
assert.equal(status.version, "patchloom 0.1.0");
});
test("resolvePatchloomStatusWithInputs reports missing binary cleanly", async () => {
const status = await resolvePatchloomStatusWithInputs({
configuredPath: "",
pathValue: "/usr/local/bin:/bin",
canExecute: async () => false,
getVersion: async () => undefined
});
assert.equal(status.ready, false);
assert.equal(status.source, "missing");
assert.match(status.message, /not found/i);
});
test("resolvePatchloomStatusWithInputs reports version execution failures", async () => {
const status = await resolvePatchloomStatusWithInputs({
configuredPath: "/custom/patchloom",
canExecute: async () => true,
getVersion: async () => {
throw new Error("boom");
}
});
assert.equal(status.ready, false);
assert.equal(status.source, "setting");
assert.match(status.message, /failed to run --version/i);
assert.match(status.message, /boom/);
});
test("findOnPath respects win32 PATH separators and command names", async () => {
const found = await findOnPath(
"C:\\tools;C:\\bin",
"win32",
async (candidate) => candidate === "C:\\bin\\patchloom.exe"
);
assert.equal(found, "C:\\bin\\patchloom.exe");
});
test("parsePatchloomVersion extracts semantic versions from --version output", () => {
assert.equal(parsePatchloomVersion("patchloom 0.1.0"), "0.1.0");
assert.equal(parsePatchloomVersion("patchloom v0.2.0-beta.1"), "0.2.0-beta.1");
assert.equal(parsePatchloomVersion("custom build"), undefined);
});
test("comparePatchloomVersions follows semantic version ordering", () => {
assert.ok(comparePatchloomVersions("0.1.0", "0.1.0") === 0);
assert.ok(comparePatchloomVersions("0.2.0", "0.1.0") > 0);
assert.ok(comparePatchloomVersions("0.1.0-beta.1", "0.1.0") < 0);
});
test("assessPatchloomCompatibility flags outdated CLI builds", () => {
const assessment = assessPatchloomCompatibility("patchloom 0.0.9", MINIMUM_SUPPORTED_PATCHLOOM_VERSION);
assert.equal(assessment.compatibility, "unsupported");
assert.equal(assessment.detectedVersion, "0.0.9");
assert.match(assessment.message, /older than the minimum supported version/i);
});
test("resolvePatchloomStatusWithInputs exposes compatibility diagnostics", async () => {
const status = await resolvePatchloomStatusWithInputs({
configuredPath: "/custom/patchloom",
canExecute: async () => true,
getVersion: async () => "patchloom 0.0.9"
});
assert.equal(status.ready, true);
assert.equal(status.compatibility, "unsupported");
assert.equal(status.minimumSupportedVersion, MINIMUM_SUPPORTED_PATCHLOOM_VERSION);
assert.match(status.compatibilityMessage ?? "", /older than the minimum supported version/i);
});
test("defaultWorkspaceFolderIndex prefers active folders and only auto-selects single roots", () => {
assert.equal(defaultWorkspaceFolderIndex(3, 2), 2);
assert.equal(defaultWorkspaceFolderIndex(1, undefined), 0);
assert.equal(defaultWorkspaceFolderIndex(2, undefined), undefined);
});
test("defaultWorkspaceFolderIndex ignores out-of-range active indices", () => {
assert.equal(defaultWorkspaceFolderIndex(3, -1), undefined);
assert.equal(defaultWorkspaceFolderIndex(3, 3), undefined);
assert.equal(defaultWorkspaceFolderIndex(3, 99), undefined);
assert.equal(defaultWorkspaceFolderIndex(0, 0), undefined);
});
test("describeWorkspaceEnvironment reports local environment for undefined remoteName", () => {
const environment = describeWorkspaceEnvironment(undefined);
assert.equal(environment.label, "Local");
assert.equal(environment.support, "supported");
assert.equal(environment.supportsUserMcpConfig, true);
assert.equal(environment.remoteName, undefined);
assert.equal(environment.note, undefined);
});
test("describeWorkspaceEnvironment reports limited remote support", () => {
const environment = describeWorkspaceEnvironment("wsl");
assert.equal(environment.label, "WSL");
assert.equal(environment.support, "limited");
assert.equal(environment.supportsUserMcpConfig, false);
assert.match(environment.note ?? "", /workspace-scoped/i);
});
test("describeWorkspaceEnvironment reports limited support for ssh-remote", () => {
const environment = describeWorkspaceEnvironment("ssh-remote");
assert.equal(environment.label, "Remote SSH");
assert.equal(environment.support, "limited");
assert.equal(environment.supportsUserMcpConfig, false);
});
test("describeWorkspaceEnvironment reports limited support for dev-container", () => {
const environment = describeWorkspaceEnvironment("dev-container");
assert.equal(environment.label, "Dev Container");
assert.equal(environment.support, "limited");
assert.equal(environment.supportsUserMcpConfig, false);
});
test("describeWorkspaceEnvironment reports limited support for codespaces", () => {
const environment = describeWorkspaceEnvironment("codespaces");
assert.equal(environment.label, "Codespaces");
assert.equal(environment.support, "limited");
assert.equal(environment.supportsUserMcpConfig, false);
});
test("describeWorkspaceEnvironment reports unverified for unknown remote names", () => {
const environment = describeWorkspaceEnvironment("some-unknown-remote");
assert.equal(environment.label, "Remote (some-unknown-remote)");
assert.equal(environment.support, "unverified");
assert.equal(environment.supportsUserMcpConfig, false);
assert.match(environment.note ?? "", /not explicitly verified/i);
});
test("resolveMcpTargets omits user config targets when disabled", () => {
const targets = resolveMcpTargets("/workspace/demo", "/Users/demo", false);
assert.deepEqual(targets.map((target) => target.kind), ["vscode-workspace", "cursor-workspace"]);
});
test("detectManagedInstallTarget maps supported platforms to release targets", () => {
assert.deepEqual(detectManagedInstallTarget("darwin", "arm64"), {
platform: "darwin",
arch: "arm64",
targetTriple: "aarch64-apple-darwin",
archiveFormat: ".tar.xz"
});
assert.deepEqual(detectManagedInstallTarget("win32", "x64"), {
platform: "win32",
arch: "x64",
targetTriple: "x86_64-pc-windows-msvc",
archiveFormat: ".zip"
});
assert.equal(detectManagedInstallTarget("linux", "arm"), undefined);
});
test("resolveManagedInstallPaths uses cargo-dist style archive names", () => {
const target = detectManagedInstallTarget("darwin", "arm64");
assert.ok(target);
const paths = resolveManagedInstallPaths("/managed/install", "0.1.0", target);
assert.equal(paths.archiveFileName, "patchloom-aarch64-apple-darwin.tar.xz");
assert.equal(paths.checksumFileName, "patchloom-aarch64-apple-darwin.tar.xz.sha256");
assert.equal(paths.binaryPath, path.join("/managed/install", "0.1.0", "managed-bin", "patchloom"));
});
test("buildManagedInstallReleaseAssets builds archive and checksum urls with patchloom-v tag", () => {
const target = detectManagedInstallTarget("linux", "x64");
assert.ok(target);
const release = buildManagedInstallReleaseAssets("v0.1.0", target);
assert.equal(release.tagName, "patchloom-v0.1.0");
assert.equal(release.archiveFileName, "patchloom-x86_64-unknown-linux-gnu.tar.xz");
assert.equal(release.checksumFileName, "patchloom-x86_64-unknown-linux-gnu.tar.xz.sha256");
assert.match(release.archiveDownloadUrl, /\/patchloom-v0\.1\.0\/patchloom-x86_64-unknown-linux-gnu\.tar\.xz$/);
assert.match(release.checksumDownloadUrl, /\/patchloom-v0\.1\.0\/patchloom-x86_64-unknown-linux-gnu\.tar\.xz\.sha256$/);
});
test("parseManagedInstallChecksumFile accepts common sha256 sidecar formats", () => {
const entries = parseManagedInstallChecksumFile([
"ece89100861aa6d4c7e409f279777d1619f8d86e0f67f396fa9f3e4535eb2f0e patchloom-aarch64-apple-darwin.tar.xz",
"1dc508c14f9b3584c992c61dcf8d18d8d3a6770f33fff241038c1e9ea7b27a97 *patchloom-x86_64-unknown-linux-gnu.tar.xz"
].join("\n"));
assert.deepEqual(entries, [
{
sha256: "ece89100861aa6d4c7e409f279777d1619f8d86e0f67f396fa9f3e4535eb2f0e",
fileName: "patchloom-aarch64-apple-darwin.tar.xz"
},
{
sha256: "1dc508c14f9b3584c992c61dcf8d18d8d3a6770f33fff241038c1e9ea7b27a97",
fileName: "patchloom-x86_64-unknown-linux-gnu.tar.xz"
}
]);
});
test("resolveManagedInstallChecksum returns the matching archive checksum", () => {
const checksum = resolveManagedInstallChecksum(
[
"ece89100861aa6d4c7e409f279777d1619f8d86e0f67f396fa9f3e4535eb2f0e patchloom-aarch64-apple-darwin.tar.xz",
"1dc508c14f9b3584c992c61dcf8d18d8d3a6770f33fff241038c1e9ea7b27a97 patchloom-x86_64-unknown-linux-gnu.tar.xz"
].join("\n"),
"patchloom-x86_64-unknown-linux-gnu.tar.xz"
);
assert.equal(checksum, "1dc508c14f9b3584c992c61dcf8d18d8d3a6770f33fff241038c1e9ea7b27a97");
});
test("verifyManagedInstallArchiveChecksum validates archive content against the checksum sidecar", () => {
const checksum = verifyManagedInstallArchiveChecksum(
"patchloom",
"ece89100861aa6d4c7e409f279777d1619f8d86e0f67f396fa9f3e4535eb2f0e patchloom-aarch64-apple-darwin.tar.xz",
"patchloom-aarch64-apple-darwin.tar.xz"
);
assert.equal(checksum, calculateSha256Hex("patchloom"));
});
test("verifyManagedInstallArchiveChecksum rejects mismatched archive content", () => {
assert.throws(
() => verifyManagedInstallArchiveChecksum(
"managed-install",
"ece89100861aa6d4c7e409f279777d1619f8d86e0f67f396fa9f3e4535eb2f0e patchloom-aarch64-apple-darwin.tar.xz",
"patchloom-aarch64-apple-darwin.tar.xz"
),
(error: unknown) => {
assert.ok(error instanceof ManagedInstallVerificationError);
assert.equal(error.reason, "checksum-mismatch");
assert.match(error.message, /checksum mismatch/i);
return true;
}
);
});
test("parseManagedInstallChecksumFile rejects invalid lines", () => {
assert.throws(
() => parseManagedInstallChecksumFile("not-a-checksum patchloom-aarch64-apple-darwin.tar.xz"),
(error: unknown) => {
assert.ok(error instanceof ManagedInstallVerificationError);
assert.equal(error.reason, "invalid-checksum-format");
return true;
}
);
});
test("assertTrustedManagedInstallDownloadUrl only accepts GitHub release download URLs", () => {
assert.doesNotThrow(() => assertTrustedManagedInstallDownloadUrl(
"https://github.com/patchloom/patchloom/releases/download/v0.1.0/patchloom-aarch64-apple-darwin.tar.xz"
));
assert.throws(
() => assertTrustedManagedInstallDownloadUrl(
"https://example.com/patchloom-aarch64-apple-darwin.tar.xz"
),
(error: unknown) => {
assert.ok(error instanceof ManagedInstallVerificationError);
assert.equal(error.reason, "untrusted-download-url");
return true;
}
);
});
test("managed install constants use a stable storage directory name", () => {
assert.equal(PATCHLOOM_MANAGED_INSTALL_DIR, "patchloom-managed");
});
test("resolveManagedInstallTransactionPaths keeps staged files separate from the live binary", () => {
const target = detectManagedInstallTarget("darwin", "arm64");
assert.ok(target);
const paths = resolveManagedInstallTransactionPaths("/managed/install", "0.1.0", target);
assert.equal(paths.archivePath, path.join("/managed/install", "0.1.0", "patchloom-aarch64-apple-darwin.tar.xz"));
assert.equal(paths.stagedArchivePath, path.join("/managed/install", "0.1.0", ".staging", "patchloom-aarch64-apple-darwin.tar.xz"));
assert.equal(paths.stagedChecksumPath, path.join("/managed/install", "0.1.0", ".staging", "patchloom-aarch64-apple-darwin.tar.xz.sha256"));
assert.equal(paths.stagedBinaryPath, path.join("/managed/install", "0.1.0", ".staging", "managed-bin", "patchloom"));
assert.equal(paths.backupBinaryPath, `${path.join("/managed/install", "0.1.0", "managed-bin", "patchloom")}.bak`);
});
test("inspectManagedInstallStatus includes the last managed install failure for diagnostics", async () => {
const target = detectManagedInstallTarget("darwin", "arm64");
assert.ok(target);
setManagedInstallFailure({
stage: "verify",
reason: "checksum-mismatch",
message: "Checksum mismatch for patchloom-aarch64-apple-darwin.tar.xz."
});
try {
const status = await inspectManagedInstallStatus({
installRoot: "/managed/install",
version: "v0.1.0",
target,
fileExists: async () => false
});
assert.deepEqual(status, {
exists: false,
binaryPath: path.join("/managed/install", "0.1.0", "managed-bin", "patchloom"),
version: "0.1.0",
target,
failure: {
stage: "verify",
reason: "checksum-mismatch",
message: "Checksum mismatch for patchloom-aarch64-apple-darwin.tar.xz."
}
});
} finally {
clearManagedInstallFailure();
}
});
test("clearManagedInstallStaging removes the entire staging directory", async () => {
const target = detectManagedInstallTarget("darwin", "arm64");
assert.ok(target);
const paths = resolveManagedInstallTransactionPaths("/managed/install", "0.1.0", target);
const operations: string[] = [];
await clearManagedInstallStaging({
paths,
removeDir: async (dirPath) => {
operations.push(`rmdir ${dirPath}`);
}
});
assert.deepEqual(operations, [
`rmdir ${paths.stagingRoot}`
]);
});
test("promoteManagedInstallBinary replaces the live binary and clears stale backups", async () => {
const target = detectManagedInstallTarget("darwin", "arm64");
assert.ok(target);
const paths = resolveManagedInstallTransactionPaths("/managed/install", "0.1.0", target);
const operations: string[] = [];
const existing = new Set([
paths.binaryPath,
paths.stagedBinaryPath,
paths.backupBinaryPath
]);
await promoteManagedInstallBinary({
paths,
fileExists: async (filePath) => existing.has(filePath),
ensureDir: async (dirPath) => {
operations.push(`mkdir ${dirPath}`);
},
renameFile: async (from, to) => {
operations.push(`rename ${from} -> ${to}`);
existing.delete(from);
existing.add(to);
},
removeFile: async (filePath) => {
operations.push(`remove ${filePath}`);
existing.delete(filePath);
},
failurePersistence: {
storageRoot: "/managed/storage",
removeFile: async (filePath) => {
operations.push(`remove ${filePath}`);
}
}
});
assert.deepEqual(operations, [
`mkdir ${path.dirname(paths.binaryPath)}`,
`remove ${paths.backupBinaryPath}`,
`rename ${paths.binaryPath} -> ${paths.backupBinaryPath}`,
`rename ${paths.stagedBinaryPath} -> ${paths.binaryPath}`,
`remove ${paths.backupBinaryPath}`,
`remove ${path.join("/managed/storage", PATCHLOOM_MANAGED_INSTALL_FAILURE_FILE)}`
]);
const status = await inspectManagedInstallStatus({
installRoot: "/managed/install",
version: "v0.1.0",
target,
fileExists: async (filePath) => existing.has(filePath)
});
assert.equal(status?.exists, true);
assert.equal(status?.failure, undefined);
});
test("promoteManagedInstallBinary restores the previous binary when replacement fails", async () => {
const target = detectManagedInstallTarget("darwin", "arm64");
assert.ok(target);
const paths = resolveManagedInstallTransactionPaths("/managed/install", "0.1.0", target);
const operations: string[] = [];
const existing = new Set([
paths.binaryPath,
paths.stagedBinaryPath
]);
await assert.rejects(
() => promoteManagedInstallBinary({
paths,
fileExists: async (filePath) => existing.has(filePath),
ensureDir: async (dirPath) => {
operations.push(`mkdir ${dirPath}`);
},
renameFile: async (from, to) => {
operations.push(`rename ${from} -> ${to}`);
if (from === paths.stagedBinaryPath && to === paths.binaryPath) {
throw new Error("simulated rename failure");
}
existing.delete(from);
existing.add(to);
},
removeFile: async (filePath) => {
operations.push(`remove ${filePath}`);
existing.delete(filePath);
},
failurePersistence: {
storageRoot: "/managed/storage",
ensureDir: async (dirPath) => {
operations.push(`mkdir ${dirPath}`);
},
writeFile: async (filePath, content) => {
operations.push(`write ${filePath} => ${content.trim()}`);
}
}
}),
/Failed to replace managed Patchloom binary/
);
try {
const failurePath = path.join("/managed/storage", PATCHLOOM_MANAGED_INSTALL_FAILURE_FILE);
assert.deepEqual(operations, [
`mkdir ${path.dirname(paths.binaryPath)}`,
`rename ${paths.binaryPath} -> ${paths.backupBinaryPath}`,
`rename ${paths.stagedBinaryPath} -> ${paths.binaryPath}`,
`rename ${paths.backupBinaryPath} -> ${paths.binaryPath}`,
`mkdir ${path.dirname(failurePath)}`,
`write ${failurePath} => {\n \"stage\": \"replace\",\n \"reason\": \"replace-failed\",\n \"message\": \"Failed to replace managed Patchloom binary (simulated rename failure).\"\n}`
]);
const status = await inspectManagedInstallStatus({
installRoot: "/managed/install",
version: "v0.1.0",
target,
fileExists: async (filePath) => existing.has(filePath)
});
assert.equal(status?.exists, true);
assert.deepEqual(status?.failure, {
stage: "replace",
reason: "replace-failed",
message: "Failed to replace managed Patchloom binary (simulated rename failure)."
});
} finally {
clearManagedInstallFailure();
}
});
test("inspectManagedInstallStatus reports discovered managed binaries", async () => {
const target = detectManagedInstallTarget("darwin", "arm64");
assert.ok(target);
const expectedBinaryPath = path.join("/managed/install", "0.1.0", "managed-bin", "patchloom");
const status = await inspectManagedInstallStatus({
installRoot: "/managed/install",
version: "v0.1.0",
target,
fileExists: async (filePath) => filePath === expectedBinaryPath
});
assert.deepEqual(status, {
exists: true,
binaryPath: expectedBinaryPath,
version: "0.1.0",
target
});
});
test("loadManagedInstallFailure reads persisted failure diagnostics from storage", async () => {
clearManagedInstallFailure();
const failure = await loadManagedInstallFailure({
storageRoot: "/managed/storage",
readFile: async (filePath) => {
assert.equal(filePath, path.join("/managed/storage", PATCHLOOM_MANAGED_INSTALL_FAILURE_FILE));
return JSON.stringify({
stage: "verify",
reason: "checksum-mismatch",
message: "Checksum mismatch for patchloom-aarch64-apple-darwin.tar.xz."
});
}
});
assert.deepEqual(failure, {
stage: "verify",
reason: "checksum-mismatch",
message: "Checksum mismatch for patchloom-aarch64-apple-darwin.tar.xz."
});
clearManagedInstallFailure();
});
test("persistManagedInstallFailure and clearManagedInstallFailureRecord update the failure record file", async () => {
const writes: string[] = [];
await persistManagedInstallFailure({
stage: "extract",
reason: "extract-failed",
message: "Archive extraction failed."
}, {
storageRoot: "/managed/storage",
ensureDir: async (dirPath) => {
writes.push(`mkdir ${dirPath}`);
},
writeFile: async (filePath, content) => {
writes.push(`write ${filePath} => ${content.trim()}`);
}
});
await clearManagedInstallFailureRecord({
storageRoot: "/managed/storage",
removeFile: async (filePath) => {
writes.push(`remove ${filePath}`);
}
});
const failurePath = path.join("/managed/storage", PATCHLOOM_MANAGED_INSTALL_FAILURE_FILE);
assert.deepEqual(writes, [
`mkdir ${path.dirname(failurePath)}`,
`write ${failurePath} => {\n \"stage\": \"extract\",\n \"reason\": \"extract-failed\",\n \"message\": \"Archive extraction failed.\"\n}`,
`remove ${failurePath}`
]);
assert.equal(await loadManagedInstallFailure({}), undefined);
});
test("resolvePatchloomStatusWithInputs surfaces persisted managed install failures after reload", async () => {
clearManagedInstallFailure();
const storageRoot = await fs.mkdtemp(path.join(os.tmpdir(), "patchloom-managed-"));
try {
await persistManagedInstallFailure({
stage: "verify",
reason: "checksum-mismatch",
message: "Checksum mismatch for patchloom-aarch64-apple-darwin.tar.xz."
}, {
storageRoot
});
clearManagedInstallFailure();
const status = await resolvePatchloomStatusWithInputs({
configuredPath: "",
pathValue: "/usr/local/bin:/bin",
platform: "darwin",
arch: "arm64",
managedInstallRoot: storageRoot,
managedInstallVersion: "0.1.0",
managedFileExists: async () => false,
canExecute: async () => false,
getVersion: async () => undefined
});
assert.equal(status.ready, false);
assert.equal(status.source, "missing");
assert.deepEqual(status.managedInstall?.failure, {
stage: "verify",
reason: "checksum-mismatch",
message: "Checksum mismatch for patchloom-aarch64-apple-darwin.tar.xz."
});
assert.deepEqual(status.diagnostics, [
"Managed install last failure stage: verify",
"Managed install last failure reason: checksum-mismatch",
"Managed install diagnostic: Checksum mismatch for patchloom-aarch64-apple-darwin.tar.xz."
]);
} finally {
clearManagedInstallFailure();
await fs.rm(storageRoot, { recursive: true, force: true });
}
});
test("normalizeReleaseVersion strips v and patchloom-v prefixes", () => {
assert.equal(normalizeReleaseVersion("v0.1.0"), "0.1.0");
assert.equal(normalizeReleaseVersion("0.1.0"), "0.1.0");
assert.equal(normalizeReleaseVersion("patchloom-v0.1.2"), "0.1.2");
});
test("buildManagedInstallReleaseAssets normalizes patchloom-v prefixed versions", () => {
const target = detectManagedInstallTarget("darwin", "arm64");
assert.ok(target);
const release = buildManagedInstallReleaseAssets("patchloom-v0.1.2", target);
assert.equal(release.tagName, "patchloom-v0.1.2");
assert.match(release.archiveDownloadUrl, /\/patchloom-v0\.1\.2\/patchloom-aarch64-apple-darwin\.tar\.xz$/);
});
test("resolvePatchloomStatusWithInputs falls back to a managed install when present", async () => {
const expectedBinaryPath = path.join("/managed/install", "0.1.0", "managed-bin", "patchloom");
const status = await resolvePatchloomStatusWithInputs({
configuredPath: "",
pathValue: "/usr/local/bin:/bin",
platform: "darwin",
arch: "arm64",
managedInstallRoot: "/managed/install",
managedInstallVersion: "0.1.0",
managedFileExists: async (filePath) => filePath === expectedBinaryPath,
canExecute: async (candidate) => candidate === expectedBinaryPath,
getVersion: async () => "patchloom 0.1.0"
});
assert.equal(status.ready, true);
assert.equal(status.source, "managed");
assert.equal(status.binaryPath, expectedBinaryPath);
assert.equal(status.managedInstall?.exists, true);
});
test("configuredBinaryPathFromSetting trims whitespace and returns undefined for empty values", () => {
assert.equal(configuredBinaryPathFromSetting("/usr/local/bin/patchloom"), "/usr/local/bin/patchloom");
assert.equal(configuredBinaryPathFromSetting(" /custom/patchloom "), "/custom/patchloom");
assert.equal(configuredBinaryPathFromSetting(""), undefined);
assert.equal(configuredBinaryPathFromSetting(" "), undefined);
assert.equal(configuredBinaryPathFromSetting(undefined), undefined);
});
test("describePatchloomSource maps all source types to labels", () => {
assert.equal(describePatchloomSource("setting"), "patchloom.path");
assert.equal(describePatchloomSource("path"), "PATH");
assert.equal(describePatchloomSource("managed"), "managed install");
assert.equal(describePatchloomSource("missing"), "not found");
});
test("describePatchloomCompatibility maps all compatibility levels to labels", () => {
assert.equal(describePatchloomCompatibility("supported"), "supported");
assert.equal(describePatchloomCompatibility("unsupported"), "upgrade required");
assert.equal(describePatchloomCompatibility("unknown"), "unable to verify");
assert.equal(describePatchloomCompatibility(undefined), "unknown");
});
// --- patchloomNeedsUpgrade tests ---
test("patchloomNeedsUpgrade returns true only for unsupported compatibility", () => {
assert.equal(patchloomNeedsUpgrade({ compatibility: "unsupported" } as any), true);
assert.equal(patchloomNeedsUpgrade({ compatibility: "supported" } as any), false);
assert.equal(patchloomNeedsUpgrade({ compatibility: "unknown" } as any), false);
assert.equal(patchloomNeedsUpgrade({ compatibility: undefined } as any), false);
});
// --- isTrustedManagedInstallDownloadUrl tests ---
test("isTrustedManagedInstallDownloadUrl accepts valid GitHub release URLs", () => {
assert.equal(
isTrustedManagedInstallDownloadUrl("https://github.com/patchloom/patchloom/releases/download/v0.1.0/archive.tar.xz"),
true
);
assert.equal(
isTrustedManagedInstallDownloadUrl("https://github.com/patchloom/patchloom/releases/download/v1.2.3/patchloom-x86_64.zip"),
true
);
});
test("isTrustedManagedInstallDownloadUrl rejects protocol-relative and non-HTTPS URLs", () => {
assert.equal(
isTrustedManagedInstallDownloadUrl("http://github.com/patchloom/patchloom/releases/download/v0.1.0/x"),
false
);
assert.equal(
isTrustedManagedInstallDownloadUrl("//github.com/patchloom/patchloom/releases/download/v0.1.0/x"),
false
);
});
test("isTrustedManagedInstallDownloadUrl handles query strings, fragments, and encoded paths", () => {
assert.equal(
isTrustedManagedInstallDownloadUrl(
"https://github.com/patchloom/patchloom/releases/download/v1.2.3/patchloom-x86_64.zip?version=1.0"
),
true
);
assert.equal(
isTrustedManagedInstallDownloadUrl(
"https://github.com/patchloom/patchloom/releases/download/v1.2.3/patchloom-x86_64.zip#sha256"
),
true
);
});
test("isTrustedManagedInstallDownloadUrl rejects non-GitHub hosts", () => {
assert.equal(
isTrustedManagedInstallDownloadUrl("https://evil.com/patchloom/patchloom/releases/download/v0.1.0/x"),
false
);
});
test("isTrustedManagedInstallDownloadUrl rejects wrong repo path", () => {
assert.equal(
isTrustedManagedInstallDownloadUrl("https://github.com/other/repo/releases/download/v0.1.0/x"),
false
);
});
test("isTrustedManagedInstallDownloadUrl rejects malformed URLs", () => {
assert.equal(isTrustedManagedInstallDownloadUrl("not-a-url"), false);
assert.equal(isTrustedManagedInstallDownloadUrl(""), false);
});
test("isTrustedManagedInstallDownloadUrl respects custom repo parameter", () => {
assert.equal(
isTrustedManagedInstallDownloadUrl(
"https://github.com/custom/repo/releases/download/v1.0.0/file.tar.xz",
"custom/repo"
),
true
);
assert.equal(
isTrustedManagedInstallDownloadUrl(
"https://github.com/patchloom/patchloom/releases/download/v1.0.0/file.tar.xz",
"custom/repo"
),
false
);
});