Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- `npm run search -- --family <name>` now rejects an unknown family (for example
`action` instead of `actions`) with the list of valid ones, and matches the name
case-insensitively. It used to print "No matching elements.", which reads as
"this concept is not in the table" during the duplicate check the ontology
proposal template asks for. `--family` or `--symbol` with no value now fails
instead of silently listing the whole table.

## [0.7.0] - 2026-11-27

### Added
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ enforced by tests rather than by convention:
or the README test inventory drift from the shipped code.
- `test/public-api.test.ts` — the loader, schema entry point, prompt builder, and
an atom are reachable through the paths a package consumer uses.
- `test/search.test.ts` — `npm run search` filters by family and symbol, and
rejects an unknown family or a flag with no value instead of answering "no match".

Run with `npm test`. No test framework dependency; `node:test` and `tsx` only.

Expand Down
30 changes: 26 additions & 4 deletions scripts/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* before it is written.
*/

import { loadOntology, type OntologyElement } from '../src/ontology.js';
import { FAMILY_IDS, isFamilyId, loadOntology, type OntologyElement } from '../src/ontology.js';

interface Options {
terms: string[];
Expand All @@ -19,6 +19,18 @@ interface Options {
list: boolean;
}

function fail(message: string): never {
process.stderr.write(`${message}\n`);
process.exit(1);
}

/** The value after a flag, or an error: a flag left without one must not widen the search. */
function flagValue(argv: string[], i: number, flag: string): string {
const value = argv[i + 1];
if (value === undefined || value.startsWith('-')) fail(`Missing value for ${flag}`);
return value;
}

function parseArgs(argv: string[]): Options {
const terms: string[] = [];
let family: string | undefined;
Expand All @@ -28,12 +40,12 @@ function parseArgs(argv: string[]): Options {
for (let i = 0; i < argv.length; i += 1) {
const arg = argv[i];
if (arg === '--family' || arg === '-f') {
family = argv[i + 1];
family = flagValue(argv, i, arg);
i += 1;
} else if (arg?.startsWith('--family=')) {
family = arg.slice('--family='.length);
} else if (arg === '--symbol' || arg === '-s') {
symbol = argv[i + 1];
symbol = flagValue(argv, i, arg);
i += 1;
} else if (arg?.startsWith('--symbol=')) {
symbol = arg.slice('--symbol='.length);
Expand All @@ -47,6 +59,16 @@ function parseArgs(argv: string[]): Options {
}
}

if (symbol === '') fail('Missing value for --symbol');
if (family !== undefined) {
// An unknown family used to report "No matching elements.", which reads as
// "this concept is not in the table" and invites a duplicate proposal.
family = family.toLowerCase();
if (!isFamilyId(family)) {
fail(`Unknown family "${family}". Expected one of: ${FAMILY_IDS.join(', ')}`);
}
}

return { terms, family, symbol, list };
}

Expand All @@ -56,7 +78,7 @@ function printUsage(): void {
'Usage: npm run search -- [options] [terms...]',
'',
'Options:',
' -f, --family <name> restrict to one family',
` -f, --family <name> restrict to one family (${FAMILY_IDS.join(', ')})`,
' -s, --symbol <sym> look up one exact symbol',
' -l, --list list every element',
' -h, --help show this message',
Expand Down
59 changes: 59 additions & 0 deletions test/search.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import * as path from 'node:path';
import { describe, it } from 'node:test';
import { fileURLToPath } from 'node:url';

const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const SCRIPT = path.join(ROOT, 'scripts', 'search.ts');

function search(...args: string[]) {
const result = spawnSync(process.execPath, ['--import', 'tsx', SCRIPT, ...args], {
cwd: ROOT,
encoding: 'utf8',
});
return { status: result.status, stdout: result.stdout, stderr: result.stderr };
}

/**
* `npm run search` is the duplicate check the ontology proposal template points
* to, so a mistyped flag must fail loudly rather than report "no match" (which
* reads as "this concept is missing") or silently list the whole table.
*/
describe('scripts/search.ts', () => {
it('finds an element within a family', () => {
const { status, stdout } = search('message', '--family', 'actions');
assert.equal(status, 0);
assert.match(stdout, /\bMg\s+Message\s+actions\b/);
assert.doesNotMatch(stdout, /\bMs\s+Message\b/);
});

it('matches the family name case-insensitively', () => {
const { status, stdout } = search('message', '--family=Actions');
assert.equal(status, 0);
assert.match(stdout, /\bMg\s+Message\b/);
});

it('rejects an unknown family and names the valid ones', () => {
const { status, stdout, stderr } = search('message', '--family', 'action');
assert.equal(status, 1);
assert.equal(stdout, '');
assert.match(stderr, /Unknown family "action"/);
assert.match(stderr, /objects, properties, actions, interfaces, intelligence, rules/);
});

for (const args of [['--family'], ['--family', '--list'], ['--symbol'], ['--symbol=']]) {
it(`rejects a flag with no value: ${args.join(' ')}`, () => {
const { status, stdout, stderr } = search(...args);
assert.equal(status, 1);
assert.equal(stdout, '');
assert.match(stderr, /Missing value for --(family|symbol)/);
});
}

it('looks up a symbol case-insensitively', () => {
const { status, stdout } = search('--symbol', 'tk');
assert.equal(status, 0);
assert.match(stdout, /\bTk\s+Task\s+objects\b/);
});
});