import assert from 'node:assert/strict'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { spawnSync } from 'node:child_process'; import test from 'node:test'; import { fileURLToPath } from 'node:url'; import { ESLint } from 'eslint'; import { loadExtensions } from '../node_modules/@earendil-works/pi-coding-agent/dist/core/extensions/loader.js'; const root = fileURLToPath(new URL('../', import.meta.url)); function fixture(t) { // Keep synthetic sources inside the actual TS project: no substitute parser config. const directory = fs.mkdtempSync(path.join(root, 'src/quality-fixture-')); t.after(() => fs.rmSync(directory, { recursive: true, force: true })); return (name, code) => { const filename = path.join(directory, name); fs.writeFileSync(filename, code); return filename; }; } function command(script, args) { return spawnSync(process.execPath, [path.join(root, script), ...args], { cwd: root, encoding: 'utf8', timeout: 120_000, }); } function successful(result) { assert.equal(result.error, undefined); assert.equal(result.status, 0, result.stdout + result.stderr); } function rejected(result) { assert.equal(result.error, undefined); assert.equal(result.status, 1, result.stdout + result.stderr); } test('canonical ESLint accepts clean TS and rejects unsafe values, lost promises and inline disabling', async (t) => { const write = fixture(t); const examples = [ write('clean.ts', 'export function twice(value: number): number { return value * 2; }\n'), write('unsafe.ts', 'export function twice(value: any): number { return value; }\n'), write('floating.ts', 'Promise.resolve(1);\n'), write('disabled.ts', '/* eslint-disable */\nPromise.resolve(1);\n'), write( 'complex.ts', 'export function nested(a: boolean, b: boolean, c: boolean, d: boolean, e: boolean): number { if (a) { if (b) { if (c) { if (d) { if (e) { return 1; } } } } } return 0; }\n', ), ]; const makeFunction = (lines) => [ 'export function increment(value: number): number {', ...Array(lines - 3).fill(' value += 1;'), ' return value;', '}', ].join('\n'); examples.push(write('limit.ts', makeFunction(80)), write('over.ts', makeFunction(81))); const eslint = new ESLint({ cwd: root }); const results = await eslint.lintFiles(examples); assert.equal(results.length, examples.length); for (const result of results) { assert.equal(result.fatalErrorCount, 0); assert( !result.messages.some( (message) => message.ruleId === null && message.message.includes('ignored'), ), ); } assert.deepEqual(results[0].messages, []); const has = (index, rule) => results[index].messages.some((message) => message.ruleId === rule); assert(has(1, '@typescript-eslint/no-explicit-any')); assert(has(1, '@typescript-eslint/no-unsafe-return')); assert(has(2, '@typescript-eslint/no-floating-promises')); assert(has(3, '@typescript-eslint/no-floating-promises')); assert(has(4, 'sonarjs/cognitive-complexity')); assert(!has(5, 'max-lines-per-function')); assert(has(6, 'max-lines-per-function')); // The CLI must propagate the failure, including a directive with no other finding. rejected(command('node_modules/eslint/bin/eslint.js', ['--max-warnings', '0', examples[3]])); const directiveOnly = write('directive.ts', '/* eslint-disable */\nexport const value = 1;\n'); rejected(command('node_modules/eslint/bin/eslint.js', ['--max-warnings', '0', directiveOnly])); }); test('canonical TypeScript and formatter reject violations and accept their clean controls', (t) => { const write = fixture(t); const file = write('types.ts', 'export const count: number = "wrong";\n'); const invalidType = command('node_modules/typescript/bin/tsc', ['--project', 'tsconfig.json']); assert.equal(invalidType.error, undefined); assert.equal(invalidType.status, 2, invalidType.stdout + invalidType.stderr); assert.match(invalidType.stdout, /TS2322/); fs.writeFileSync(file, 'export const count: number = 1;\n'); successful(command('node_modules/typescript/bin/tsc', ['--project', 'tsconfig.json'])); const unformatted = write('format.ts', 'export const count=1;'); rejected(command('node_modules/prettier/bin/prettier.cjs', ['--check', unformatted])); fs.writeFileSync(unformatted, 'export const count = 1;\n'); successful(command('node_modules/prettier/bin/prettier.cjs', ['--check', unformatted])); }); test('pinned Pi loader loads the package entry and registers its real command and events', async (t) => { const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'pi-supervisor-loader-')); t.after(() => fs.rmSync(cwd, { recursive: true, force: true })); const manifest = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8')); assert.deepEqual(manifest.pi.extensions, ['./src/index.ts']); // An installed extension must load without this repository's devDependencies. fs.cpSync(path.join(root, 'src'), path.join(cwd, 'src'), { recursive: true, }); fs.writeFileSync( path.join(cwd, 'package.json'), JSON.stringify({ type: 'module', pi: manifest.pi }), ); const result = await loadExtensions( manifest.pi.extensions.map((entry) => path.join(cwd, entry)), cwd, ); assert.deepEqual(result.errors, []); assert.equal(result.extensions.length, 1); const extension = result.extensions[0]; assert.deepEqual([...extension.commands.keys()], ['supervisor']); assert.deepEqual([...extension.handlers.keys()].sort(), [ 'agent_settled', 'session_shutdown', 'session_start', 'session_tree', ]); const notices = []; await extension.commands.get('supervisor').handler('help', { hasUI: true, ui: { notify: (message) => notices.push(message) }, }); assert.equal(notices.length, 1); assert.match(notices[0], /\/supervisor start/); });