Repository navigation
Expand file tree
/
Copy pathWebCryptCLIFullCoverage.test.js
More file actions
158 lines (132 loc) · 5.08 KB
/
Copy pathWebCryptCLIFullCoverage.test.js
File metadata and controls
158 lines (132 loc) · 5.08 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
import fs from "fs";
import path from "path";
import os from "os";
import { getCrypto } from "../src/_crypto.js";
import {
isValidBase64,
arrayBufferToBase64,
base64ToArrayBuffer,
base64ToUint8Array,
} from "../src/_base64.js";
import { runInit, runInitGlobal } from "../src/cli/init.js";
import { runDoctor, runDoctorGlobal } from "../src/cli/doctor.js";
import {
getRegistry,
registerProject,
unregisterProject,
pruneStaleProjects,
} from "../src/cli/registry.js";
describe("WebCrypt CLI, Registry & Low-Level Helpers Full Coverage", () => {
let tmpDir;
let origEnv;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "webcrypt-cov-"));
origEnv = process.env.WEBCRYPT_REGISTRY_PATH;
process.env.WEBCRYPT_REGISTRY_PATH = path.join(tmpDir, "projects.json");
});
afterEach(() => {
process.env.WEBCRYPT_REGISTRY_PATH = origEnv;
fs.rmSync(tmpDir, { recursive: true, force: true });
});
test("getCrypto returns subtle crypto instance and handles error when unavailable", () => {
const crypto = getCrypto();
expect(crypto).toBeDefined();
expect(crypto.subtle).toBeDefined();
// Test error when globalThis.crypto is unavailable
const origCrypto = globalThis.crypto;
try {
globalThis.crypto = undefined;
expect(() => getCrypto()).toThrow(
"Web Crypto API (crypto.subtle) is not available in this environment"
);
} finally {
globalThis.crypto = origCrypto;
}
});
test("isValidBase64 validates varied string shapes", () => {
expect(isValidBase64("")).toBe(false);
expect(isValidBase64(null)).toBe(false);
expect(isValidBase64(123)).toBe(false);
expect(isValidBase64("A")).toBe(false); // Mod 4 == 1
expect(isValidBase64("Invalid $$$ Character")).toBe(false);
expect(isValidBase64("SGVsbG8gV29ybGQ=")).toBe(true);
expect(isValidBase64("YWJjZA==")).toBe(true);
});
test("base64 type error assertions", () => {
expect(() => base64ToArrayBuffer(123)).toThrow(TypeError);
expect(() => base64ToUint8Array(null)).toThrow(TypeError);
});
test("base64 unpadded string decoding", () => {
// "hello" -> "aGVsbG8=" (unpadded "aGVsbG8")
const buf = base64ToArrayBuffer("aGVsbG8");
const u8 = base64ToUint8Array("aGVsbG8");
expect(buf.byteLength).toBe(5);
expect(u8.length).toBe(5);
});
test("registry corrupted JSON and temp file handling", () => {
const regPath = path.join(tmpDir, "projects.json");
fs.writeFileSync(regPath, "{ malformed json");
// Creates temp file to test cleanup
const tempFile = path.join(tmpDir, "projects.json.tmp.12345");
fs.writeFileSync(tempFile, "temp");
const reg = getRegistry();
expect(reg).toEqual({});
expect(fs.existsSync(tempFile)).toBe(false); // Cleaned up
});
test("unregistering nonexistent project is safe no-op", () => {
unregisterProject("nonexistent-slug");
expect(getRegistry()).toEqual({});
});
test("runInit replaces existing marker in file rather than appending", async () => {
const projDir = path.join(tmpDir, "marker-app");
fs.mkdirSync(projDir, { recursive: true });
const agentsMd = path.join(projDir, ".agents", "AGENTS.md");
fs.mkdirSync(path.dirname(agentsMd), { recursive: true });
fs.writeFileSync(
agentsMd,
"# Title\n\n<!-- webcrypt-mcp:start -->\nOLD CONTENT\n<!-- webcrypt-mcp:end -->\n\nFooter"
);
await runInit(projDir, { silent: true });
const updated = fs.readFileSync(agentsMd, "utf-8");
expect(updated).toContain("# Title");
expect(updated).toContain("Cryptographic Vault & Security");
expect(updated).not.toContain("OLD CONTENT");
expect(updated).toContain("Footer");
});
test("runInit creates vscode config when .vscode directory exists", async () => {
const projDir = path.join(tmpDir, "vscode-app");
fs.mkdirSync(path.join(projDir, ".vscode"), { recursive: true });
await runInit(projDir, { silent: true });
const vscodeMcp = path.join(projDir, ".vscode", "mcp.json");
expect(fs.existsSync(vscodeMcp)).toBe(true);
});
test("runDoctorGlobal with empty registry and JSON mode", async () => {
const logs = [];
const origLog = console.log;
console.log = (...args) => logs.push(args.join(" "));
try {
await runDoctorGlobal({ json: true, cleanStale: true });
} finally {
console.log = origLog;
}
const output = JSON.parse(logs.join(""));
expect(output.total).toBe(0);
expect(output.results).toEqual([]);
});
test("runDoctor handles missing project directory gracefully", async () => {
const missingDir = path.join(tmpDir, "does-not-exist");
const docRes = await runDoctor(missingDir, { json: true });
expect(docRes.allPassed).toBe(false);
});
test("runInitGlobal handles empty registry and scan option", async () => {
const logs = [];
const origLog = console.log;
console.log = (...args) => logs.push(args.join(" "));
try {
await runInitGlobal({ scan: null, cleanStale: true });
} finally {
console.log = origLog;
}
expect(logs.join(" ")).toContain("No registered projects found");
});
});