The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.
test(lib): cover five untested scripts/lib modules (#3641)
Affaan Mustafa5af4395e5c52
diff --git a/tests/lib/agent-tool-map.test.js b/tests/lib/agent-tool-map.test.jsnew file mode 100644index 000000000..50ba31664--- /dev/null+++ b/tests/lib/agent-tool-map.test.js@@ -0,0 +1,125 @@+'use strict';++const test = require('node:test');+const assert = require('node:assert/strict');++const { CLAUDE_TO_PI_TOOLS, CLAUDE_MODEL_TIERS, mapToolToPi } = require('../../scripts/lib/agent-tool-map');++// Pi built-ins per @earendil-works/pi-coding-agent dist/core/tools/index.d.ts.+// `powershell` is Windows-only and never emitted; `ls` has no Claude source.+const PI_BUILTINS = new Set(['read', 'bash', 'powershell', 'edit', 'write', 'grep', 'find', 'ls']);+const PI_READ_ONLY = new Set(['read', 'grep', 'find', 'ls']);+const CLAUDE_READ_ONLY = ['Read', 'Grep', 'Glob'];++test('the table covers exactly the six core Claude tools', () => {+ assert.deepEqual(Object.keys(CLAUDE_TO_PI_TOOLS).sort(), ['Bash', 'Edit', 'Glob', 'Grep', 'Read', 'Write']);+});++test('every target is a documented Pi built-in and powershell is never emitted', () => {+ for (const [claudeTool, piTool] of Object.entries(CLAUDE_TO_PI_TOOLS)) {+ assert.ok(PI_BUILTINS.has(piTool), `${claudeTool} -> ${piTool} is not a Pi built-in`);+ assert.notEqual(piTool, 'powershell', `${claudeTool} must not map to powershell`);+ }+});++test('read-only Claude tools never gain shell or write authority in Pi', () => {+ for (const claudeTool of CLAUDE_READ_ONLY) {+ const mapped = mapToolToPi(claudeTool);+ assert.equal(mapped.unsupported, false);+ assert.ok(PI_READ_ONLY.has(mapped.tool), `${claudeTool} -> ${mapped.tool} escalates a read-only tool`);+ }+});++test('mapped tools come back with their Pi name, unsupported false, and no note', () => {+ assert.deepEqual(mapToolToPi('Read'), { tool: 'read', unsupported: false });+ assert.deepEqual(mapToolToPi('Grep'), { tool: 'grep', unsupported: false });+ assert.deepEqual(mapToolToPi('Glob'), { tool: 'find', unsupported: false });+ assert.deepEqual(mapToolToPi('Bash'), { tool: 'bash', unsupported: false });+ assert.deepEqual(mapToolToPi('Edit'), { tool: 'edit', unsupported: false });+ assert.deepEqual(mapToolToPi('Write'), { tool: 'write', unsupported: false });+});++test('surrounding whitespace is trimmed before lookup', () => {+ assert.deepEqual(mapToolToPi(' Read '), { tool: 'read', unsupported: false });+ assert.deepEqual(mapToolToPi('\tBash\n'), { tool: 'bash', unsupported: false });+});++test('tool names are case sensitive, so a lowercase or shouted name is not guessed', () => {+ for (const name of ['read', 'BASH', 'write', 'Glob ']) {+ const mapped = mapToolToPi(name);+ if (name.trim() === 'Glob') continue;+ assert.equal(mapped.unsupported, true, `${JSON.stringify(name)} should be unsupported`);+ assert.equal(mapped.tool, null);+ }+});++test('WebSearch and WebFetch are deliberately unmapped', () => {+ for (const name of ['WebSearch', 'WebFetch']) {+ assert.deepEqual(mapToolToPi(name), {+ tool: null,+ unsupported: true,+ note: `unmapped tool: ${name} (not a Pi built-in)`+ });+ }+});++test('MCP tools are unsupported with a note that names the tool and the server step', () => {+ const name = 'mcp__context7__query-docs';+ const mapped = mapToolToPi(name);+ assert.equal(mapped.tool, null);+ assert.equal(mapped.unsupported, true);+ assert.ok(mapped.note.includes(name));+ assert.match(mapped.note, /configure the MCP server explicitly/);+ assert.doesNotMatch(mapped.note, /^unmapped tool:/);+});++test('an mcp__ prefix after whitespace is still recognised as an MCP tool', () => {+ assert.match(mapToolToPi(' mcp__x__y ').note, /^MCP tool mcp__x__y /);+});++test('unknown tools are reported, not dropped or guessed', () => {+ assert.deepEqual(mapToolToPi('Task'), {+ tool: null,+ unsupported: true,+ note: 'unmapped tool: Task (not a Pi built-in)'+ });+});++test('prototype property names are not treated as table entries', () => {+ for (const name of ['constructor', '__proto__', 'toString', 'hasOwnProperty', 'valueOf']) {+ const mapped = mapToolToPi(name);+ assert.equal(mapped.tool, null, `${name} leaked a prototype member as a tool`);+ assert.equal(mapped.unsupported, true);+ assert.equal(typeof mapped.note, 'string');+ }+});++test('non-string and empty input is coerced and reported rather than thrown', () => {+ for (const input of [undefined, null, 42, '', ' ']) {+ const mapped = mapToolToPi(input);+ assert.equal(mapped.tool, null, `${String(input)} should not map`);+ assert.equal(mapped.unsupported, true);+ assert.match(mapped.note, /^unmapped tool: /);+ }+ assert.ok(mapToolToPi(undefined).note.includes('undefined'));+ assert.ok(mapToolToPi(42).note.includes('42'));+});++test('a result either carries a Pi tool or is unsupported, never both or neither', () => {+ const inputs = [...Object.keys(CLAUDE_TO_PI_TOOLS), 'WebFetch', 'mcp__a__b', 'Task', '', 'read'];+ for (const input of inputs) {+ const mapped = mapToolToPi(input);+ assert.equal(mapped.tool !== null, !mapped.unsupported, `${JSON.stringify(input)}`);+ assert.equal('note' in mapped, mapped.unsupported, `${JSON.stringify(input)} note presence`);+ }+});++test('the tables are frozen and the model tiers are the three Claude tiers in order', () => {+ assert.ok(Object.isFrozen(CLAUDE_TO_PI_TOOLS));+ assert.ok(Object.isFrozen(CLAUDE_MODEL_TIERS));+ assert.deepEqual(CLAUDE_MODEL_TIERS, ['haiku', 'sonnet', 'opus']);+ assert.throws(() => {+ CLAUDE_TO_PI_TOOLS.WebFetch = 'bash';+ }, TypeError);+ assert.equal(mapToolToPi('WebFetch').unsupported, true);+});diff --git a/tests/lib/compute-sponsor.test.js b/tests/lib/compute-sponsor.test.jsnew file mode 100644index 000000000..22c44a9dd--- /dev/null+++ b/tests/lib/compute-sponsor.test.js@@ -0,0 +1,77 @@+'use strict';++const test = require('node:test');+const assert = require('node:assert/strict');++const computeSponsor = require('../../scripts/lib/compute-sponsor');++const { ITO_COMPUTE_URL, getComputeSponsorCopy } = computeSponsor;+const URL_TOKEN_PATTERN = /https?:\/\/[^\s<>"'`(){}\\]+/g;++function urlsIn(text) {+ return (text.match(URL_TOKEN_PATTERN) || []).map(token => token.replace(/[.,;:!?]+$/, ''));+}++test('the compute URL is the exact Ito route with no path, query, hash, or credentials', () => {+ const parsed = new URL(ITO_COMPUTE_URL);+ assert.equal(parsed.protocol, 'https:');+ assert.equal(parsed.hostname, 'compute.itomarkets.com');+ assert.equal(parsed.port, '');+ assert.equal(parsed.username, '');+ assert.equal(parsed.password, '');+ assert.equal(parsed.pathname, '/');+ assert.equal(parsed.search, '');+ assert.equal(parsed.hash, '');+ assert.equal(ITO_COMPUTE_URL, 'https://compute.itomarkets.com');+});++test('the copy links the compute URL exactly once and links nowhere else', () => {+ assert.deepEqual(urlsIn(getComputeSponsorCopy()), [ITO_COMPUTE_URL]);+});++test('the copy names Ito as the preferred sponsor and keeps other providers open', () => {+ const copy = getComputeSponsorCopy();+ assert.match(copy, /^Run or self-host any open-source model\./);+ assert.match(copy, /Itô is ECC's preferred compute sponsor/);+ assert.match(copy, /Any GPU provider works\./);+});++test('the copy keeps the sponsorship link passive', () => {+ const copy = getComputeSponsorCopy();+ assert.match(copy, /This sponsorship link is passive/);+ assert.match(copy, /does not invoke an RFQ, reserve capacity, provision compute, or configure serving/);+});++test('the copy keeps the ecc ito find bridge separate, opt-in, and non-reserving', () => {+ const copy = getComputeSponsorCopy();+ assert.match(copy, /Separately, the opt-in "ecc ito find" bridge/);+ assert.match(copy, /explicitly configured canonical Itô CLI/);+ assert.match(copy, /submits a live authenticated RFQ; it does not reserve capacity\./);+});++test('the copy does not claim managed inference is live', () => {+ assert.match(getComputeSponsorCopy(), /Managed inference through Itô is not live yet\.$/);+});++test('the copy is a single trimmed line of printable text', () => {+ const copy = getComputeSponsorCopy();+ assert.equal(typeof copy, 'string');+ assert.ok(copy.length > 0);+ assert.equal(copy, copy.trim());+ // eslint-disable-next-line no-control-regex+ assert.doesNotMatch(copy, /[\x00-\x1f\x7f]/);+ assert.doesNotMatch(copy, / {2}/);+});++test('the copy is deterministic across calls', () => {+ assert.equal(getComputeSponsorCopy(), getComputeSponsorCopy());+});++test('the module surface is frozen and exports only the URL and the copy builder', () => {+ assert.ok(Object.isFrozen(computeSponsor));+ assert.deepEqual(Object.keys(computeSponsor).sort(), ['ITO_COMPUTE_URL', 'getComputeSponsorCopy']);+ assert.throws(() => {+ computeSponsor.ITO_COMPUTE_URL = 'https://example.invalid/';+ }, TypeError);+ assert.equal(computeSponsor.ITO_COMPUTE_URL, 'https://compute.itomarkets.com');+});diff --git a/tests/lib/cursor-agent-names.test.js b/tests/lib/cursor-agent-names.test.jsnew file mode 100644index 000000000..bbff29622--- /dev/null+++ b/tests/lib/cursor-agent-names.test.js@@ -0,0 +1,80 @@+'use strict';++const test = require('node:test');+const assert = require('node:assert/strict');+const path = require('path');++const { toCursorAgentFileName, toCursorAgentRelativePath } = require('../../scripts/lib/cursor-agent-names');++test('a bare file name gets the ecc- prefix', () => {+ assert.equal(toCursorAgentFileName('planner.md'), 'ecc-planner.md');+ assert.equal(toCursorAgentFileName('code-reviewer.md'), 'ecc-code-reviewer.md');+});++test('an already prefixed name is left alone, so the transform is idempotent', () => {+ assert.equal(toCursorAgentFileName('ecc-planner.md'), 'ecc-planner.md');+ assert.equal(toCursorAgentFileName(toCursorAgentFileName('planner.md')), 'ecc-planner.md');+});++test('empty and missing names pass through unchanged', () => {+ assert.equal(toCursorAgentFileName(''), '');+ assert.equal(toCursorAgentFileName(undefined), undefined);+ assert.equal(toCursorAgentFileName(null), null);+});++test('only a leading, lowercase ecc- counts as the prefix', () => {+ assert.equal(toCursorAgentFileName('my-ecc-agent.md'), 'ecc-my-ecc-agent.md');+ assert.equal(toCursorAgentFileName('ECC-planner.md'), 'ecc-ECC-planner.md');+ assert.equal(toCursorAgentFileName('ecc.md'), 'ecc-ecc.md');+ assert.equal(toCursorAgentFileName('ecc_planner.md'), 'ecc-ecc_planner.md');+});++test('a relative path keeps its directories and renames only the last segment', () => {+ assert.equal(toCursorAgentRelativePath('review/planner.md'), path.join('review', 'ecc-planner.md'));+ assert.equal(toCursorAgentRelativePath('a/b/c.md'), path.join('a', 'b', 'ecc-c.md'));+ assert.equal(toCursorAgentRelativePath('ecc-review/planner.md'), path.join('ecc-review', 'ecc-planner.md'));+});++test('a path without directories behaves like a bare file name', () => {+ assert.equal(toCursorAgentRelativePath('planner.md'), 'ecc-planner.md');+ assert.equal(toCursorAgentRelativePath('ecc-planner.md'), 'ecc-planner.md');+});++test('backslash and repeated separators are normalized to the platform separator', () => {+ assert.equal(toCursorAgentRelativePath('review\\planner.md'), path.join('review', 'ecc-planner.md'));+ assert.equal(toCursorAgentRelativePath('review//planner.md'), path.join('review', 'ecc-planner.md'));+ assert.equal(toCursorAgentRelativePath('review\\\\sub/planner.md'), path.join('review', 'sub', 'ecc-planner.md'));+});++test('a trailing separator is dropped and the last real segment is renamed', () => {+ assert.equal(toCursorAgentRelativePath('review/planner.md/'), path.join('review', 'ecc-planner.md'));+});++test('empty, missing, and separator-only paths come back unchanged', () => {+ assert.equal(toCursorAgentRelativePath(''), '');+ assert.equal(toCursorAgentRelativePath(undefined), undefined);+ assert.equal(toCursorAgentRelativePath(null), null);+ assert.equal(toCursorAgentRelativePath('/'), '/');+ assert.equal(toCursorAgentRelativePath('\\\\'), '\\\\');+});++test('the result is always relative to the destination root', () => {+ // The executor joins the result under the target agents directory, so a+ // leading separator must not be able to re-root the destination.+ const result = toCursorAgentRelativePath('/etc/planner.md');+ assert.equal(result, path.join('etc', 'ecc-planner.md'));+ assert.equal(path.isAbsolute(result), false);+});++test('dot segments are not interpreted; the transform renames, it does not resolve', () => {+ // The executor walks real directory entries, so these never reach the+ // transform in practice. Pin that it does not quietly collapse them either.+ assert.equal(toCursorAgentRelativePath('./planner.md'), path.join('.', 'ecc-planner.md'));+ assert.equal(toCursorAgentRelativePath('../planner.md'), path.join('..', 'ecc-planner.md'));+});++test('the relative path transform composes the file name transform', () => {+ for (const file of ['planner.md', 'ecc-planner.md', 'README']) {+ assert.equal(toCursorAgentRelativePath(`nested/${file}`), path.join('nested', toCursorAgentFileName(file)));+ }+});diff --git a/tests/lib/feedback-links.test.js b/tests/lib/feedback-links.test.jsnew file mode 100644index 000000000..c3777bc45--- /dev/null+++ b/tests/lib/feedback-links.test.js@@ -0,0 +1,112 @@+'use strict';++const test = require('node:test');+const assert = require('node:assert/strict');+const fs = require('fs');+const path = require('path');++const { FEEDBACK_ROUTES, exitFeedbackLines, getFeedbackPayload, problemReportLines } = require('../../scripts/lib/feedback-links');++const ISSUE_TEMPLATE_DIR = path.join(__dirname, '..', '..', '.github', 'ISSUE_TEMPLATE');+const ISSUES_NEW_URL = 'https://github.com/affaan-m/ECC/issues/new';++test('every route is a new-issue URL on the ECC repository with only a template query', () => {+ assert.deepEqual(Object.keys(FEEDBACK_ROUTES).sort(), ['feature', 'feedback', 'problem']);+ for (const [name, route] of Object.entries(FEEDBACK_ROUTES)) {+ const parsed = new URL(route);+ assert.equal(parsed.protocol, 'https:', `${name} protocol`);+ assert.equal(parsed.hostname, 'github.com', `${name} host`);+ assert.equal(parsed.pathname, '/affaan-m/ECC/issues/new', `${name} path`);+ assert.equal(parsed.username, '', `${name} username`);+ assert.equal(parsed.password, '', `${name} password`);+ assert.equal(parsed.hash, '', `${name} hash`);+ assert.deepEqual([...parsed.searchParams.keys()], ['template'], `${name} query keys`);+ }+});++test('every route points at an issue template that exists in the repository', () => {+ for (const [name, route] of Object.entries(FEEDBACK_ROUTES)) {+ const template = new URL(route).searchParams.get('template');+ assert.match(template, /^[a-z-]+\.yml$/, `${name} template name`);+ assert.ok(fs.existsSync(path.join(ISSUE_TEMPLATE_DIR, template)), `${name} route names ${template}, which is not in .github/ISSUE_TEMPLATE`);+ }+});++test('each route maps to the intended template', () => {+ assert.equal(FEEDBACK_ROUTES.problem, `${ISSUES_NEW_URL}?template=install-problem.yml`);+ assert.equal(FEEDBACK_ROUTES.feedback, `${ISSUES_NEW_URL}?template=quick-feedback.yml`);+ assert.equal(FEEDBACK_ROUTES.feature, `${ISSUES_NEW_URL}?template=feature-request.yml`);+});++test('the route table is frozen', () => {+ assert.ok(Object.isFrozen(FEEDBACK_ROUTES));+ assert.throws(() => {+ FEEDBACK_ROUTES.problem = 'https://example.invalid/';+ }, TypeError);+ assert.throws(() => {+ FEEDBACK_ROUTES.extra = 'https://example.invalid/';+ }, TypeError);+ assert.equal(FEEDBACK_ROUTES.problem, `${ISSUES_NEW_URL}?template=install-problem.yml`);+});++test('the payload declares the public schema and no diagnostics upload', () => {+ assert.deepEqual(getFeedbackPayload(), {+ schemaVersion: 'ecc.feedback.v1',+ privacy: 'public-github',+ diagnosticsUploaded: false,+ routes: { ...FEEDBACK_ROUTES }+ });+});++test('the payload survives a JSON round trip unchanged', () => {+ const payload = getFeedbackPayload();+ assert.deepEqual(JSON.parse(JSON.stringify(payload)), payload);+});++test('each payload is a fresh copy so a caller cannot alter the route table', () => {+ const first = getFeedbackPayload();+ const second = getFeedbackPayload();+ assert.notEqual(first, second);+ assert.notEqual(first.routes, second.routes);+ assert.deepEqual(first, second);++ first.routes.problem = 'https://example.invalid/';+ first.diagnosticsUploaded = true;+ assert.equal(getFeedbackPayload().routes.problem, FEEDBACK_ROUTES.problem);+ assert.equal(getFeedbackPayload().diagnosticsUploaded, false);+});++test('problem report lines carry the problem route and the redaction warning', () => {+ const lines = problemReportLines();+ assert.equal(lines.length, 3);+ assert.match(lines[0], /public GitHub issue/);+ assert.equal(lines[1], FEEDBACK_ROUTES.problem);+ assert.match(lines[2], /does not upload diagnostics/);+ assert.match(lines[2], /Redact paths, repository names, prompts, and secrets/);+});++test('exit feedback lines carry the feedback route and promise not to block uninstall', () => {+ const lines = exitFeedbackLines();+ assert.equal(lines.length, 3);+ assert.match(lines[0], /public GitHub issue/);+ assert.equal(lines[1], FEEDBACK_ROUTES.feedback);+ assert.match(lines[2], /does not upload diagnostics/);+ assert.match(lines[2], /block uninstall/);+});++test('every line is a single printable line, so join("\\n") output stays well formed', () => {+ for (const line of [...problemReportLines(), ...exitFeedbackLines()]) {+ assert.equal(typeof line, 'string');+ assert.ok(line.length > 0);+ // eslint-disable-next-line no-control-regex+ assert.doesNotMatch(line, /[\x00-\x1f\x7f]/);+ assert.equal(line, line.trim());+ }+});++test('line arrays are rebuilt per call', () => {+ const lines = problemReportLines();+ lines[1] = 'tampered';+ assert.equal(problemReportLines()[1], FEEDBACK_ROUTES.problem);+ assert.notEqual(exitFeedbackLines(), exitFeedbackLines());+});diff --git a/tests/lib/invocation-environment.test.js b/tests/lib/invocation-environment.test.jsnew file mode 100644index 000000000..509ec5508--- /dev/null+++ b/tests/lib/invocation-environment.test.js@@ -0,0 +1,106 @@+'use strict';++const test = require('node:test');+const assert = require('node:assert/strict');++const { resolveInvocationEnvironment } = require('../../scripts/lib/invocation-environment');++const PROBE = 'ECC_INVOCATION_ENVIRONMENT_PROBE';++function withProbe(value, fn) {+ const saved = process.env[PROBE];+ process.env[PROBE] = value;+ try {+ return fn();+ } finally {+ if (saved === undefined) {+ delete process.env[PROBE];+ } else {+ process.env[PROBE] = saved;+ }+ }+}++test('with no options the ambient process environment is returned as a copy', () => {+ withProbe('ambient', () => {+ const resolved = resolveInvocationEnvironment();+ assert.notEqual(resolved, process.env);+ assert.deepEqual(resolved, { ...process.env });+ assert.equal(resolved[PROBE], 'ambient');+ });+});++test('an empty options object behaves like no options', () => {+ withProbe('ambient', () => {+ assert.deepEqual(resolveInvocationEnvironment({}), { ...process.env });+ });+});++test('mutating the copy never writes back to process.env', () => {+ const resolved = resolveInvocationEnvironment();+ resolved[PROBE] = 'leaked';+ delete resolved.PATH;+ assert.equal(process.env[PROBE], undefined);+ assert.equal(typeof process.env.PATH, 'string');+});++test('the copy is a snapshot and does not see later process.env changes', () => {+ const resolved = resolveInvocationEnvironment();+ withProbe('later', () => {+ assert.equal(resolved[PROBE], undefined);+ });+});++test('an explicit env is copied, not shared, and replaces the ambient environment', () => {+ withProbe('ambient', () => {+ const source = { HOME: '/scoped/home', PATH: '/scoped/bin' };+ const resolved = resolveInvocationEnvironment({ env: source });+ assert.notEqual(resolved, source);+ assert.deepEqual(resolved, source);+ assert.equal(resolved[PROBE], undefined);++ resolved.EXTRA = '1';+ assert.equal(source.EXTRA, undefined);+ });+});++test('only own enumerable keys of an explicit env are copied', () => {+ const source = Object.create({ INHERITED: 'yes' });+ source.OWN = 'yes';+ Object.defineProperty(source, 'HIDDEN', { value: 'yes', enumerable: false });+ assert.deepEqual(resolveInvocationEnvironment({ env: source }), { OWN: 'yes' });+});++test('env: null and env: undefined mean an empty environment, not the ambient one', () => {+ withProbe('ambient', () => {+ assert.deepEqual(resolveInvocationEnvironment({ env: null }), {});+ assert.deepEqual(resolveInvocationEnvironment({ env: undefined }), {});+ });+});++test('an explicit env wins over homeDir', () => {+ const resolved = resolveInvocationEnvironment({ env: { A: '1' }, homeDir: '/scoped' });+ assert.deepEqual(resolved, { A: '1' });+});++test('a scoped homeDir gives an empty environment so ambient variables cannot leak into the install', () => {+ withProbe('ambient', () => {+ const resolved = resolveInvocationEnvironment({ homeDir: '/scoped/home' });+ assert.deepEqual(resolved, {});+ assert.equal(resolved.HOME, undefined);+ assert.equal(resolved[PROBE], undefined);+ });+});++test('a blank or non-string homeDir does not count as scoped', () => {+ withProbe('ambient', () => {+ for (const homeDir of ['', ' ', '\n', null, undefined, 42, true]) {+ assert.deepEqual(resolveInvocationEnvironment({ homeDir }), { ...process.env }, `homeDir ${JSON.stringify(homeDir)} should fall back to the ambient environment`);+ }+ });+});++test('each call returns a distinct object', () => {+ assert.notEqual(resolveInvocationEnvironment(), resolveInvocationEnvironment());+ assert.notEqual(resolveInvocationEnvironment({ homeDir: '/scoped' }), resolveInvocationEnvironment({ homeDir: '/scoped' }));+});