mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-15 13:03:42 +00:00
Breaking changes: - Settings: 'hooks' and 'customTools' arrays replaced with 'extensions' - CLI: '--hook' and '--tool' flags replaced with '--extension' / '-e' - API: HookMessage renamed to CustomMessage, role 'hookMessage' to 'custom' - API: FileSlashCommand renamed to PromptTemplate - API: discoverSlashCommands() renamed to discoverPromptTemplates() - Directories: commands/ renamed to prompts/ for prompt templates Migration: - Session version bumped to 3 (auto-migrates v2 sessions) - Old 'hookMessage' role entries converted to 'custom' Structural changes: - src/core/hooks/ and src/core/custom-tools/ merged into src/core/extensions/ - src/core/slash-commands.ts renamed to src/core/prompt-templates.ts - examples/hooks/ and examples/custom-tools/ merged into examples/extensions/ - docs/hooks.md and docs/custom-tools.md merged into docs/extensions.md New test coverage: - test/extensions-runner.test.ts (10 tests) - test/extensions-discovery.test.ts (26 tests) - test/prompt-templates.test.ts
277 lines
10 KiB
TypeScript
277 lines
10 KiB
TypeScript
/**
|
|
* Tests for prompt template argument parsing and substitution.
|
|
*
|
|
* Tests verify:
|
|
* - Argument parsing with quotes and special characters
|
|
* - Placeholder substitution ($1, $2, $@, $ARGUMENTS)
|
|
* - No recursive substitution of patterns in argument values
|
|
* - Edge cases and integration between parsing and substitution
|
|
*/
|
|
|
|
import { describe, expect, test } from "vitest";
|
|
import { parseCommandArgs, substituteArgs } from "../src/core/prompt-templates.js";
|
|
|
|
// ============================================================================
|
|
// substituteArgs
|
|
// ============================================================================
|
|
|
|
describe("substituteArgs", () => {
|
|
test("should replace $ARGUMENTS with all args joined", () => {
|
|
expect(substituteArgs("Test: $ARGUMENTS", ["a", "b", "c"])).toBe("Test: a b c");
|
|
});
|
|
|
|
test("should replace $@ with all args joined", () => {
|
|
expect(substituteArgs("Test: $@", ["a", "b", "c"])).toBe("Test: a b c");
|
|
});
|
|
|
|
test("should replace $@ and $ARGUMENTS identically", () => {
|
|
const args = ["foo", "bar", "baz"];
|
|
expect(substituteArgs("Test: $@", args)).toBe(substituteArgs("Test: $ARGUMENTS", args));
|
|
});
|
|
|
|
// CRITICAL: argument values containing patterns should remain literal
|
|
test("should NOT recursively substitute patterns in argument values", () => {
|
|
expect(substituteArgs("$ARGUMENTS", ["$1", "$ARGUMENTS"])).toBe("$1 $ARGUMENTS");
|
|
expect(substituteArgs("$@", ["$100", "$1"])).toBe("$100 $1");
|
|
expect(substituteArgs("$ARGUMENTS", ["$100", "$1"])).toBe("$100 $1");
|
|
});
|
|
|
|
test("should support mixed $1, $2, and $ARGUMENTS", () => {
|
|
expect(substituteArgs("$1: $ARGUMENTS", ["prefix", "a", "b"])).toBe("prefix: prefix a b");
|
|
});
|
|
|
|
test("should support mixed $1, $2, and $@", () => {
|
|
expect(substituteArgs("$1: $@", ["prefix", "a", "b"])).toBe("prefix: prefix a b");
|
|
});
|
|
|
|
test("should handle empty arguments array with $ARGUMENTS", () => {
|
|
expect(substituteArgs("Test: $ARGUMENTS", [])).toBe("Test: ");
|
|
});
|
|
|
|
test("should handle empty arguments array with $@", () => {
|
|
expect(substituteArgs("Test: $@", [])).toBe("Test: ");
|
|
});
|
|
|
|
test("should handle empty arguments array with $1", () => {
|
|
expect(substituteArgs("Test: $1", [])).toBe("Test: ");
|
|
});
|
|
|
|
test("should handle multiple occurrences of $ARGUMENTS", () => {
|
|
expect(substituteArgs("$ARGUMENTS and $ARGUMENTS", ["a", "b"])).toBe("a b and a b");
|
|
});
|
|
|
|
test("should handle multiple occurrences of $@", () => {
|
|
expect(substituteArgs("$@ and $@", ["a", "b"])).toBe("a b and a b");
|
|
});
|
|
|
|
test("should handle mixed occurrences of $@ and $ARGUMENTS", () => {
|
|
expect(substituteArgs("$@ and $ARGUMENTS", ["a", "b"])).toBe("a b and a b");
|
|
});
|
|
|
|
test("should handle special characters in arguments", () => {
|
|
// Note: $100 in argument doesn't get partially matched - full strings are substituted
|
|
expect(substituteArgs("$1 $2: $ARGUMENTS", ["arg100", "@user"])).toBe("arg100 @user: arg100 @user");
|
|
});
|
|
|
|
test("should handle out-of-range numbered placeholders", () => {
|
|
// Note: Out-of-range placeholders become empty strings (preserving spaces from template)
|
|
expect(substituteArgs("$1 $2 $3 $4 $5", ["a", "b"])).toBe("a b ");
|
|
});
|
|
|
|
test("should handle unicode characters", () => {
|
|
expect(substituteArgs("$ARGUMENTS", ["日本語", "🎉", "café"])).toBe("日本語 🎉 café");
|
|
});
|
|
|
|
test("should preserve newlines and tabs in argument values", () => {
|
|
expect(substituteArgs("$1 $2", ["line1\nline2", "tab\tthere"])).toBe("line1\nline2 tab\tthere");
|
|
});
|
|
|
|
test("should handle consecutive dollar patterns", () => {
|
|
expect(substituteArgs("$1$2", ["a", "b"])).toBe("ab");
|
|
});
|
|
|
|
test("should handle quoted arguments with spaces", () => {
|
|
expect(substituteArgs("$ARGUMENTS", ["first arg", "second arg"])).toBe("first arg second arg");
|
|
});
|
|
|
|
test("should handle single argument with $ARGUMENTS", () => {
|
|
expect(substituteArgs("Test: $ARGUMENTS", ["only"])).toBe("Test: only");
|
|
});
|
|
|
|
test("should handle single argument with $@", () => {
|
|
expect(substituteArgs("Test: $@", ["only"])).toBe("Test: only");
|
|
});
|
|
|
|
test("should handle $0 (zero index)", () => {
|
|
expect(substituteArgs("$0", ["a", "b"])).toBe("");
|
|
});
|
|
|
|
test("should handle decimal number in pattern (only integer part matches)", () => {
|
|
expect(substituteArgs("$1.5", ["a"])).toBe("a.5");
|
|
});
|
|
|
|
test("should handle $ARGUMENTS as part of word", () => {
|
|
expect(substituteArgs("pre$ARGUMENTS", ["a", "b"])).toBe("prea b");
|
|
});
|
|
|
|
test("should handle $@ as part of word", () => {
|
|
expect(substituteArgs("pre$@", ["a", "b"])).toBe("prea b");
|
|
});
|
|
|
|
test("should handle empty arguments in middle of list", () => {
|
|
expect(substituteArgs("$ARGUMENTS", ["a", "", "c"])).toBe("a c");
|
|
});
|
|
|
|
test("should handle trailing and leading spaces in arguments", () => {
|
|
expect(substituteArgs("$ARGUMENTS", [" leading ", "trailing "])).toBe(" leading trailing ");
|
|
});
|
|
|
|
test("should handle argument containing pattern partially", () => {
|
|
expect(substituteArgs("Prefix $ARGUMENTS suffix", ["ARGUMENTS"])).toBe("Prefix ARGUMENTS suffix");
|
|
});
|
|
|
|
test("should handle non-matching patterns", () => {
|
|
expect(substituteArgs("$A $$ $ $ARGS", ["a"])).toBe("$A $$ $ $ARGS");
|
|
});
|
|
|
|
test("should handle case variations (case-sensitive)", () => {
|
|
expect(substituteArgs("$arguments $Arguments $ARGUMENTS", ["a", "b"])).toBe("$arguments $Arguments a b");
|
|
});
|
|
|
|
test("should handle both syntaxes in same command with same result", () => {
|
|
const args = ["x", "y", "z"];
|
|
const result1 = substituteArgs("$@ and $ARGUMENTS", args);
|
|
const result2 = substituteArgs("$ARGUMENTS and $@", args);
|
|
expect(result1).toBe(result2);
|
|
expect(result1).toBe("x y z and x y z");
|
|
});
|
|
|
|
test("should handle very long argument lists", () => {
|
|
const args = Array.from({ length: 100 }, (_, i) => `arg${i}`);
|
|
const result = substituteArgs("$ARGUMENTS", args);
|
|
expect(result).toBe(args.join(" "));
|
|
});
|
|
|
|
test("should handle numbered placeholders with single digit", () => {
|
|
expect(substituteArgs("$1 $2 $3", ["a", "b", "c"])).toBe("a b c");
|
|
});
|
|
|
|
test("should handle numbered placeholders with multiple digits", () => {
|
|
const args = Array.from({ length: 15 }, (_, i) => `val${i}`);
|
|
expect(substituteArgs("$10 $12 $15", args)).toBe("val9 val11 val14");
|
|
});
|
|
|
|
test("should handle escaped dollar signs (literal backslash preserved)", () => {
|
|
// Note: No escape mechanism exists - backslash is treated literally
|
|
expect(substituteArgs("Price: \\$100", [])).toBe("Price: \\");
|
|
});
|
|
|
|
test("should handle mixed numbered and wildcard placeholders", () => {
|
|
expect(substituteArgs("$1: $@ ($ARGUMENTS)", ["first", "second", "third"])).toBe(
|
|
"first: first second third (first second third)",
|
|
);
|
|
});
|
|
|
|
test("should handle command with no placeholders", () => {
|
|
expect(substituteArgs("Just plain text", ["a", "b"])).toBe("Just plain text");
|
|
});
|
|
|
|
test("should handle command with only placeholders", () => {
|
|
expect(substituteArgs("$1 $2 $@", ["a", "b", "c"])).toBe("a b a b c");
|
|
});
|
|
});
|
|
|
|
// ============================================================================
|
|
// parseCommandArgs
|
|
// ============================================================================
|
|
|
|
describe("parseCommandArgs", () => {
|
|
test("should parse simple space-separated arguments", () => {
|
|
expect(parseCommandArgs("a b c")).toEqual(["a", "b", "c"]);
|
|
});
|
|
|
|
test("should parse quoted arguments with spaces", () => {
|
|
expect(parseCommandArgs('"first arg" second')).toEqual(["first arg", "second"]);
|
|
});
|
|
|
|
test("should parse single-quoted arguments", () => {
|
|
expect(parseCommandArgs("'first arg' second")).toEqual(["first arg", "second"]);
|
|
});
|
|
|
|
test("should parse mixed quote styles", () => {
|
|
expect(parseCommandArgs('"double" \'single\' "double again"')).toEqual(["double", "single", "double again"]);
|
|
});
|
|
|
|
test("should handle empty string", () => {
|
|
expect(parseCommandArgs("")).toEqual([]);
|
|
});
|
|
|
|
test("should handle extra spaces", () => {
|
|
expect(parseCommandArgs("a b c")).toEqual(["a", "b", "c"]);
|
|
});
|
|
|
|
test("should handle tabs as separators", () => {
|
|
expect(parseCommandArgs("a\tb\tc")).toEqual(["a", "b", "c"]);
|
|
});
|
|
|
|
test("should handle quoted empty string", () => {
|
|
// Note: Empty quotes are skipped by current implementation
|
|
expect(parseCommandArgs('"" " "')).toEqual([" "]);
|
|
});
|
|
|
|
test("should handle arguments with special characters", () => {
|
|
expect(parseCommandArgs("$100 @user #tag")).toEqual(["$100", "@user", "#tag"]);
|
|
});
|
|
|
|
test("should handle unicode characters", () => {
|
|
expect(parseCommandArgs("日本語 🎉 café")).toEqual(["日本語", "🎉", "café"]);
|
|
});
|
|
|
|
test("should handle newlines in arguments", () => {
|
|
expect(parseCommandArgs('"line1\nline2" second')).toEqual(["line1\nline2", "second"]);
|
|
});
|
|
|
|
test("should handle escaped quotes inside quoted strings", () => {
|
|
// Note: This implementation doesn't handle escaped quotes - backslash is literal
|
|
expect(parseCommandArgs('"quoted \\"text\\""')).toEqual(["quoted \\text\\"]);
|
|
});
|
|
|
|
test("should handle trailing spaces", () => {
|
|
expect(parseCommandArgs("a b c ")).toEqual(["a", "b", "c"]);
|
|
});
|
|
|
|
test("should handle leading spaces", () => {
|
|
expect(parseCommandArgs(" a b c")).toEqual(["a", "b", "c"]);
|
|
});
|
|
});
|
|
|
|
// ============================================================================
|
|
// Integration
|
|
// ============================================================================
|
|
|
|
describe("parseCommandArgs + substituteArgs integration", () => {
|
|
test("should parse and substitute together correctly", () => {
|
|
const input = 'Button "onClick handler" "disabled support"';
|
|
const args = parseCommandArgs(input);
|
|
const template = "Create component $1 with features: $ARGUMENTS";
|
|
const result = substituteArgs(template, args);
|
|
expect(result).toBe("Create component Button with features: Button onClick handler disabled support");
|
|
});
|
|
|
|
test("should handle the example from README", () => {
|
|
const input = 'Button "onClick handler" "disabled support"';
|
|
const args = parseCommandArgs(input);
|
|
const template = "Create a React component named $1 with features: $ARGUMENTS";
|
|
const result = substituteArgs(template, args);
|
|
expect(result).toBe(
|
|
"Create a React component named Button with features: Button onClick handler disabled support",
|
|
);
|
|
});
|
|
|
|
test("should produce same result with $@ and $ARGUMENTS", () => {
|
|
const args = parseCommandArgs("feature1 feature2 feature3");
|
|
const template1 = "Implement: $@";
|
|
const template2 = "Implement: $ARGUMENTS";
|
|
expect(substituteArgs(template1, args)).toBe(substituteArgs(template2, args));
|
|
});
|
|
});
|