Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';
import { detectHypervisor, stripJsoncComments, validateName, vmConfigPath, vmDir,} from './config.ts';
//region validateName: rejects unsafe VM names, accepts safe ones
await describe({ name: '', children: [ describe({ name: validateName.name, children: [ it({ name: 'accepts simple alphanumeric name', fn: async () => { expect(() => { validateName('alpine',); },) .not .toThrow(); }, },),
it({ name: 'accepts name with hyphens and underscores', fn: async () => { expect(() => { validateName('my-vm_01',); },) .not .toThrow(); }, },),
it({ name: 'accepts single character name', fn: async () => { expect(() => { validateName('a',); },) .not .toThrow(); }, },),
it({ name: 'accepts name starting with digit', fn: async () => { expect(() => { validateName('9test',); },) .not .toThrow(); }, },),
it({ name: 'rejects name starting with hyphen', fn: async () => { expect(() => { validateName('-bad',); },) .toThrow('invalid VM name',); }, },),
it({ name: 'rejects name starting with underscore', fn: async () => { expect(() => { validateName('_bad',); },) .toThrow('invalid VM name',); }, },),
it({ name: 'rejects path traversal', fn: async () => { expect(() => { validateName('../evil',); },) .toThrow('invalid VM name',); }, },),
it({ name: 'rejects name with spaces', fn: async () => { expect(() => { validateName('my vm',); },) .toThrow('invalid VM name',); }, },),
it({ name: 'rejects name with dots', fn: async () => { expect(() => { validateName('my.vm',); },) .toThrow('invalid VM name',); }, },),
it({ name: 'rejects empty string', fn: async () => { expect(() => { validateName('',); },) .toThrow('invalid VM name',); }, },),
it({ name: 'rejects name with slashes', fn: async () => { expect(() => { validateName('a/b',); },) .toThrow('invalid VM name',); }, },), ], },),
//endregion validateName
//region stripJsoncComments: removes comments while preserving string content
describe({ name: stripJsoncComments.name, children: [ it({ name: 'returns plain JSON unchanged', fn: async () => { const input = '{"a": 1, "b": "hello"}'; expect(stripJsoncComments(input,),).toBe(input,); }, },),
it({ name: 'strips single-line comment', fn: async () => { const input = '{"a": 1 // comment\n}'; expect(stripJsoncComments(input,),).toBe('{"a": 1 \n}',); }, },),
it({ name: 'strips multi-line comment', fn: async () => { const input = '{"a": /* block */ 1}'; expect(stripJsoncComments(input,),).toBe('{"a": 1}',); }, },),
it({ name: 'preserves comment-like content inside strings', fn: async () => { const input = '{"url": "http://example.com"}'; expect(stripJsoncComments(input,),).toBe(input,); }, },),
it({ name: 'preserves double-slash inside strings', fn: async () => { const input = '{"msg": "see // this"}'; expect(stripJsoncComments(input,),).toBe(input,); }, },),
it({ name: 'preserves block comment syntax inside strings', fn: async () => { const input = '{"msg": "a /* b */ c"}'; expect(stripJsoncComments(input,),).toBe(input,); }, },),
it({ name: 'handles escaped quotes inside strings', fn: async () => { const input = String.raw`{"msg": "say \"hello\""}`; expect(stripJsoncComments(input,),).toBe(input,); }, },),
it({ name: 'strips multiple single-line comments', fn: async () => { const input = '{\n// first\n"a": 1,\n// second\n"b": 2\n}'; const expected = '{\n\n"a": 1,\n\n"b": 2\n}'; expect(stripJsoncComments(input,),).toBe(expected,); }, },),
it({ name: 'strips trailing comment after value', fn: async () => { const input = '{"a": 1} // trailing'; expect(stripJsoncComments(input,),).toBe('{"a": 1} ',); }, },),
it({ name: 'handles empty input', fn: async () => { expect(stripJsoncComments('',),).toBe('',); }, },),
it({ name: 'result is valid JSON after stripping JSONC comments', fn: async () => { const input = `{ // VM name "name": "alpine", /* disk size in bytes */ "size": 1024}`; const stripped = stripJsoncComments(input,); expect(() => { JSON.parse(stripped,); },) .not .toThrow(); const parsed = JSON.parse(stripped,) as { name: string; size: number; }; expect(parsed.name,).toBe('alpine',); expect(parsed.size,).toBe(1_024,); }, },), ], },),
//endregion stripJsoncComments
//region vmDir / vmConfigPath: path construction from VM name
describe({ name: vmDir.name, children: [ it({ name: 'appends name to data directory', fn: async () => { const result = vmDir('alpine',); expect(result.endsWith('vmsync/alpine',),).toBe(true,); }, },),
it({ name: 'handles name with hyphens', fn: async () => { const result = vmDir('fedora-dev',); expect(result.endsWith('vmsync/fedora-dev',),).toBe(true,); }, },), ], },), describe({ name: vmConfigPath.name, children: [ it({ name: 'appends config filename to VM directory', fn: async () => { const result = vmConfigPath('alpine',); expect(result.endsWith('vmsync/alpine/vmsync.jsonc',),).toBe(true,); }, },), ], },),
//endregion vmDir / vmConfigPath
//region detectHypervisor: platform-based hypervisor detection
describe({ name: detectHypervisor.name, children: [ it({ name: 'returns kvm on linux', fn: async () => { // The test environment is Linux, so this should return 'kvm' expect(detectHypervisor(),).toBe('kvm',); }, },), ], },), //endregion detectHypervisor ],},);