Repository navigation
Expand file tree
/
Copy pathtest-cli-package.mjs
More file actions
1242 lines (1128 loc) · 51.5 KB
/
Copy pathtest-cli-package.mjs
File metadata and controls
1242 lines (1128 loc) · 51.5 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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import assert from 'node:assert/strict';
import crypto from 'node:crypto';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { execFileSync, spawnSync } from 'node:child_process';
import { after, before, describe, test } from 'node:test';
import { fileURLToPath } from 'node:url';
import {
INSTALLER_VERSION,
MANAGED_SECTION_START,
MANAGED_SECTION_END,
} from './install-skills.mjs';
import { ensureMainRepoGitRestored } from './generate-current-audit.mjs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const ROOT = path.resolve(__dirname, '..');
function sha256Buffer(buf) {
return crypto.createHash('sha256').update(buf).digest('hex');
}
function sha256File(filePath) {
return sha256Buffer(fs.readFileSync(filePath));
}
// Strict character allow/deny check for Windows .cmd batch invocation via cmd.exe /d /c call.
// Rejects cmd.exe metacharacters, variable expansion markers, quotes, and control characters
// so untrusted input can never inject shell commands, while permitting spaces and Unicode paths.
const UNSAFE_WINDOWS_CMD_METACHARS = /["%&|<>^()\r\n\0!;`]/;
export function assertSafeWindowsCmdArgs(executablePath, args = []) {
if (typeof executablePath !== 'string' || executablePath.length === 0) {
throw new Error('Windows .cmd executablePath must be a non-empty string');
}
if (UNSAFE_WINDOWS_CMD_METACHARS.test(executablePath)) {
throw new Error(
`Unsafe executable path rejected for Windows .cmd execution: ${JSON.stringify(executablePath)}`,
);
}
if (!Array.isArray(args)) {
throw new Error('Windows .cmd args must be an array of strings');
}
for (const arg of args) {
if (typeof arg !== 'string') {
throw new Error(`Windows .cmd argument must be a string, got ${typeof arg}`);
}
if (UNSAFE_WINDOWS_CMD_METACHARS.test(arg)) {
throw new Error(
`Unsafe argument rejected for Windows .cmd execution: ${JSON.stringify(arg)}`,
);
}
}
}
export function resolveWindowsComSpec(env = process.env) {
const candidate = env.ComSpec || env.COMSPEC;
if (typeof candidate === 'string' && candidate.trim().length > 0) {
return candidate.trim();
}
const sysRoot = env.SystemRoot || env.SYSTEMROOT || 'C:\\Windows';
return path.win32.join(sysRoot, 'System32', 'cmd.exe');
}
export function resolveNpmCliScript(
scriptName,
{ execPath = process.execPath, env = process.env } = {},
) {
if (scriptName === 'npm-cli.js' && typeof env.npm_execpath === 'string' && env.npm_execpath.length > 0) {
if (/\.[cm]?js$/i.test(env.npm_execpath) && fs.existsSync(env.npm_execpath)) {
return env.npm_execpath;
}
}
if (typeof env.npm_execpath === 'string' && env.npm_execpath.length > 0) {
const sibling = path.join(path.dirname(env.npm_execpath), scriptName);
if (fs.existsSync(sibling)) {
return sibling;
}
}
const execDir = path.dirname(execPath);
const candidates = [
path.join(execDir, 'node_modules', 'npm', 'bin', scriptName),
path.join(execDir, '..', 'lib', 'node_modules', 'npm', 'bin', scriptName),
];
for (const candidate of candidates) {
if (fs.existsSync(candidate)) {
return candidate;
}
}
return null;
}
export function buildSubprocessInvocation({
platform = process.platform,
executablePath,
args = [],
comSpec,
env = process.env,
}) {
if (typeof executablePath !== 'string' || executablePath.length === 0) {
throw new Error('executablePath must be a non-empty string');
}
if (!Array.isArray(args) || !args.every((a) => typeof a === 'string')) {
throw new Error('args must be an array of strings');
}
// On Windows, .cmd/.bat shims cannot be spawned directly by execFileSync without a command interpreter
// (Node CVE-2024-27980 throws EINVAL). Using cmd.exe with ['/d', '/c', 'call', executablePath, ...args]
// avoids shell: true and avoids string concatenation: 'call' ensures the first token after '/c' is not
// a quote character so cmd.exe never strips quotes when both executablePath and args contain spaces.
if (platform === 'win32' && /\.(?:cmd|bat)$/i.test(executablePath)) {
assertSafeWindowsCmdArgs(executablePath, args);
return {
file: comSpec || resolveWindowsComSpec(env),
args: ['/d', '/c', 'call', executablePath, ...args],
windowsHide: true,
};
}
return {
file: executablePath,
args: [...args],
windowsHide: true,
};
}
export function execNpmTool(toolName, args, options = {}) {
const scriptName = toolName === 'npx' ? 'npx-cli.js' : 'npm-cli.js';
const cliJsPath = resolveNpmCliScript(scriptName);
if (cliJsPath) {
return execFileSync(process.execPath, [cliJsPath, ...args], {
windowsHide: true,
...options,
});
}
const executablePath = process.platform === 'win32' ? `${toolName}.cmd` : toolName;
const invocation = buildSubprocessInvocation({ executablePath, args });
return execFileSync(invocation.file, invocation.args, {
windowsHide: invocation.windowsHide,
...options,
});
}
const IS_DIRECT_CLI_PACKAGE_TEST = process.argv
.slice(1)
.some((arg) => !arg.startsWith('-') && path.resolve(arg) === __filename);
if (IS_DIRECT_CLI_PACKAGE_TEST) {
describe('Distributable CLI & Package Readiness', () => {
let suiteTmpDir;
let packDir;
let consumerDir;
let unrelatedCwd;
let packMetadata;
let tarballPath;
let installedBinPath;
let installedJsEntrypoint;
let installedPkgRoot;
before(() => {
ensureMainRepoGitRestored(ROOT);
suiteTmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'blendops-test-cli-package-'));
packDir = path.join(suiteTmpDir, 'pack');
consumerDir = path.join(suiteTmpDir, 'consumer');
unrelatedCwd = path.join(suiteTmpDir, 'unrelated-cwd');
fs.mkdirSync(packDir, { recursive: true });
fs.mkdirSync(consumerDir, { recursive: true });
fs.mkdirSync(unrelatedCwd, { recursive: true });
fs.writeFileSync(
path.join(consumerDir, 'package.json'),
`${JSON.stringify({ name: 'blendops-consumer-fixture', version: '1.0.0', private: true }, null, 2)}\n`,
'utf8',
);
const packJsonRaw = execNpmTool(
'npm',
['pack', '--pack-destination', packDir, '--json'],
{ cwd: ROOT, encoding: 'utf8' },
);
const parsedPack = JSON.parse(packJsonRaw);
assert.ok(Array.isArray(parsedPack) && parsedPack.length === 1, 'npm pack --json must return 1 package entry');
packMetadata = parsedPack[0];
tarballPath = path.join(packDir, packMetadata.filename);
assert.ok(fs.existsSync(tarballPath), `Packed tarball must exist at ${tarballPath}`);
execNpmTool(
'npm',
['install', '--prefer-offline', '--no-audit', '--no-fund', tarballPath],
{ cwd: consumerDir, stdio: ['ignore', 'pipe', 'pipe'] },
);
installedBinPath = path.join(
consumerDir,
'node_modules',
'.bin',
process.platform === 'win32' ? 'blendops.cmd' : 'blendops',
);
installedPkgRoot = path.join(consumerDir, 'node_modules', 'blendops');
installedJsEntrypoint = path.join(installedPkgRoot, 'bin', 'blendops.mjs');
});
after(() => {
if (suiteTmpDir && fs.existsSync(suiteTmpDir)) {
fs.rmSync(suiteTmpDir, { recursive: true, force: true });
}
});
function runInstalledCli(
args,
{ cwd = unrelatedCwd, env = {}, via = 'shim', binPath = installedBinPath, jsEntrypoint = installedJsEntrypoint } = {},
) {
if (via === 'js') {
return execFileSync(process.execPath, [jsEntrypoint, ...args], {
cwd,
encoding: 'utf8',
env: { ...process.env, ...env },
stdio: ['ignore', 'pipe', 'pipe'],
windowsHide: true,
});
}
const invocation = buildSubprocessInvocation({
executablePath: binPath,
args,
});
return execFileSync(invocation.file, invocation.args, {
cwd,
encoding: 'utf8',
env: { ...process.env, ...env },
stdio: ['ignore', 'pipe', 'pipe'],
windowsHide: invocation.windowsHide,
});
}
function spawnInstalledCli(
args,
{ cwd = unrelatedCwd, env = {}, via = 'shim', binPath = installedBinPath, jsEntrypoint = installedJsEntrypoint } = {},
) {
const file = via === 'js' ? process.execPath : buildSubprocessInvocation({ executablePath: binPath, args }).file;
const finalArgs =
via === 'js'
? [jsEntrypoint, ...args]
: buildSubprocessInvocation({ executablePath: binPath, args }).args;
const spawned = spawnSync(file, finalArgs, {
cwd,
encoding: 'utf8',
env: { ...process.env, ...env },
stdio: ['ignore', 'pipe', 'pipe'],
windowsHide: true,
maxBuffer: 16 * 1024 * 1024,
});
if (spawned.error) {
throw spawned.error;
}
return {
status: spawned.status,
signal: spawned.signal,
stdout: String(spawned.stdout || ''),
stderr: String(spawned.stderr || ''),
};
}
function runInstalledCliExpectFailure(
args,
{ cwd = unrelatedCwd, env = {}, via = 'shim', binPath = installedBinPath, jsEntrypoint = installedJsEntrypoint } = {},
) {
try {
runInstalledCli(args, { cwd, env, via, binPath, jsEntrypoint });
assert.fail(`Expected blendops ${args.join(' ')} to fail with non-zero exit code`);
} catch (err) {
if (err && err.code === 'ERR_ASSERTION') {
throw err;
}
return {
status: err.status,
stdout: String(err.stdout || ''),
stderr: String(err.stderr || ''),
};
}
}
test('Scenario A: CLI contract (--version, -v, --help, -h, subcommand help, and invalid commands/flags fail with non-zero exit code and zero writes)', () => {
const emptyTargetDir = path.join(suiteTmpDir, 'scenario-a-empty');
fs.mkdirSync(emptyTargetDir, { recursive: true });
// 1. --version, -v, and version subcommand
for (const verArgs of [['--version'], ['-v'], ['version']]) {
const out = runInstalledCli(verArgs);
assert.equal(out.trim(), `blendops ${INSTALLER_VERSION}`);
}
// 2. --help, -h, help subcommand, and subcommand --help
for (const helpArgs of [
['--help'],
['-h'],
['help'],
['doctor', '--help'],
['install', '--help'],
['rollback', '--help'],
]) {
const helpOut = runInstalledCli(helpArgs);
assert.match(helpOut, /blendops doctor/);
assert.match(helpOut, /blendops install/);
assert.match(helpOut, /blendops rollback/);
assert.match(helpOut, /Runtime status: Not Run/);
assert.match(helpOut, /Artifact status: Not Produced/);
}
// 3. Invalid commands and flags fail closed with non-zero exit code and write nothing
const invalidInvocations = [
{ args: [], match: /Missing operation mode/ },
{ args: ['unknown-subcommand'], match: /Unknown or malformed CLI argument: unknown-subcommand/ },
{ args: ['install', '--project', emptyTargetDir], match: /Missing operation mode for 'install'/ },
{
args: ['install', '--project', emptyTargetDir, '--dry-run', '--write'],
match: /Conflicting operation modes/,
},
{
args: ['install', '--project', emptyTargetDir, '--doctor'],
match: /Conflicting operation modes/,
},
{
args: ['install', '--project', emptyTargetDir, '--dry-run', '--allow-global'],
match: /Global installation \(--allow-global\) is disabled/,
},
{
args: ['rollback'],
match: /--rollback requires --project <path> \(or --out <path>\)/,
},
{
args: ['install', '--target', 'unknown-agent', '--project', emptyTargetDir, '--dry-run'],
match: /Unsupported --target 'unknown-agent'/,
},
];
for (const { args, match } of invalidInvocations) {
const failed = runInstalledCliExpectFailure(args);
assert.notEqual(failed.status, 0);
assert.match(failed.stderr, match);
assert.match(failed.stderr, /Runtime status: Not Run/);
assert.match(failed.stderr, /Artifact status: Not Produced/);
}
assert.deepEqual(
fs.readdirSync(emptyTargetDir),
[],
'Invalid or help/version CLI invocations must write zero files to target directory',
);
});
test('Scenario B: Packed-package integrity (npm pack contains all required runtime/content assets and excludes tests, dev-server, audits, and git artifacts)', () => {
assert.equal(packMetadata.name, 'blendops');
assert.equal(packMetadata.version, INSTALLER_VERSION);
const packedPaths = new Set(packMetadata.files.map((f) => f.path));
// Required top-level and runtime files
const requiredFiles = [
'package.json',
'README.md',
'LICENSE',
'CHANGELOG.md',
'bin/blendops.mjs',
'scripts/install-skills.mjs',
'scripts/cli/installer.mjs',
'scripts/cli/constants.mjs',
'scripts/export-claude-skills.mjs',
'laws/README.md',
'packs/README.md',
'bundles/generic-project-local/BLENDOPS.md',
'bundles/generic-project-local/ROLLBACK.md',
'bundles/generic-project-local/INSTALL_REPORT_TEMPLATE.md',
'bundles/generic-project-local/manifest.json',
'bundles/claude-desktop-manual/README.md',
'bundles/claude-desktop-manual/SKILL.md',
'bundles/claude-desktop-manual/BLENDOPS_SINGLE_FILE.md',
'bundles/claude-desktop-manual/IMPORT_STEPS.md',
'bundles/claude-desktop-manual/manifest.json',
'registry/compatibility-matrix.json',
'registry/skills.json',
];
for (const reqPath of requiredFiles) {
assert.ok(packedPaths.has(reqPath), `Packed tarball must include required file '${reqPath}'`);
}
// All 16 canonical skills must have both SKILL.md and EVAL.md in the packed tarball
const skillDirs = fs
.readdirSync(path.join(ROOT, 'skills'), { withFileTypes: true })
.filter((d) => d.isDirectory() && d.name !== '_template')
.map((d) => d.name)
.sort();
assert.equal(skillDirs.length, 16, 'Repository must have 16 canonical skills');
for (const skillName of skillDirs) {
assert.ok(
packedPaths.has(`skills/${skillName}/SKILL.md`),
`Packed tarball missing skills/${skillName}/SKILL.md`,
);
assert.ok(
packedPaths.has(`skills/${skillName}/EVAL.md`),
`Packed tarball missing skills/${skillName}/EVAL.md`,
);
}
// Excluded repository-internal files must NOT be in the packed tarball
for (const filePath of packedPaths) {
assert.ok(
!filePath.startsWith('scripts/test-'),
`Packed tarball must not include test script: ${filePath}`,
);
assert.notEqual(
filePath,
'scripts/dev-server.mjs',
'Packed tarball must not include scripts/dev-server.mjs',
);
assert.ok(
!filePath.startsWith('docs/audits/'),
`Packed tarball must not include audit snapshots: ${filePath}`,
);
assert.ok(
!filePath.startsWith('dist/'),
`Packed tarball must not include dist/ artifacts: ${filePath}`,
);
assert.ok(
!filePath.startsWith('.aistudio/'),
`Packed tarball must not include .aistudio/ artifacts: ${filePath}`,
);
assert.ok(
!filePath.startsWith('.git/'),
`Packed tarball must not include .git/ metadata: ${filePath}`,
);
}
});
test('Scenario C: Consumer installation works from an unrelated working directory via node_modules/.bin shim, packaged bin/blendops.mjs entrypoint, and npx --no-install blendops', () => {
assert.ok(fs.existsSync(installedBinPath), 'node_modules/.bin/blendops shim must exist in consumer project');
assert.ok(fs.existsSync(installedJsEntrypoint), 'node_modules/blendops/bin/blendops.mjs must exist in consumer project');
assert.ok(fs.existsSync(installedPkgRoot), 'node_modules/blendops must exist in consumer project');
assert.ok(
fs.existsSync(path.join(consumerDir, 'node_modules', 'fflate')),
'Runtime dependency fflate must be installed in consumer node_modules',
);
// 1. Invoke via generated npm bin shim (node_modules/.bin/blendops or blendops.cmd on Windows) from an unrelated cwd
const binVer = runInstalledCli(['--version'], { cwd: unrelatedCwd, via: 'shim' });
assert.equal(binVer.trim(), `blendops ${INSTALLER_VERSION}`);
// 2. Invoke via packaged JavaScript entrypoint (node_modules/blendops/bin/blendops.mjs) from an unrelated cwd
const jsVer = runInstalledCli(['--version'], { cwd: unrelatedCwd, via: 'js' });
assert.equal(jsVer.trim(), `blendops ${INSTALLER_VERSION}`);
const jsDoc = runInstalledCli(['doctor'], { cwd: unrelatedCwd, via: 'js' });
assert.match(jsDoc, /Mode: doctor/);
assert.match(jsDoc, /16 canonical skills verified/);
assert.match(jsDoc, /Runtime status: Not Run/);
assert.match(jsDoc, /Artifact status: Not Produced/);
// 3. Invoke via npx --no-install blendops inside consumerDir
const npxVer = execNpmTool('npx', ['--no-install', 'blendops', '--version'], {
cwd: consumerDir,
encoding: 'utf8',
});
assert.equal(npxVer.trim(), `blendops ${INSTALLER_VERSION}`);
// 4. Verify backward-compatible npm run skills:install -- --help in the source repository
const repoScriptHelp = execFileSync(
process.execPath,
[path.join(ROOT, 'scripts', 'install-skills.mjs'), '--help'],
{ cwd: unrelatedCwd, encoding: 'utf8' },
);
assert.match(repoScriptHelp, /blendops install/);
});
test('catalog-validate resolves catalog references from the installed tarball', () => {
const result = JSON.parse(runInstalledCli(['catalog-validate', '--json']));
assert.equal(result.valid, true);
assert.ok(result.counts.knowledge_records > 0);
});
test('catalog-eval runs the retrieval suite from the installed tarball', () => {
const result = JSON.parse(runInstalledCli(['catalog-eval', '--json']));
assert.equal(result.passed, true);
assert.ok(result.total > 0);
assert.equal(result.passed_count, result.total);
});
test('Scenario D: Doctor mode from installed tarball inspects package health, target markers, manifests, and journals with zero target mutations', () => {
const docBase = path.join(suiteTmpDir, 'scenario-d');
const cleanProj = path.join(docBase, 'clean-proj');
const cursorProj = path.join(docBase, 'cursor-proj');
const ambiguousProj = path.join(docBase, 'ambiguous-proj');
const journalProj = path.join(docBase, 'journal-proj');
fs.mkdirSync(cleanProj, { recursive: true });
fs.mkdirSync(path.join(cursorProj, '.cursor'), { recursive: true });
fs.mkdirSync(path.join(ambiguousProj, '.claude'), { recursive: true });
fs.mkdirSync(path.join(ambiguousProj, '.cursor'), { recursive: true });
fs.mkdirSync(path.join(journalProj, '.blendops'), { recursive: true });
fs.writeFileSync(
path.join(journalProj, '.blendops', 'transaction-journal.json'),
JSON.stringify({ installer_version: INSTALLER_VERSION }),
'utf8',
);
// 1. Package-level doctor without --project
const pkgDocOut = runInstalledCli(['doctor']);
assert.match(pkgDocOut, /Mode: doctor/);
assert.match(pkgDocOut, /16 canonical skills verified/);
assert.match(pkgDocOut, /Runtime status: Not Run/);
assert.match(pkgDocOut, /Artifact status: Not Produced/);
// 2. Clean project doctor
const cleanDocOut = runInstalledCli(['doctor', '--project', cleanProj]);
assert.match(cleanDocOut, /Target selected: generic-project/);
assert.match(cleanDocOut, /Detected markers: none/);
assert.deepEqual(fs.readdirSync(cleanProj), [], 'doctor must not mutate cleanProj');
// 3. Single-marker project doctor
const cursorDocOut = runInstalledCli(['doctor', '--project', cursorProj]);
assert.match(cursorDocOut, /Target selected: cursor/);
assert.match(cursorDocOut, /Detected markers: cursor \(\.cursor\)/);
assert.deepEqual(fs.readdirSync(cursorProj), ['.cursor'], 'doctor must not mutate cursorProj');
// 4. Ambiguous multi-marker project doctor
const ambigDocOut = runInstalledCli(['doctor', '--project', ambiguousProj]);
assert.match(ambigDocOut, /Multiple target markers detected \(claude-code, cursor\)/);
assert.deepEqual(
fs.readdirSync(ambiguousProj).sort(),
['.claude', '.cursor'],
'doctor must not mutate ambiguousProj',
);
// 5. Pending transaction journal detection (strictly read-only!)
const journalDocOut = runInstalledCli(['doctor', '--project', journalProj]);
assert.match(journalDocOut, /Pending transaction journal detected/);
assert.ok(
fs.existsSync(path.join(journalProj, '.blendops', 'transaction-journal.json')),
'doctor must remain strictly read-only and not delete transaction-journal.json',
);
});
test('Scenario E: Full install lifecycle from installed tarball (dry-run gate, stale-plan rejection, write, idempotent rerun, and user-edit preservation)', () => {
const envPlans = { BLENDOPS_INSTALLER_PLAN_DIR: path.join(suiteTmpDir, 'plans-e') };
const projDir = path.join(suiteTmpDir, 'scenario-e-proj');
fs.mkdirSync(projDir, { recursive: true });
const origAgents = '# Consumer Project Agents\n\nKeep this header intact.\n';
fs.writeFileSync(path.join(projDir, 'AGENTS.md'), origAgents, 'utf8');
const origAgentsSha = sha256File(path.join(projDir, 'AGENTS.md'));
// 1. --write before --dry-run fails closed
const writeBeforeDry = runInstalledCliExpectFailure(
['install', '--target', 'generic-project', '--project', projDir, '--patch-agents-md', '--write'],
{ env: envPlans },
);
assert.match(writeBeforeDry.stderr, /no prior --dry-run plan exists/);
assert.ok(!fs.existsSync(path.join(projDir, 'BLENDOPS.md')));
// 2. --dry-run records plan without modifying target files
const dryOut = runInstalledCli(
['install', '--target', 'generic-project', '--project', projDir, '--patch-agents-md', '--dry-run'],
{ env: envPlans },
);
assert.match(dryOut, /Mode: dry-run/);
assert.match(dryOut, /Files created: BLENDOPS\.md/);
assert.match(dryOut, /Files modified: AGENTS\.md/);
assert.ok(!fs.existsSync(path.join(projDir, 'BLENDOPS.md')));
assert.equal(sha256File(path.join(projDir, 'AGENTS.md')), origAgentsSha);
// 3. Stale plan rejection when target file changes between --dry-run and --write
fs.writeFileSync(path.join(projDir, 'AGENTS.md'), `${origAgents}\nExtra line before write.\n`, 'utf8');
const staleWrite = runInstalledCliExpectFailure(
['install', '--target', 'generic-project', '--project', projDir, '--patch-agents-md', '--write'],
{ env: envPlans },
);
assert.match(staleWrite.stderr, /stale dry-run plan/);
fs.writeFileSync(path.join(projDir, 'AGENTS.md'), origAgents, 'utf8');
// 4. Fresh --dry-run followed by --write succeeds
runInstalledCli(
['install', '--target', 'generic-project', '--project', projDir, '--patch-agents-md', '--dry-run'],
{ env: envPlans },
);
const writeOut = runInstalledCli(
['install', '--target', 'generic-project', '--project', projDir, '--patch-agents-md', '--write'],
{ env: envPlans },
);
assert.match(writeOut, /Mode: write/);
assert.match(writeOut, /Runtime status: Not Run/);
assert.match(writeOut, /Artifact status: Not Produced/);
assert.ok(fs.existsSync(path.join(projDir, 'BLENDOPS.md')));
assert.ok(fs.existsSync(path.join(projDir, '.blendops', 'install-manifest.json')));
assert.ok(fs.existsSync(path.join(projDir, '.blendops', 'INSTALL_REPORT.md')));
assert.ok(fs.existsSync(path.join(projDir, '.blendops', 'backups', 'AGENTS.md.bak')));
assert.equal(sha256File(path.join(projDir, '.blendops', 'backups', 'AGENTS.md.bak')), origAgentsSha);
const patchedAgents = fs.readFileSync(path.join(projDir, 'AGENTS.md'), 'utf8');
assert.ok(patchedAgents.includes(origAgents.trim()));
assert.ok(patchedAgents.includes(MANAGED_SECTION_START));
assert.ok(patchedAgents.includes(MANAGED_SECTION_END));
// 5. Rerun is idempotent
runInstalledCli(
['install', '--target', 'generic-project', '--project', projDir, '--patch-agents-md', '--dry-run'],
{ env: envPlans },
);
const rerunWriteOut = runInstalledCli(
['install', '--target', 'generic-project', '--project', projDir, '--patch-agents-md', '--write'],
{ env: envPlans },
);
assert.match(rerunWriteOut, /Files created: none/);
assert.match(rerunWriteOut, /Files modified: none/);
// 6. User edits outside the managed section in installer-created BLENDOPS.md transition it to files_modified with backup
const userNote = '\n## Custom Team Notes\nDo not delete our team notes.\n';
fs.appendFileSync(path.join(projDir, 'BLENDOPS.md'), userNote, 'utf8');
const userEditedBlendopsSha = sha256File(path.join(projDir, 'BLENDOPS.md'));
runInstalledCli(
['install', '--target', 'generic-project', '--project', projDir, '--patch-agents-md', '--dry-run'],
{ env: envPlans },
);
runInstalledCli(
['install', '--target', 'generic-project', '--project', projDir, '--patch-agents-md', '--write'],
{ env: envPlans },
);
const updatedManifest = JSON.parse(
fs.readFileSync(path.join(projDir, '.blendops', 'install-manifest.json'), 'utf8'),
);
assert.ok(!updatedManifest.files_created.includes('BLENDOPS.md'));
assert.ok(updatedManifest.files_modified.includes('BLENDOPS.md'));
assert.equal(
sha256File(path.join(projDir, '.blendops', 'backups', 'BLENDOPS.md.bak')),
userEditedBlendopsSha,
);
});
test('Scenario F: Rollback lifecycle from installed tarball restores modified files, removes eligible created files, preserves unrelated files, and guards against post-install drift', () => {
const envPlans = { BLENDOPS_INSTALLER_PLAN_DIR: path.join(suiteTmpDir, 'plans-f') };
const projDir = path.join(suiteTmpDir, 'scenario-f-proj');
fs.mkdirSync(projDir, { recursive: true });
const origClaude = '# Existing Claude Code Config\n\nProject-specific notes.\n';
const unrelatedReadme = '# Unrelated App README\n';
fs.writeFileSync(path.join(projDir, 'CLAUDE.md'), origClaude, 'utf8');
fs.writeFileSync(path.join(projDir, 'README.md'), unrelatedReadme, 'utf8');
const origClaudeSha = sha256File(path.join(projDir, 'CLAUDE.md'));
const origReadmeSha = sha256File(path.join(projDir, 'README.md'));
// Install claude-code target (creates BLENDOPS.md, modifies CLAUDE.md with backup)
runInstalledCli(
['install', '--target', 'claude-code', '--project', projDir, '--dry-run'],
{ env: envPlans },
);
runInstalledCli(
['install', '--target', 'claude-code', '--project', projDir, '--write'],
{ env: envPlans },
);
assert.ok(fs.existsSync(path.join(projDir, 'BLENDOPS.md')));
assert.notEqual(sha256File(path.join(projDir, 'CLAUDE.md')), origClaudeSha);
// 1. Simulate post-install user edit to BLENDOPS.md: rollback without --force-rollback must refuse
const postInstallBlendops = fs.readFileSync(path.join(projDir, 'BLENDOPS.md'), 'utf8');
fs.writeFileSync(path.join(projDir, 'BLENDOPS.md'), `${postInstallBlendops}\nUser edit after install\n`, 'utf8');
const refusedRollback = runInstalledCliExpectFailure(
['rollback', '--project', projDir],
{ env: envPlans },
);
assert.match(refusedRollback.stderr, /modified after installation/);
assert.ok(fs.existsSync(path.join(projDir, 'BLENDOPS.md')), 'BLENDOPS.md must remain after refused rollback');
// Restore exact post-install BLENDOPS.md and perform clean rollback
fs.writeFileSync(path.join(projDir, 'BLENDOPS.md'), postInstallBlendops, 'utf8');
const rollbackOut = runInstalledCli(['rollback', '--project', projDir], { env: envPlans });
assert.match(rollbackOut, /Mode: rollback/);
assert.match(rollbackOut, /Runtime status: Not Run/);
assert.match(rollbackOut, /Artifact status: Not Produced/);
assert.ok(!fs.existsSync(path.join(projDir, 'BLENDOPS.md')), 'BLENDOPS.md must be removed on rollback');
assert.ok(!fs.existsSync(path.join(projDir, '.blendops')), '.blendops directory must be pruned on clean rollback');
assert.equal(
sha256File(path.join(projDir, 'CLAUDE.md')),
origClaudeSha,
'CLAUDE.md must be restored byte-for-byte to pre-install SHA-256',
);
assert.equal(
sha256File(path.join(projDir, 'README.md')),
origReadmeSha,
'Unrelated README.md must remain untouched',
);
});
test('Scenario G: Transaction recovery and package integrity from installed tarball (interrupted transaction journal recovery and corrupted package asset fail-closed check)', () => {
const envPlans = { BLENDOPS_INSTALLER_PLAN_DIR: path.join(suiteTmpDir, 'plans-g') };
const projDir = path.join(suiteTmpDir, 'scenario-g-proj');
fs.mkdirSync(projDir, { recursive: true });
const origAgents = '# Original Pre-Crash AGENTS.md\n';
fs.writeFileSync(path.join(projDir, 'AGENTS.md'), origAgents, 'utf8');
const origAgentsSha = sha256File(path.join(projDir, 'AGENTS.md'));
// 1. Simulate an interrupted write transaction with a valid pending transaction-journal.json
const blendopsDir = path.join(projDir, '.blendops');
const txStagingDir = path.join(blendopsDir, 'tx-staging');
const backupsDir = path.join(blendopsDir, 'backups');
fs.mkdirSync(txStagingDir, { recursive: true });
fs.mkdirSync(backupsDir, { recursive: true });
fs.writeFileSync(path.join(txStagingDir, 'target-0-AGENTS.md.bin'), origAgents, 'utf8');
fs.writeFileSync(path.join(backupsDir, 'AGENTS.md.bak'), origAgents, 'utf8');
fs.writeFileSync(path.join(projDir, 'AGENTS.md'), '# Partial Corrupted Write\n', 'utf8');
fs.writeFileSync(path.join(projDir, 'BLENDOPS.md'), '# Partial Created File\n', 'utf8');
fs.writeFileSync(path.join(blendopsDir, 'install-manifest.json'), '{"provisional":true}\n', 'utf8');
const journal = {
installer_version: INSTALLER_VERSION,
started_at: new Date().toISOString(),
project: fs.realpathSync(projDir),
blendops_existed_before: false,
backups_dir_existed_before: false,
manifest_state: {
rel_path: '.blendops/install-manifest.json',
existed_before: false,
pre_sha256: null,
staging_rel_path: null,
},
report_state: {
rel_path: '.blendops/INSTALL_REPORT.md',
existed_before: false,
pre_sha256: null,
staging_rel_path: null,
},
newly_created_backups: [
{
target_rel_path: 'AGENTS.md',
backup_rel_path: '.blendops/backups/AGENTS.md.bak',
sha256: origAgentsSha,
},
],
target_records: [
{
rel_path: 'AGENTS.md',
action: 'modify',
existed_before: true,
pre_sha256: origAgentsSha,
desired_sha256: '0'.repeat(64),
staging_rel_path: '.blendops/tx-staging/target-0-AGENTS.md.bin',
},
{
rel_path: 'BLENDOPS.md',
action: 'create',
existed_before: false,
pre_sha256: null,
desired_sha256: '1'.repeat(64),
staging_rel_path: null,
},
],
};
fs.writeFileSync(
path.join(blendopsDir, 'transaction-journal.json'),
`${JSON.stringify(journal, null, 2)}\n`,
'utf8',
);
// Running blendops install --dry-run automatically recovers the interrupted journal before planning
runInstalledCli(
['install', '--target', 'generic-project', '--project', projDir, '--dry-run'],
{ env: envPlans },
);
assert.equal(
sha256File(path.join(projDir, 'AGENTS.md')),
origAgentsSha,
'Interrupted transaction recovery must restore AGENTS.md byte-for-byte',
);
assert.ok(
!fs.existsSync(path.join(projDir, 'BLENDOPS.md')),
'Interrupted transaction recovery must remove partially created BLENDOPS.md',
);
assert.ok(
!fs.existsSync(path.join(projDir, '.blendops')),
'Interrupted transaction recovery must prune .blendops metadata directory',
);
// 2. Missing/corrupted package asset inside a mutated unpacked copy fails closed with zero target writes
const brokenConsumerDir = path.join(suiteTmpDir, 'broken-consumer');
fs.cpSync(consumerDir, brokenConsumerDir, { recursive: true });
const brokenBlendopsMd = path.join(
brokenConsumerDir,
'node_modules',
'blendops',
'bundles',
'generic-project-local',
'BLENDOPS.md',
);
fs.unlinkSync(brokenBlendopsMd);
const brokenBin = path.join(
brokenConsumerDir,
'node_modules',
'blendops',
'bin',
'blendops.mjs',
);
const cleanTarget = path.join(suiteTmpDir, 'scenario-g-clean-target');
fs.mkdirSync(cleanTarget, { recursive: true });
try {
execFileSync(
process.execPath,
[brokenBin, 'install', '--target', 'generic-project', '--project', cleanTarget, '--dry-run'],
{ cwd: unrelatedCwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] },
);
assert.fail('Expected broken package asset to fail closed');
} catch (err) {
assert.match(String(err.stderr || ''), /Missing required BlendOps source file/);
}
assert.deepEqual(fs.readdirSync(cleanTarget), [], 'Corrupted package must write zero files to target');
});
test('Scenario H: Claude Desktop export from installed tarball (dry-run, existing --out refusal, write 16 ZIPs + manual bundle, and rollback)', () => {
const envPlans = { BLENDOPS_INSTALLER_PLAN_DIR: path.join(suiteTmpDir, 'plans-h') };
const existingOutDir = path.join(suiteTmpDir, 'scenario-h-existing-out');
fs.mkdirSync(existingOutDir, { recursive: true });
fs.writeFileSync(path.join(existingOutDir, 'keep.txt'), 'preserve user file\n', 'utf8');
// 1. Existing --out directory is refused in --dry-run without modifying keep.txt
const refusedExisting = runInstalledCliExpectFailure(
['install', '--target', 'claude-desktop', '--out', existingOutDir, '--dry-run'],
{ env: envPlans },
);
assert.match(refusedExisting.stderr, /Refusing to use existing --out destination/);
assert.equal(fs.readFileSync(path.join(existingOutDir, 'keep.txt'), 'utf8'), 'preserve user file\n');
// 2. Non-existent --out directory succeeds in --dry-run and --write, then rolls back cleanly
const outDir = path.join(suiteTmpDir, 'scenario-h-desktop-out');
const dryOut = runInstalledCli(
['install', '--target', 'claude-desktop', '--output', outDir, '--dry-run'],
{ env: envPlans },
);
assert.match(dryOut, /Mode: dry-run/);
assert.match(dryOut, /SKILL\.md count if ZIP mode: 1 per ZIP \(16 ZIPs\)/);
assert.ok(!fs.existsSync(outDir), '--dry-run must not create --out directory');
const writeOut = runInstalledCli(
['install', '--target', 'claude-desktop', '--output', outDir, '--write'],
{ env: envPlans },
);
assert.match(writeOut, /Mode: write/);
assert.match(writeOut, /Runtime status: Not Run/);
assert.match(writeOut, /Artifact status: Not Produced/);
assert.match(writeOut, /Runtime setup: Separate; not performed/);
const zipFiles = fs.readdirSync(path.join(outDir, 'desktop-zips')).filter((f) => f.endsWith('.zip'));
assert.equal(zipFiles.length, 16, 'Must generate all 16 per-skill ZIP archives from installed tarball');
assert.ok(fs.existsSync(path.join(outDir, 'claude-desktop-manual', 'SKILL.md')));
assert.ok(fs.existsSync(path.join(outDir, 'manifest.json')));
assert.ok(fs.existsSync(path.join(outDir, '.blendops', 'install-manifest.json')));
assert.ok(fs.existsSync(path.join(outDir, '.blendops', 'INSTALL_REPORT.md')));
const rollbackOut = runInstalledCli(['rollback', '--out', outDir], { env: envPlans });
assert.match(rollbackOut, /Mode: rollback/);
assert.deepEqual(
fs.readdirSync(outDir),
[],
'Clean Claude Desktop bundle rollback must remove all generated bundle files and .blendops metadata',
);
});
test('Scenario I: Cross-platform paths with spaces and Unicode work end-to-end across npm install, packaged JS entrypoint, generated npm bin shim, and Windows .cmd invocation guard', () => {
const unicodeBase = path.join(suiteTmpDir, 'unicode space suite — Tiếng Việt テスト αβγ');
const unicodeConsumerDir = path.join(unicodeBase, 'consumer with spaces — 空間');
const unicodeProjectJs = path.join(unicodeBase, 'target project js — Dự Án テスト');
const unicodeProjectShim = path.join(unicodeBase, 'target project shim — Dự Án テスト');
const unicodePlanDir = path.join(unicodeBase, 'plans with spaces — 計画');
fs.mkdirSync(unicodeConsumerDir, { recursive: true });
fs.mkdirSync(unicodeProjectJs, { recursive: true });
fs.mkdirSync(unicodeProjectShim, { recursive: true });
fs.writeFileSync(
path.join(unicodeConsumerDir, 'package.json'),
`${JSON.stringify({ name: 'unicode-space-consumer', version: '1.0.0', private: true }, null, 2)}\n`,
'utf8',
);
// 1. npm install into a consumer path containing spaces and Unicode
execNpmTool(
'npm',
['install', '--prefer-offline', '--no-audit', '--no-fund', tarballPath],
{ cwd: unicodeConsumerDir, stdio: ['ignore', 'pipe', 'pipe'] },
);
const unicodeBinShim = path.join(
unicodeConsumerDir,
'node_modules',
'.bin',
process.platform === 'win32' ? 'blendops.cmd' : 'blendops',
);
const unicodeJsEntry = path.join(
unicodeConsumerDir,
'node_modules',
'blendops',
'bin',
'blendops.mjs',
);
assert.ok(fs.existsSync(unicodeBinShim), 'Generated npm command shim must exist in space+Unicode consumer path');
assert.ok(fs.existsSync(unicodeJsEntry), 'Packaged JS entrypoint must exist in space+Unicode consumer path');
const envUnicodePlans = { BLENDOPS_INSTALLER_PLAN_DIR: unicodePlanDir };
// 2. Full lifecycle (doctor -> dry-run -> write -> rollback) via packaged JS entrypoint on space+Unicode paths
const jsDoctor = runInstalledCli(['doctor', '--project', unicodeProjectJs], {
via: 'js',
jsEntrypoint: unicodeJsEntry,
env: envUnicodePlans,
});
assert.match(jsDoctor, /Mode: doctor/);
assert.match(jsDoctor, /Runtime status: Not Run/);
runInstalledCli(
['install', '--target', 'generic-project', '--project', unicodeProjectJs, '--dry-run'],
{ via: 'js', jsEntrypoint: unicodeJsEntry, env: envUnicodePlans },
);
runInstalledCli(
['install', '--target', 'generic-project', '--project', unicodeProjectJs, '--write'],
{ via: 'js', jsEntrypoint: unicodeJsEntry, env: envUnicodePlans },
);
assert.ok(fs.existsSync(path.join(unicodeProjectJs, 'BLENDOPS.md')));
runInstalledCli(['rollback', '--project', unicodeProjectJs], {
via: 'js',
jsEntrypoint: unicodeJsEntry,
env: envUnicodePlans,
});
assert.deepEqual(fs.readdirSync(unicodeProjectJs), []);
// 3. Full lifecycle (doctor -> dry-run -> write -> rollback) via generated npm command shim on space+Unicode paths
const shimDoctor = runInstalledCli(['doctor', '--project', unicodeProjectShim], {
via: 'shim',
binPath: unicodeBinShim,
env: envUnicodePlans,
});
assert.match(shimDoctor, /Mode: doctor/);
assert.match(shimDoctor, /Runtime status: Not Run/);
runInstalledCli(
['install', '--target', 'generic-project', '--project', unicodeProjectShim, '--dry-run'],
{ via: 'shim', binPath: unicodeBinShim, env: envUnicodePlans },
);
runInstalledCli(
['install', '--target', 'generic-project', '--project', unicodeProjectShim, '--write'],
{ via: 'shim', binPath: unicodeBinShim, env: envUnicodePlans },
);
assert.ok(fs.existsSync(path.join(unicodeProjectShim, 'BLENDOPS.md')));
runInstalledCli(['rollback', '--project', unicodeProjectShim], {
via: 'shim',
binPath: unicodeBinShim,
env: envUnicodePlans,
});
assert.deepEqual(fs.readdirSync(unicodeProjectShim), []);
// 4. Verify Windows .cmd invocation builder and strict untrusted-argument metacharacter rejection
const winCmdPath = 'C:\\Users\\Test User — Tiếng Việt\\app\\node_modules\\.bin\\blendops.cmd';
const winProjectPath = 'D:\\Workspaces\\Project With Spaces — テスト';
const winInvocation = buildSubprocessInvocation({
platform: 'win32',
executablePath: winCmdPath,
args: ['install', '--target', 'generic-project', '--project', winProjectPath, '--dry-run'],
comSpec: 'C:\\Windows\\System32\\cmd.exe',
});
assert.equal(winInvocation.file, 'C:\\Windows\\System32\\cmd.exe');
assert.deepEqual(winInvocation.args, [
'/d',
'/c',
'call',
winCmdPath,
'install',
'--target',
'generic-project',
'--project',
winProjectPath,
'--dry-run',
]);
assert.equal(winInvocation.windowsHide, true);
// Reject shell injection metacharacters fail-closed when building Windows .cmd invocations
for (const unsafeArg of [
'proj & calc.exe',
'proj | whoami',
'proj > out.txt',
'proj < in.txt',
'proj ^ escape',
'proj %PATH%',