initial commit
This commit is contained in:
293
test/smoke.js
Executable file
293
test/smoke.js
Executable file
@@ -0,0 +1,293 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { mkdtempSync, rmSync, existsSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
const cliPath = new URL("../index.js", import.meta.url).pathname;
|
||||
const tempDir = mkdtempSync(join(tmpdir(), "glimpse-smoke-"));
|
||||
const filters = process.argv.slice(2).filter((arg) => arg !== "--list");
|
||||
const shouldList = process.argv.includes("--list");
|
||||
const tests = [];
|
||||
|
||||
function test(name, tags, fn) {
|
||||
tests.push({ name, tags, fn });
|
||||
}
|
||||
|
||||
function dataHtml(html) {
|
||||
return `data:text/html,${html}`;
|
||||
}
|
||||
|
||||
function runCli(args, options = {}) {
|
||||
return spawnSync(process.execPath, [cliPath, ...args], {
|
||||
encoding: "utf-8",
|
||||
env: options.env ?? process.env,
|
||||
timeout: 30000,
|
||||
});
|
||||
}
|
||||
|
||||
function parseJson(text) {
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch (err) {
|
||||
throw new Error(`Failed to parse JSON: ${err.message}\n${text}`);
|
||||
}
|
||||
}
|
||||
|
||||
function expectSuccess(args) {
|
||||
const result = runCli(args);
|
||||
assert.equal(result.status, 0, result.stderr || result.stdout);
|
||||
return parseJson(result.stdout);
|
||||
}
|
||||
|
||||
function expectFailure(args) {
|
||||
const result = runCli(args);
|
||||
assert.notEqual(result.status, 0, result.stdout || result.stderr);
|
||||
return parseJson(result.stderr);
|
||||
}
|
||||
|
||||
function matchesFilters(entry) {
|
||||
if (filters.length === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return filters.some((filter) => {
|
||||
const normalized = filter.toLowerCase();
|
||||
return (
|
||||
entry.name.toLowerCase().includes(normalized) ||
|
||||
entry.tags.includes(normalized)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
test("no args prints help", ["help", "cli"], () => {
|
||||
const result = runCli([]);
|
||||
|
||||
assert.equal(result.status, 0, result.stderr || result.stdout);
|
||||
assert.match(result.stdout, /Usage: glimpse <command> <url> \[options\]/);
|
||||
assert.match(result.stdout, /snapshot <url>/);
|
||||
assert.equal(result.stderr, "");
|
||||
});
|
||||
|
||||
test("help flag prints help", ["help", "cli"], () => {
|
||||
const result = runCli(["--help"]);
|
||||
|
||||
assert.equal(result.status, 0, result.stderr || result.stdout);
|
||||
assert.match(result.stdout, /Usage: glimpse <command> <url> \[options\]/);
|
||||
assert.match(result.stdout, /--wait-js=<code>/);
|
||||
assert.equal(result.stderr, "");
|
||||
});
|
||||
|
||||
test("snapshot returns page metadata and content", ["snapshot"], () => {
|
||||
const output = expectSuccess([
|
||||
"snapshot",
|
||||
dataHtml('<title>Hello</title><h1>Main</h1><a href="/x">X</a><button>Go</button>'),
|
||||
]);
|
||||
|
||||
assert.equal(output.ok, true);
|
||||
assert.equal(output.title, "Hello");
|
||||
assert.equal(typeof output.elapsedMs, "number");
|
||||
assert.deepEqual(output.result.headings, [{ level: 1, text: "Main" }]);
|
||||
assert.deepEqual(output.result.links, [{ href: "/x", text: "X" }]);
|
||||
assert.equal(output.result.buttons[0].text, "Go");
|
||||
assert.match(output.result.text, /Main/);
|
||||
});
|
||||
|
||||
test("snapshot extracts aria headings", ["snapshot"], () => {
|
||||
const output = expectSuccess([
|
||||
"snapshot",
|
||||
dataHtml('<title>Hello</title><div role="heading" aria-level="2">ARIA</div>'),
|
||||
]);
|
||||
|
||||
assert.equal(output.ok, true);
|
||||
assert.deepEqual(output.result.headings, [{ level: 2, text: "ARIA" }]);
|
||||
});
|
||||
|
||||
test("snapshot runs top-level javascript before extraction", ["snapshot", "js"], () => {
|
||||
const output = expectSuccess([
|
||||
"snapshot",
|
||||
dataHtml("<title>Hello</title><h1>Old</h1>"),
|
||||
"--js=document.querySelector('h1').textContent = 'New'",
|
||||
]);
|
||||
|
||||
assert.equal(output.ok, true);
|
||||
assert.deepEqual(output.result.headings, [{ level: 1, text: "New" }]);
|
||||
assert.equal(output.result.text, "New");
|
||||
});
|
||||
|
||||
test("exec returns javascript result", ["exec", "js"], () => {
|
||||
const result = runCli([
|
||||
"exec",
|
||||
dataHtml("<title>Hello</title>"),
|
||||
"--js=return document.title",
|
||||
]);
|
||||
|
||||
assert.equal(result.status, 0, result.stderr || result.stdout);
|
||||
assert.equal(result.stdout.trim(), "Hello");
|
||||
});
|
||||
|
||||
test("screenshot returns standard success envelope and writes file", ["screenshot"], () => {
|
||||
const outputPath = join(tempDir, "page.png");
|
||||
const output = expectSuccess([
|
||||
"screenshot",
|
||||
dataHtml("<title>Hello</title>"),
|
||||
`--output=${outputPath}`,
|
||||
]);
|
||||
|
||||
assert.equal(output.ok, true);
|
||||
assert.equal(output.result.path, outputPath);
|
||||
assert.equal(typeof output.elapsedMs, "number");
|
||||
assert.equal(existsSync(outputPath), true);
|
||||
});
|
||||
|
||||
test("search validates kagi token in provider", ["search", "errors"], () => {
|
||||
const env = { ...process.env };
|
||||
delete env.KAGI_TOKEN;
|
||||
const result = runCli(["search", "example query"], { env });
|
||||
const output = parseJson(result.stderr);
|
||||
|
||||
assert.notEqual(result.status, 0, result.stdout || result.stderr);
|
||||
assert.equal(output.ok, false);
|
||||
assert.equal(output.error.code, "KAGI_TOKEN_REQUIRED");
|
||||
assert.match(output.error.message, /Kagi search requires/);
|
||||
assert.equal(typeof output.elapsedMs, "number");
|
||||
});
|
||||
|
||||
test("unknown command returns structured error", ["errors", "cli"], () => {
|
||||
const output = expectFailure(["nope", dataHtml("<title>Hello</title>")]);
|
||||
|
||||
assert.equal(output.ok, false);
|
||||
assert.equal(output.error.code, "UNKNOWN_COMMAND");
|
||||
assert.match(output.error.message, /Unknown command: nope/);
|
||||
assert.equal(typeof output.elapsedMs, "number");
|
||||
});
|
||||
|
||||
test("invalid timeout returns invalid option before browser startup", ["errors", "timeout"], () => {
|
||||
const output = expectFailure([
|
||||
"snapshot",
|
||||
dataHtml("<title>Hello</title>"),
|
||||
"--timeout=abc",
|
||||
]);
|
||||
|
||||
assert.equal(output.ok, false);
|
||||
assert.equal(output.error.code, "INVALID_OPTION");
|
||||
assert.match(output.error.message, /--timeout must be a positive integer/);
|
||||
assert.equal(typeof output.elapsedMs, "number");
|
||||
});
|
||||
|
||||
test("invalid wait-until returns invalid option", ["errors", "wait"], () => {
|
||||
const output = expectFailure([
|
||||
"snapshot",
|
||||
dataHtml("<title>Hello</title>"),
|
||||
"--wait-until=loaded",
|
||||
]);
|
||||
|
||||
assert.equal(output.ok, false);
|
||||
assert.equal(output.error.code, "INVALID_OPTION");
|
||||
assert.match(output.error.message, /Unsupported --wait-until value: loaded/);
|
||||
});
|
||||
|
||||
test("wait-js succeeds when condition is true", ["wait"], () => {
|
||||
const output = expectSuccess([
|
||||
"snapshot",
|
||||
dataHtml("<title>Hello</title>"),
|
||||
'--wait-js=return document.title === "Hello"',
|
||||
]);
|
||||
|
||||
assert.equal(output.ok, true);
|
||||
assert.equal(output.title, "Hello");
|
||||
});
|
||||
|
||||
test("wait-js timeout returns wait timeout", ["wait", "errors"], () => {
|
||||
const output = expectFailure([
|
||||
"snapshot",
|
||||
dataHtml("<title>Hello</title>"),
|
||||
"--wait-js=return false",
|
||||
"--timeout=1",
|
||||
]);
|
||||
|
||||
assert.equal(output.ok, false);
|
||||
assert.equal(output.error.code, "WAIT_TIMEOUT");
|
||||
assert.match(output.error.message, /waiting for --wait-js/);
|
||||
assert.equal(typeof output.elapsedMs, "number");
|
||||
assert.match(output.url, /^data:text\/html,/);
|
||||
});
|
||||
|
||||
test("wait-js exception returns script failed", ["wait", "errors", "js"], () => {
|
||||
const output = expectFailure([
|
||||
"snapshot",
|
||||
dataHtml("<title>Hello</title>"),
|
||||
'--wait-js=throw new Error("boom")',
|
||||
]);
|
||||
|
||||
assert.equal(output.ok, false);
|
||||
assert.equal(output.error.code, "SCRIPT_FAILED");
|
||||
assert.match(output.error.message, /--wait-js failed/);
|
||||
assert.match(output.error.message, /boom/);
|
||||
});
|
||||
|
||||
test("top-level javascript exception returns script failed", ["errors", "js"], () => {
|
||||
const output = expectFailure([
|
||||
"snapshot",
|
||||
dataHtml("<title>Hello</title>"),
|
||||
'--js=throw new Error("boom")',
|
||||
]);
|
||||
|
||||
assert.equal(output.ok, false);
|
||||
assert.equal(output.error.code, "SCRIPT_FAILED");
|
||||
assert.match(output.error.message, /Prelude script failed/);
|
||||
assert.match(output.error.message, /boom/);
|
||||
});
|
||||
|
||||
function listTests() {
|
||||
const tags = [...new Set(tests.flatMap((entry) => entry.tags))].sort();
|
||||
console.log(`Tags: ${tags.join(", ")}`);
|
||||
for (const entry of tests) {
|
||||
console.log(`${entry.name} [${entry.tags.join(", ")}]`);
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
if (shouldList) {
|
||||
listTests();
|
||||
return;
|
||||
}
|
||||
|
||||
const selectedTests = tests.filter(matchesFilters);
|
||||
if (selectedTests.length === 0) {
|
||||
console.error(`No tests matched: ${filters.join(", ")}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
let failed = 0;
|
||||
|
||||
// Run Tests
|
||||
for (const { name, fn } of selectedTests) {
|
||||
try {
|
||||
await fn();
|
||||
console.log(`ok - ${name}`);
|
||||
} catch (err) {
|
||||
failed += 1;
|
||||
console.error(`not ok - ${name}`);
|
||||
console.error(err.stack || err.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Clean Temporary Files
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
|
||||
if (failed > 0) {
|
||||
console.error(`${failed} test(s) failed`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`${selectedTests.length} test(s) passed`);
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
console.error(err.stack || err.message);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user