Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 121 lines 4.42 KB
Raw
1 import * as fs from 'node:fs';
2 import * as path from 'node:path';
3
4 export interface CommandTestExemption {
5 readonly command: string;
6 readonly reason: string;
7 }
8
9 export interface CommandTestInputs {
10 readonly commands: readonly string[];
11 readonly tests: readonly string[];
12 readonly exemptions: readonly CommandTestExemption[];
13 }
14
15 const COMMAND_SUFFIX = '.cmd.ts';
16 const TEST_SUFFIX = '.cmd.test.ts';
17
18 // Command paths are relative to src/commands/; tests are relative to
19 // test/src/commands/. Both use forward slashes.
20 export const COMMAND_TEST_EXEMPTIONS: readonly CommandTestExemption[] = [];
21
22 // A group entry is `<dir>.cmd.ts` or `root-<dir>.cmd.ts` inside `<dir>/` when
23 // `<dir>/` holds other command modules; it only wires subcommands and is
24 // covered by root-help-consistency.test.ts. The same name with nothing below
25 // it is a top-level command in the per-command directory shape
26 // (`login/login.cmd.ts`).
27 export const isGroupEntry = (command: string, commands: readonly string[]): boolean => {
28 const segments = command.split('/');
29 const dir = segments.at(-2);
30 const base = segments.at(-1);
31 if (dir === undefined || base === undefined) return false;
32 if (base !== `${dir}${COMMAND_SUFFIX}` && base !== `root-${dir}${COMMAND_SUFFIX}`) return false;
33 const prefix = `${segments.slice(0, -1).join('/')}/`;
34 return commands.some(other => other !== command && other.startsWith(prefix));
35 };
36
37 export const mirroredTestPath = (command: string): string =>
38 command
39 .split('/')
40 .filter(segment => segment !== 'commands')
41 .join('/')
42 .replace(/\.cmd\.ts$/, TEST_SUFFIX);
43
44 // Resolves a test to the command it exercises: the longest dotted prefix of
45 // its basename that names a `<prefix>.cmd.ts` anywhere under src/commands/.
46 // Extra segments name a test aspect (`setup.telemetry`, `tools.execute.client`).
47 export const resolveTestCommand = (
48 test: string,
49 commands: readonly string[]
50 ): string | undefined => {
51 const base = path.posix.basename(test, TEST_SUFFIX);
52 const byBasename = new Map(commands.map(command => [path.posix.basename(command), command]));
53 const parts = base.split('.');
54 for (let length = parts.length; length > 0; length--) {
55 const match = byBasename.get(`${parts.slice(0, length).join('.')}${COMMAND_SUFFIX}`);
56 if (match !== undefined) return match;
57 }
58 return undefined;
59 };
60
61 export const checkCommandTests = ({
62 commands,
63 tests,
64 exemptions,
65 }: CommandTestInputs): readonly string[] => {
66 const errors: string[] = [];
67 const testSet = new Set(tests);
68 const commandSet = new Set(commands);
69 const exempted = new Map(exemptions.map(exemption => [exemption.command, exemption]));
70
71 for (const exemption of exemptions) {
72 if (!commandSet.has(exemption.command)) {
73 errors.push(
74 `stale exemption: ${exemption.command} is not a command under src/commands/; remove it from COMMAND_TEST_EXEMPTIONS.`
75 );
76 } else if (isGroupEntry(exemption.command, commands)) {
77 errors.push(
78 `stale exemption: ${exemption.command} is a group entry and needs no test; remove it from COMMAND_TEST_EXEMPTIONS.`
79 );
80 }
81 }
82
83 for (const command of [...commands].sort()) {
84 if (isGroupEntry(command, commands)) continue;
85 const expected = mirroredTestPath(command);
86 const hasTest = testSet.has(expected);
87 if (exempted.has(command)) {
88 if (hasTest) {
89 errors.push(
90 `stale exemption: ${command} now has test/src/commands/${expected}; remove it from COMMAND_TEST_EXEMPTIONS.`
91 );
92 }
93 } else if (!hasTest) {
94 errors.push(`missing test: src/commands/${command} needs test/src/commands/${expected}.`);
95 }
96 }
97
98 for (const test of [...tests].sort()) {
99 if (resolveTestCommand(test, commands) === undefined) {
100 errors.push(
101 `orphan test: test/src/commands/${test} matches no <prefix>.cmd.ts under src/commands/.`
102 );
103 }
104 }
105
106 return errors;
107 };
108
109 const listFiles = (root: string, suffix: string): string[] =>
110 fs
111 .readdirSync(root, { recursive: true, encoding: 'utf8' })
112 .filter(file => file.endsWith(suffix) && fs.statSync(path.join(root, file)).isFile())
113 .map(file => file.split(path.sep).join('/'))
114 .sort();
115
116 export const collectCommandTests = (
117 packageRoot: string
118 ): { readonly commands: readonly string[]; readonly tests: readonly string[] } => ({
119 commands: listFiles(path.join(packageRoot, 'src', 'commands'), COMMAND_SUFFIX),
120 tests: listFiles(path.join(packageRoot, 'test', 'src', 'commands'), TEST_SUFFIX),
121 });