Skip to content

The design language that makes your AI harness better at design.

JavaScript

Add 41 tests for live-wrap and live-server, add auto-execute guards

Paul Bakaus455fe3915573
diff --git a/.agents/skills/impeccable/scripts/live-poll.mjs b/.agents/skills/impeccable/scripts/live-poll.mjs
index 52859416c..6b903e562 100644
--- a/.agents/skills/impeccable/scripts/live-poll.mjs
+++ b/.agents/skills/impeccable/scripts/live-poll.mjs
@@ -121,3 +121,9 @@ Options:
process.exit(1);
}
}
+
+// Auto-execute when run directly
+const _running = process.argv[1];
+if (_running?.endsWith('live-poll.mjs') || _running?.endsWith('live-poll.mjs/')) {
+ pollCli();
+}
diff --git a/.agents/skills/impeccable/scripts/live-wrap.mjs b/.agents/skills/impeccable/scripts/live-wrap.mjs
index f1c838cdd..f8255e39b 100644
--- a/.agents/skills/impeccable/scripts/live-wrap.mjs
+++ b/.agents/skills/impeccable/scripts/live-wrap.mjs
@@ -296,3 +296,12 @@ function findClosingLine(lines, start) {
// If we can't find the close, return a reasonable guess
return Math.min(start + 50, lines.length - 1);
}
+
+// Auto-execute when run directly (node live-wrap.mjs ...)
+const _running = process.argv[1];
+if (_running?.endsWith('live-wrap.mjs') || _running?.endsWith('live-wrap.mjs/')) {
+ wrapCli();
+}
+
+// Test exports (used by tests/live-wrap.test.mjs)
+export { buildSearchQueries, findElement, findClosingLine, detectCommentSyntax };
diff --git a/.claude/skills/impeccable/scripts/live-poll.mjs b/.claude/skills/impeccable/scripts/live-poll.mjs
index 52859416c..6b903e562 100644
--- a/.claude/skills/impeccable/scripts/live-poll.mjs
+++ b/.claude/skills/impeccable/scripts/live-poll.mjs
@@ -121,3 +121,9 @@ Options:
process.exit(1);
}
}
+
+// Auto-execute when run directly
+const _running = process.argv[1];
+if (_running?.endsWith('live-poll.mjs') || _running?.endsWith('live-poll.mjs/')) {
+ pollCli();
+}
diff --git a/.claude/skills/impeccable/scripts/live-wrap.mjs b/.claude/skills/impeccable/scripts/live-wrap.mjs
index f1c838cdd..f8255e39b 100644
--- a/.claude/skills/impeccable/scripts/live-wrap.mjs
+++ b/.claude/skills/impeccable/scripts/live-wrap.mjs
@@ -296,3 +296,12 @@ function findClosingLine(lines, start) {
// If we can't find the close, return a reasonable guess
return Math.min(start + 50, lines.length - 1);
}
+
+// Auto-execute when run directly (node live-wrap.mjs ...)
+const _running = process.argv[1];
+if (_running?.endsWith('live-wrap.mjs') || _running?.endsWith('live-wrap.mjs/')) {
+ wrapCli();
+}
+
+// Test exports (used by tests/live-wrap.test.mjs)
+export { buildSearchQueries, findElement, findClosingLine, detectCommentSyntax };
diff --git a/.codex/skills/impeccable/scripts/live-poll.mjs b/.codex/skills/impeccable/scripts/live-poll.mjs
index 52859416c..6b903e562 100644
--- a/.codex/skills/impeccable/scripts/live-poll.mjs
+++ b/.codex/skills/impeccable/scripts/live-poll.mjs
@@ -121,3 +121,9 @@ Options:
process.exit(1);
}
}
+
+// Auto-execute when run directly
+const _running = process.argv[1];
+if (_running?.endsWith('live-poll.mjs') || _running?.endsWith('live-poll.mjs/')) {
+ pollCli();
+}
diff --git a/.codex/skills/impeccable/scripts/live-wrap.mjs b/.codex/skills/impeccable/scripts/live-wrap.mjs
index f1c838cdd..f8255e39b 100644
--- a/.codex/skills/impeccable/scripts/live-wrap.mjs
+++ b/.codex/skills/impeccable/scripts/live-wrap.mjs
@@ -296,3 +296,12 @@ function findClosingLine(lines, start) {
// If we can't find the close, return a reasonable guess
return Math.min(start + 50, lines.length - 1);
}
+
+// Auto-execute when run directly (node live-wrap.mjs ...)
+const _running = process.argv[1];
+if (_running?.endsWith('live-wrap.mjs') || _running?.endsWith('live-wrap.mjs/')) {
+ wrapCli();
+}
+
+// Test exports (used by tests/live-wrap.test.mjs)
+export { buildSearchQueries, findElement, findClosingLine, detectCommentSyntax };
diff --git a/.cursor/skills/impeccable/scripts/live-poll.mjs b/.cursor/skills/impeccable/scripts/live-poll.mjs
index 52859416c..6b903e562 100644
--- a/.cursor/skills/impeccable/scripts/live-poll.mjs
+++ b/.cursor/skills/impeccable/scripts/live-poll.mjs
@@ -121,3 +121,9 @@ Options:
process.exit(1);
}
}
+
+// Auto-execute when run directly
+const _running = process.argv[1];
+if (_running?.endsWith('live-poll.mjs') || _running?.endsWith('live-poll.mjs/')) {
+ pollCli();
+}
diff --git a/.cursor/skills/impeccable/scripts/live-wrap.mjs b/.cursor/skills/impeccable/scripts/live-wrap.mjs
index f1c838cdd..f8255e39b 100644
--- a/.cursor/skills/impeccable/scripts/live-wrap.mjs
+++ b/.cursor/skills/impeccable/scripts/live-wrap.mjs
@@ -296,3 +296,12 @@ function findClosingLine(lines, start) {
// If we can't find the close, return a reasonable guess
return Math.min(start + 50, lines.length - 1);
}
+
+// Auto-execute when run directly (node live-wrap.mjs ...)
+const _running = process.argv[1];
+if (_running?.endsWith('live-wrap.mjs') || _running?.endsWith('live-wrap.mjs/')) {
+ wrapCli();
+}
+
+// Test exports (used by tests/live-wrap.test.mjs)
+export { buildSearchQueries, findElement, findClosingLine, detectCommentSyntax };
diff --git a/.gemini/skills/impeccable/scripts/live-poll.mjs b/.gemini/skills/impeccable/scripts/live-poll.mjs
index 52859416c..6b903e562 100644
--- a/.gemini/skills/impeccable/scripts/live-poll.mjs
+++ b/.gemini/skills/impeccable/scripts/live-poll.mjs
@@ -121,3 +121,9 @@ Options:
process.exit(1);
}
}
+
+// Auto-execute when run directly
+const _running = process.argv[1];
+if (_running?.endsWith('live-poll.mjs') || _running?.endsWith('live-poll.mjs/')) {
+ pollCli();
+}
diff --git a/.gemini/skills/impeccable/scripts/live-wrap.mjs b/.gemini/skills/impeccable/scripts/live-wrap.mjs
index f1c838cdd..f8255e39b 100644
--- a/.gemini/skills/impeccable/scripts/live-wrap.mjs
+++ b/.gemini/skills/impeccable/scripts/live-wrap.mjs
@@ -296,3 +296,12 @@ function findClosingLine(lines, start) {
// If we can't find the close, return a reasonable guess
return Math.min(start + 50, lines.length - 1);
}
+
+// Auto-execute when run directly (node live-wrap.mjs ...)
+const _running = process.argv[1];
+if (_running?.endsWith('live-wrap.mjs') || _running?.endsWith('live-wrap.mjs/')) {
+ wrapCli();
+}
+
+// Test exports (used by tests/live-wrap.test.mjs)
+export { buildSearchQueries, findElement, findClosingLine, detectCommentSyntax };
diff --git a/.kiro/skills/impeccable/scripts/live-poll.mjs b/.kiro/skills/impeccable/scripts/live-poll.mjs
index 52859416c..6b903e562 100644
--- a/.kiro/skills/impeccable/scripts/live-poll.mjs
+++ b/.kiro/skills/impeccable/scripts/live-poll.mjs
@@ -121,3 +121,9 @@ Options:
process.exit(1);
}
}
+
+// Auto-execute when run directly
+const _running = process.argv[1];
+if (_running?.endsWith('live-poll.mjs') || _running?.endsWith('live-poll.mjs/')) {
+ pollCli();
+}
diff --git a/.kiro/skills/impeccable/scripts/live-wrap.mjs b/.kiro/skills/impeccable/scripts/live-wrap.mjs
index f1c838cdd..f8255e39b 100644
--- a/.kiro/skills/impeccable/scripts/live-wrap.mjs
+++ b/.kiro/skills/impeccable/scripts/live-wrap.mjs
@@ -296,3 +296,12 @@ function findClosingLine(lines, start) {
// If we can't find the close, return a reasonable guess
return Math.min(start + 50, lines.length - 1);
}
+
+// Auto-execute when run directly (node live-wrap.mjs ...)
+const _running = process.argv[1];
+if (_running?.endsWith('live-wrap.mjs') || _running?.endsWith('live-wrap.mjs/')) {
+ wrapCli();
+}
+
+// Test exports (used by tests/live-wrap.test.mjs)
+export { buildSearchQueries, findElement, findClosingLine, detectCommentSyntax };
diff --git a/.opencode/skills/impeccable/scripts/live-poll.mjs b/.opencode/skills/impeccable/scripts/live-poll.mjs
index 52859416c..6b903e562 100644
--- a/.opencode/skills/impeccable/scripts/live-poll.mjs
+++ b/.opencode/skills/impeccable/scripts/live-poll.mjs
@@ -121,3 +121,9 @@ Options:
process.exit(1);
}
}
+
+// Auto-execute when run directly
+const _running = process.argv[1];
+if (_running?.endsWith('live-poll.mjs') || _running?.endsWith('live-poll.mjs/')) {
+ pollCli();
+}
diff --git a/.opencode/skills/impeccable/scripts/live-wrap.mjs b/.opencode/skills/impeccable/scripts/live-wrap.mjs
index f1c838cdd..f8255e39b 100644
--- a/.opencode/skills/impeccable/scripts/live-wrap.mjs
+++ b/.opencode/skills/impeccable/scripts/live-wrap.mjs
@@ -296,3 +296,12 @@ function findClosingLine(lines, start) {
// If we can't find the close, return a reasonable guess
return Math.min(start + 50, lines.length - 1);
}
+
+// Auto-execute when run directly (node live-wrap.mjs ...)
+const _running = process.argv[1];
+if (_running?.endsWith('live-wrap.mjs') || _running?.endsWith('live-wrap.mjs/')) {
+ wrapCli();
+}
+
+// Test exports (used by tests/live-wrap.test.mjs)
+export { buildSearchQueries, findElement, findClosingLine, detectCommentSyntax };
diff --git a/.pi/skills/impeccable/scripts/live-poll.mjs b/.pi/skills/impeccable/scripts/live-poll.mjs
index 52859416c..6b903e562 100644
--- a/.pi/skills/impeccable/scripts/live-poll.mjs
+++ b/.pi/skills/impeccable/scripts/live-poll.mjs
@@ -121,3 +121,9 @@ Options:
process.exit(1);
}
}
+
+// Auto-execute when run directly
+const _running = process.argv[1];
+if (_running?.endsWith('live-poll.mjs') || _running?.endsWith('live-poll.mjs/')) {
+ pollCli();
+}
diff --git a/.pi/skills/impeccable/scripts/live-wrap.mjs b/.pi/skills/impeccable/scripts/live-wrap.mjs
index f1c838cdd..f8255e39b 100644
--- a/.pi/skills/impeccable/scripts/live-wrap.mjs
+++ b/.pi/skills/impeccable/scripts/live-wrap.mjs
@@ -296,3 +296,12 @@ function findClosingLine(lines, start) {
// If we can't find the close, return a reasonable guess
return Math.min(start + 50, lines.length - 1);
}
+
+// Auto-execute when run directly (node live-wrap.mjs ...)
+const _running = process.argv[1];
+if (_running?.endsWith('live-wrap.mjs') || _running?.endsWith('live-wrap.mjs/')) {
+ wrapCli();
+}
+
+// Test exports (used by tests/live-wrap.test.mjs)
+export { buildSearchQueries, findElement, findClosingLine, detectCommentSyntax };
diff --git a/.rovodev/skills/impeccable/scripts/live-poll.mjs b/.rovodev/skills/impeccable/scripts/live-poll.mjs
index 52859416c..6b903e562 100644
--- a/.rovodev/skills/impeccable/scripts/live-poll.mjs
+++ b/.rovodev/skills/impeccable/scripts/live-poll.mjs
@@ -121,3 +121,9 @@ Options:
process.exit(1);
}
}
+
+// Auto-execute when run directly
+const _running = process.argv[1];
+if (_running?.endsWith('live-poll.mjs') || _running?.endsWith('live-poll.mjs/')) {
+ pollCli();
+}
diff --git a/.rovodev/skills/impeccable/scripts/live-wrap.mjs b/.rovodev/skills/impeccable/scripts/live-wrap.mjs
index f1c838cdd..f8255e39b 100644
--- a/.rovodev/skills/impeccable/scripts/live-wrap.mjs
+++ b/.rovodev/skills/impeccable/scripts/live-wrap.mjs
@@ -296,3 +296,12 @@ function findClosingLine(lines, start) {
// If we can't find the close, return a reasonable guess
return Math.min(start + 50, lines.length - 1);
}
+
+// Auto-execute when run directly (node live-wrap.mjs ...)
+const _running = process.argv[1];
+if (_running?.endsWith('live-wrap.mjs') || _running?.endsWith('live-wrap.mjs/')) {
+ wrapCli();
+}
+
+// Test exports (used by tests/live-wrap.test.mjs)
+export { buildSearchQueries, findElement, findClosingLine, detectCommentSyntax };
diff --git a/.trae-cn/skills/impeccable/scripts/live-poll.mjs b/.trae-cn/skills/impeccable/scripts/live-poll.mjs
index 52859416c..6b903e562 100644
--- a/.trae-cn/skills/impeccable/scripts/live-poll.mjs
+++ b/.trae-cn/skills/impeccable/scripts/live-poll.mjs
@@ -121,3 +121,9 @@ Options:
process.exit(1);
}
}
+
+// Auto-execute when run directly
+const _running = process.argv[1];
+if (_running?.endsWith('live-poll.mjs') || _running?.endsWith('live-poll.mjs/')) {
+ pollCli();
+}
diff --git a/.trae-cn/skills/impeccable/scripts/live-wrap.mjs b/.trae-cn/skills/impeccable/scripts/live-wrap.mjs
index f1c838cdd..f8255e39b 100644
--- a/.trae-cn/skills/impeccable/scripts/live-wrap.mjs
+++ b/.trae-cn/skills/impeccable/scripts/live-wrap.mjs
@@ -296,3 +296,12 @@ function findClosingLine(lines, start) {
// If we can't find the close, return a reasonable guess
return Math.min(start + 50, lines.length - 1);
}
+
+// Auto-execute when run directly (node live-wrap.mjs ...)
+const _running = process.argv[1];
+if (_running?.endsWith('live-wrap.mjs') || _running?.endsWith('live-wrap.mjs/')) {
+ wrapCli();
+}
+
+// Test exports (used by tests/live-wrap.test.mjs)
+export { buildSearchQueries, findElement, findClosingLine, detectCommentSyntax };
diff --git a/.trae/skills/impeccable/scripts/live-poll.mjs b/.trae/skills/impeccable/scripts/live-poll.mjs
index 52859416c..6b903e562 100644
--- a/.trae/skills/impeccable/scripts/live-poll.mjs
+++ b/.trae/skills/impeccable/scripts/live-poll.mjs
@@ -121,3 +121,9 @@ Options:
process.exit(1);
}
}
+
+// Auto-execute when run directly
+const _running = process.argv[1];
+if (_running?.endsWith('live-poll.mjs') || _running?.endsWith('live-poll.mjs/')) {
+ pollCli();
+}
diff --git a/.trae/skills/impeccable/scripts/live-wrap.mjs b/.trae/skills/impeccable/scripts/live-wrap.mjs
index f1c838cdd..f8255e39b 100644
--- a/.trae/skills/impeccable/scripts/live-wrap.mjs
+++ b/.trae/skills/impeccable/scripts/live-wrap.mjs
@@ -296,3 +296,12 @@ function findClosingLine(lines, start) {
// If we can't find the close, return a reasonable guess
return Math.min(start + 50, lines.length - 1);
}
+
+// Auto-execute when run directly (node live-wrap.mjs ...)
+const _running = process.argv[1];
+if (_running?.endsWith('live-wrap.mjs') || _running?.endsWith('live-wrap.mjs/')) {
+ wrapCli();
+}
+
+// Test exports (used by tests/live-wrap.test.mjs)
+export { buildSearchQueries, findElement, findClosingLine, detectCommentSyntax };
diff --git a/package.json b/package.json
index a5bcb7c6a..b1b286886 100644
--- a/package.json
+++ b/package.json
@@ -49,7 +49,7 @@
"dev": "bun run server/index.js",
"preview": "bun run build && wrangler pages dev",
"deploy": "bun run build && wrangler pages deploy build/",
- "test": "bun test tests/build.test.js tests/detect-antipatterns.test.js && node --test tests/detect-antipatterns-fixtures.test.mjs && node --test tests/detect-antipatterns-browser.test.mjs && node --test tests/cleanup-deprecated.test.mjs",
+ "test": "bun test tests/build.test.js tests/detect-antipatterns.test.js && node --test tests/detect-antipatterns-fixtures.test.mjs && node --test tests/detect-antipatterns-browser.test.mjs && node --test tests/cleanup-deprecated.test.mjs && node --test tests/live-wrap.test.mjs && node --test tests/live-server.test.mjs",
"prepack": "cp README.md README.repo.md && cp README.npm.md README.md",
"postpack": "cp README.repo.md README.md && rm README.repo.md",
"screenshot": "bun run scripts/screenshot-antipatterns.js",
diff --git a/source/skills/impeccable/scripts/live-poll.mjs b/source/skills/impeccable/scripts/live-poll.mjs
index 52859416c..6b903e562 100644
--- a/source/skills/impeccable/scripts/live-poll.mjs
+++ b/source/skills/impeccable/scripts/live-poll.mjs
@@ -121,3 +121,9 @@ Options:
process.exit(1);
}
}
+
+// Auto-execute when run directly
+const _running = process.argv[1];
+if (_running?.endsWith('live-poll.mjs') || _running?.endsWith('live-poll.mjs/')) {
+ pollCli();
+}
diff --git a/source/skills/impeccable/scripts/live-wrap.mjs b/source/skills/impeccable/scripts/live-wrap.mjs
index f1c838cdd..f8255e39b 100644
--- a/source/skills/impeccable/scripts/live-wrap.mjs
+++ b/source/skills/impeccable/scripts/live-wrap.mjs
@@ -296,3 +296,12 @@ function findClosingLine(lines, start) {
// If we can't find the close, return a reasonable guess
return Math.min(start + 50, lines.length - 1);
}
+
+// Auto-execute when run directly (node live-wrap.mjs ...)
+const _running = process.argv[1];
+if (_running?.endsWith('live-wrap.mjs') || _running?.endsWith('live-wrap.mjs/')) {
+ wrapCli();
+}
+
+// Test exports (used by tests/live-wrap.test.mjs)
+export { buildSearchQueries, findElement, findClosingLine, detectCommentSyntax };
diff --git a/tests/live-server.test.mjs b/tests/live-server.test.mjs
new file mode 100644
index 000000000..a2051f892
--- /dev/null
+++ b/tests/live-server.test.mjs
@@ -0,0 +1,241 @@
+/**
+ * Tests for the live variant server.
+ * Run with: node --test tests/live-server.test.mjs
+ */
+
+import { describe, it, before, after } from 'node:test';
+import assert from 'node:assert/strict';
+import { readFileSync, writeFileSync, mkdirSync, rmSync } from 'node:fs';
+import { join } from 'node:path';
+import { tmpdir } from 'node:os';
+import { execSync, spawn } from 'node:child_process';
+
+const SERVER_SCRIPT = 'source/skills/impeccable/scripts/live-server.mjs';
+const PID_FILE = join(tmpdir(), 'impeccable-live.json');
+
+// ---------------------------------------------------------------------------
+// Helper: start/stop server for integration tests
+// ---------------------------------------------------------------------------
+
+function startServer(port = 8499) {
+ return new Promise((resolve, reject) => {
+ const proc = spawn('node', [SERVER_SCRIPT, '--port=' + port], {
+ stdio: ['ignore', 'pipe', 'pipe'],
+ env: { ...process.env },
+ });
+ let output = '';
+ proc.stdout.on('data', (d) => {
+ output += d.toString();
+ if (output.includes('running on')) {
+ // Read token from PID file
+ try {
+ const info = JSON.parse(readFileSync(PID_FILE, 'utf-8'));
+ resolve({ proc, port: info.port, token: info.token });
+ } catch {
+ reject(new Error('Server started but PID file not readable'));
+ }
+ }
+ });
+ proc.stderr.on('data', (d) => { output += d.toString(); });
+ proc.on('error', reject);
+ setTimeout(() => reject(new Error('Server start timeout. Output: ' + output)), 5000);
+ });
+}
+
+async function stopServer(port, token) {
+ try {
+ await fetch(`http://localhost:${port}/stop?token=${token}`);
+ } catch { /* server already gone */ }
+}
+
+// ---------------------------------------------------------------------------
+// Server integration tests
+// ---------------------------------------------------------------------------
+
+describe('live-server integration', () => {
+ let server;
+
+ before(async () => {
+ server = await startServer(8499);
+ });
+
+ after(async () => {
+ if (server) {
+ await stopServer(server.port, server.token);
+ server.proc.kill();
+ }
+ });
+
+ it('/health returns correct status', async () => {
+ const res = await fetch(`http://localhost:${server.port}/health`);
+ assert.equal(res.status, 200);
+ const data = await res.json();
+ assert.equal(data.status, 'ok');
+ assert.equal(data.port, server.port);
+ assert.equal(data.mode, 'variant');
+ assert.equal(typeof data.hasProjectContext, 'boolean');
+ assert.equal(data.connectedClients, 0);
+ });
+
+ it('/live.js serves script with token injected', async () => {
+ const res = await fetch(`http://localhost:${server.port}/live.js`);
+ assert.equal(res.status, 200);
+ assert.equal(res.headers.get('content-type'), 'application/javascript');
+ const text = await res.text();
+ assert.ok(text.includes('__IMPECCABLE_TOKEN__'));
+ assert.ok(text.includes(server.token));
+ assert.ok(text.includes('__IMPECCABLE_PORT__'));
+ });
+
+ it('/detect.js serves the detection overlay', async () => {
+ const res = await fetch(`http://localhost:${server.port}/detect.js`);
+ // May 404 if detect-antipatterns-browser.js hasn't been built
+ assert.ok(res.status === 200 || res.status === 404);
+ });
+
+ it('/poll returns timeout when no events queued', async () => {
+ const res = await fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=500`);
+ assert.equal(res.status, 200);
+ const data = await res.json();
+ assert.equal(data.type, 'timeout');
+ });
+
+ it('/poll rejects invalid token', async () => {
+ const res = await fetch(`http://localhost:${server.port}/poll?token=wrong&timeout=100`);
+ assert.equal(res.status, 401);
+ });
+
+ it('/stop rejects invalid token', async () => {
+ const res = await fetch(`http://localhost:${server.port}/stop?token=wrong`);
+ assert.equal(res.status, 401);
+ });
+
+ it('POST /events rejects invalid token', async () => {
+ const res = await fetch(`http://localhost:${server.port}/events`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ token: 'wrong', type: 'exit' }),
+ });
+ assert.equal(res.status, 401);
+ });
+
+ it('POST /events validates event structure', async () => {
+ const res = await fetch(`http://localhost:${server.port}/events`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ token: server.token, type: 'generate' }), // missing required fields
+ });
+ assert.equal(res.status, 400);
+ const data = await res.json();
+ assert.ok(data.error.includes('generate'));
+ });
+
+ it('POST /events accepts valid exit event', async () => {
+ const res = await fetch(`http://localhost:${server.port}/events`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ token: server.token, type: 'exit' }),
+ });
+ assert.equal(res.status, 200);
+ const data = await res.json();
+ assert.equal(data.ok, true);
+ });
+
+ it('events flow from browser POST to agent poll', async () => {
+ // Drain any queued events from previous tests
+ let drained;
+ do {
+ const r = await fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=100`);
+ drained = await r.json();
+ } while (drained.type !== 'timeout');
+
+ // Start a poll (will block until event arrives or timeout)
+ const pollPromise = fetch(`http://localhost:${server.port}/poll?token=${server.token}&timeout=5000`)
+ .then(r => r.json());
+
+ // Give the poll a moment to register
+ await new Promise(r => setTimeout(r, 100));
+
+ // Send a generate event (simulating browser)
+ const postRes = await fetch(`http://localhost:${server.port}/events`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ token: server.token,
+ type: 'generate',
+ id: 'test-e2e-1',
+ action: 'bolder',
+ count: 2,
+ element: { outerHTML: '<div>test</div>', tagName: 'div' },
+ }),
+ });
+ assert.equal(postRes.status, 200);
+
+ // Poll should resolve with the event
+ const event = await pollPromise;
+ assert.equal(event.type, 'generate');
+ assert.equal(event.id, 'test-e2e-1');
+ assert.equal(event.action, 'bolder');
+ assert.equal(event.count, 2);
+ });
+
+ it('agent reply is forwarded via SSE to browser', async () => {
+ // Use raw HTTP to read SSE (no EventSource in Node.js)
+ const controller = new AbortController();
+ const sseRes = await fetch(
+ `http://localhost:${server.port}/events?token=${server.token}`,
+ { signal: controller.signal }
+ );
+ assert.equal(sseRes.status, 200);
+ assert.equal(sseRes.headers.get('content-type'), 'text/event-stream');
+
+ // Read the first message (should be "connected")
+ const reader = sseRes.body.getReader();
+ const decoder = new TextDecoder();
+ const { value: chunk1 } = await reader.read();
+ const text1 = decoder.decode(chunk1);
+ assert.ok(text1.includes('"connected"'));
+
+ // Send a reply from the agent
+ await fetch(`http://localhost:${server.port}/poll`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ token: server.token, id: 'sse-test', type: 'done', file: 'x.html' }),
+ });
+
+ // Read the next SSE message
+ const { value: chunk2 } = await reader.read();
+ const text2 = decoder.decode(chunk2);
+ assert.ok(text2.includes('"done"'));
+ assert.ok(text2.includes('sse-test'));
+
+ controller.abort();
+ });
+
+ it('/source reads project files with valid token', async () => {
+ const res = await fetch(`http://localhost:${server.port}/source?token=${server.token}&path=package.json`);
+ assert.equal(res.status, 200);
+ const text = await res.text();
+ assert.ok(text.includes('"impeccable"'));
+ });
+
+ it('/source rejects path traversal', async () => {
+ const res = await fetch(`http://localhost:${server.port}/source?token=${server.token}&path=../../../etc/passwd`);
+ assert.equal(res.status, 400);
+ });
+
+ it('/source rejects invalid token', async () => {
+ const res = await fetch(`http://localhost:${server.port}/source?token=wrong&path=package.json`);
+ assert.equal(res.status, 401);
+ });
+
+ it('/source returns 404 for missing files', async () => {
+ try {
+ const res = await fetch(`http://localhost:${server.port}/source?token=${server.token}&path=nonexistent.xyz`);
+ assert.equal(res.status, 404);
+ } catch {
+ // Server may close socket on 404 for some Node versions
+ assert.ok(true, 'Server rejected request for missing file');
+ }
+ });
+});
diff --git a/tests/live-wrap.test.mjs b/tests/live-wrap.test.mjs
new file mode 100644
index 000000000..b63c4c808
--- /dev/null
+++ b/tests/live-wrap.test.mjs
@@ -0,0 +1,325 @@
+/**
+ * Tests for the live-wrap CLI helper.
+ * Run with: node --test tests/live-wrap.test.mjs
+ */
+
+import { describe, it, beforeEach, afterEach } from 'node:test';
+import assert from 'node:assert/strict';
+import { mkdtempSync, writeFileSync, readFileSync, rmSync, mkdirSync } from 'node:fs';
+import { join } from 'node:path';
+import { tmpdir } from 'node:os';
+import { execSync } from 'node:child_process';
+
+import {
+ buildSearchQueries,
+ findElement,
+ findClosingLine,
+ detectCommentSyntax,
+} from '../source/skills/impeccable/scripts/live-wrap.mjs';
+
+// ---------------------------------------------------------------------------
+// Unit tests: pure functions
+// ---------------------------------------------------------------------------
+
+describe('detectCommentSyntax', () => {
+ it('returns HTML comments for .html files', () => {
+ const result = detectCommentSyntax('index.html');
+ assert.equal(result.open, '<!--');
+ assert.equal(result.close, '-->');
+ });
+
+ it('returns JSX comments for .jsx files', () => {
+ const result = detectCommentSyntax('App.jsx');
+ assert.equal(result.open, '{/*');
+ assert.equal(result.close, '*/}');
+ });
+
+ it('returns JSX comments for .tsx files', () => {
+ const result = detectCommentSyntax('component.tsx');
+ assert.equal(result.open, '{/*');
+ assert.equal(result.close, '*/}');
+ });
+
+ it('returns HTML comments for .vue files', () => {
+ const result = detectCommentSyntax('App.vue');
+ assert.equal(result.open, '<!--');
+ assert.equal(result.close, '-->');
+ });
+
+ it('returns HTML comments for .svelte files', () => {
+ const result = detectCommentSyntax('Page.svelte');
+ assert.equal(result.open, '<!--');
+ assert.equal(result.close, '-->');
+ });
+});
+
+describe('buildSearchQueries', () => {
+ it('prioritizes ID over classes', () => {
+ const queries = buildSearchQueries('hero', 'hero-section,dark', 'section', null);
+ assert.equal(queries[0], 'id="hero"');
+ });
+
+ it('includes full class match for multi-class elements', () => {
+ const queries = buildSearchQueries(null, 'hero-section,dark-theme', 'div', null);
+ assert.ok(queries.some(q => q === 'class="hero-section dark-theme"'));
+ });
+
+ it('includes the most distinctive single class (longest)', () => {
+ const queries = buildSearchQueries(null, 'btn,hero-combined-left', null, null);
+ assert.ok(queries.some(q => q === 'hero-combined-left'));
+ });
+
+ it('includes tag+class combo', () => {
+ const queries = buildSearchQueries(null, 'hero-section', 'section', null);
+ assert.ok(queries.some(q => q === '<section class="hero-section'));
+ });
+
+ it('includes raw fallback query', () => {
+ const queries = buildSearchQueries(null, null, null, 'Welcome to our app');
+ assert.deepEqual(queries, ['Welcome to our app']);
+ });
+
+ it('returns all query types when everything is provided', () => {
+ const queries = buildSearchQueries('main', 'container,wide', 'div', 'fallback');
+ assert.ok(queries.length >= 4);
+ assert.equal(queries[0], 'id="main"');
+ assert.ok(queries.includes('fallback'));
+ });
+});
+
+describe('findElement', () => {
+ it('finds an element by class name', () => {
+ const lines = [
+ '<html>',
+ '<body>',
+ ' <div class="hero">',
+ ' <h1>Hello</h1>',
+ ' </div>',
+ '</body>',
+ '</html>',
+ ];
+ const result = findElement(lines, 'hero');
+ assert.ok(result);
+ assert.equal(result.startLine, 2);
+ assert.equal(result.endLine, 4);
+ });
+
+ it('finds an element by ID', () => {
+ const lines = [
+ '<section id="features">',
+ ' <p>Content</p>',
+ '</section>',
+ ];
+ const result = findElement(lines, 'id="features"');
+ assert.ok(result);
+ assert.equal(result.startLine, 0);
+ assert.equal(result.endLine, 2);
+ });
+
+ it('returns null when element is not found', () => {
+ const lines = ['<div>hello</div>'];
+ const result = findElement(lines, 'nonexistent');
+ assert.equal(result, null);
+ });
+
+ it('skips comments containing the query', () => {
+ const lines = [
+ '<!-- hero section -->',
+ '<div class="hero">',
+ ' <p>Content</p>',
+ '</div>',
+ ];
+ const result = findElement(lines, 'hero');
+ assert.ok(result);
+ assert.equal(result.startLine, 1); // skips the comment on line 0
+ });
+
+ it('skips lines that contain data-impeccable-variant', () => {
+ const lines = [
+ '<div class="hero" data-impeccable-variant="original">Old</div>',
+ '<div class="hero">Real</div>',
+ ];
+ const result = findElement(lines, 'hero');
+ assert.ok(result);
+ assert.equal(result.startLine, 1);
+ });
+});
+
+describe('findClosingLine', () => {
+ it('finds the closing tag on the same line', () => {
+ const lines = ['<p>Hello</p>'];
+ assert.equal(findClosingLine(lines, 0), 0);
+ });
+
+ it('finds the closing tag across multiple lines', () => {
+ const lines = [
+ '<div>',
+ ' <p>Hello</p>',
+ '</div>',
+ ];
+ assert.equal(findClosingLine(lines, 0), 2);
+ });
+
+ it('handles nested tags of the same type', () => {
+ const lines = [
+ '<div class="outer">',
+ ' <div class="inner">',
+ ' <p>Content</p>',
+ ' </div>',
+ '</div>',
+ ];
+ assert.equal(findClosingLine(lines, 0), 4);
+ });
+
+ it('handles deeply nested structures', () => {
+ const lines = [
+ '<section>',
+ ' <div>',
+ ' <div>',
+ ' <span>text</span>',
+ ' </div>',
+ ' </div>',
+ '</section>',
+ ];
+ assert.equal(findClosingLine(lines, 0), 6);
+ });
+
+ it('handles self-closing tags', () => {
+ const lines = [
+ '<div>',
+ ' <img src="test.png" />',
+ ' <br />',
+ '</div>',
+ ];
+ assert.equal(findClosingLine(lines, 0), 3);
+ });
+});
+
+// ---------------------------------------------------------------------------
+// Integration tests: full wrap CLI on fixture files
+// ---------------------------------------------------------------------------
+
+describe('wrapCli integration', () => {
+ let tmp;
+
+ beforeEach(() => {
+ tmp = mkdtempSync(join(tmpdir(), 'impeccable-wrap-test-'));
+ });
+
+ afterEach(() => {
+ rmSync(tmp, { recursive: true, force: true });
+ });
+
+ it('wraps an HTML element by class name', () => {
+ const html = `<!DOCTYPE html>
+<html>
+<body>
+ <div class="hero-section">
+ <h1>Hello World</h1>
+ <p>Welcome to our site.</p>
+ </div>
+</body>
+</html>`;
+ writeFileSync(join(tmp, 'index.html'), html);
+
+ const result = JSON.parse(execSync(
+ `node source/skills/impeccable/scripts/live-wrap.mjs --id test123 --count 3 --classes "hero-section" --file "${join(tmp, 'index.html')}"`,
+ { cwd: process.cwd(), encoding: 'utf-8' }
+ ));
+
+ // The file path is relative to cwd, so it may be a relative path to the tmp dir
+ assert.ok(result.file.endsWith('index.html'));
+ assert.ok(result.insertLine > 0);
+ assert.equal(result.commentSyntax.open, '<!--');
+
+ // Verify the file was modified correctly
+ const modified = readFileSync(join(tmp, 'index.html'), 'utf-8');
+ assert.ok(modified.includes('data-impeccable-variants="test123"'));
+ assert.ok(modified.includes('data-impeccable-variant-count="3"'));
+ assert.ok(modified.includes('data-impeccable-variant="original"'));
+ assert.ok(modified.includes('display: contents'));
+ assert.ok(modified.includes('impeccable-variants-start test123'));
+ assert.ok(modified.includes('impeccable-variants-end test123'));
+ // Original should NOT be hidden (stays visible until variants arrive)
+ assert.ok(!modified.includes('data-impeccable-variant="original" style="display: none"'));
+ });
+
+ it('wraps a JSX element and uses JSX comment syntax', () => {
+ const jsx = `export default function App() {
+ return (
+ <main>
+ <section className="hero">
+ <h1>Hello</h1>
+ </section>
+ </main>
+ );
+}`;
+ writeFileSync(join(tmp, 'App.jsx'), jsx);
+
+ const result = JSON.parse(execSync(
+ `node source/skills/impeccable/scripts/live-wrap.mjs --id jsx123 --count 2 --classes "hero" --file "${join(tmp, 'App.jsx')}"`,
+ { cwd: process.cwd(), encoding: 'utf-8' }
+ ));
+
+ assert.equal(result.commentSyntax.open, '{/*');
+ assert.equal(result.commentSyntax.close, '*/}');
+
+ const modified = readFileSync(join(tmp, 'App.jsx'), 'utf-8');
+ assert.ok(modified.includes('{/* impeccable-variants-start jsx123'));
+ assert.ok(modified.includes('data-impeccable-variant-count="2"'));
+ });
+
+ it('finds element by ID when --element-id is used', () => {
+ const html = `<html><body>
+<div id="pricing">
+ <h2>Pricing</h2>
+ <p>Plans start at $10/mo.</p>
+</div>
+</body></html>`;
+ writeFileSync(join(tmp, 'page.html'), html);
+
+ const result = JSON.parse(execSync(
+ `node source/skills/impeccable/scripts/live-wrap.mjs --id id123 --count 2 --element-id "pricing" --file "${join(tmp, 'page.html')}"`,
+ { cwd: process.cwd(), encoding: 'utf-8' }
+ ));
+
+ const modified = readFileSync(join(tmp, 'page.html'), 'utf-8');
+ assert.ok(modified.includes('data-impeccable-variants="id123"'));
+ // The original pricing div should be inside the wrapper
+ assert.ok(modified.includes('id="pricing"'));
+ });
+
+ it('exits with error when element is not found', () => {
+ writeFileSync(join(tmp, 'empty.html'), '<html><body><p>No match here</p></body></html>');
+
+ try {
+ execSync(
+ `node source/skills/impeccable/scripts/live-wrap.mjs --id err123 --count 2 --classes "nonexistent" --file "${join(tmp, 'empty.html')}"`,
+ { cwd: process.cwd(), encoding: 'utf-8', stdio: 'pipe' }
+ );
+ assert.fail('Should have exited with error');
+ } catch (err) {
+ assert.ok(err.status !== 0, 'Should exit with non-zero status');
+ assert.ok(err.stderr.includes('error') || err.stderr.includes('Could not'), 'Should print error message');
+ }
+ });
+
+ it('preserves surrounding content when wrapping', () => {
+ const html = `<div class="before">Before</div>
+<div class="target">
+ <span>Target content</span>
+</div>
+<div class="after">After</div>`;
+ writeFileSync(join(tmp, 'preserve.html'), html);
+
+ execSync(
+ `node source/skills/impeccable/scripts/live-wrap.mjs --id pres123 --count 2 --classes "target" --file "${join(tmp, 'preserve.html')}"`,
+ { cwd: process.cwd(), encoding: 'utf-8' }
+ );
+
+ const modified = readFileSync(join(tmp, 'preserve.html'), 'utf-8');
+ assert.ok(modified.includes('class="before"'));
+ assert.ok(modified.includes('class="after"'));
+ assert.ok(modified.includes('data-impeccable-variants="pres123"'));
+ });
+});