Files
msd-core/sdk/src/prompt-sanitizer.test.ts
TÂCHES 89f95c43ba feat: auto --init flag, headless prompts, and prompt sanitizer (#1417)
* fix: Created 10 headless prompt files (5 workflows + 5 agents) in sdk/p…

- "sdk/prompts/workflows/execute-plan.md"
- "sdk/prompts/workflows/research-phase.md"
- "sdk/prompts/workflows/plan-phase.md"
- "sdk/prompts/workflows/verify-phase.md"
- "sdk/prompts/workflows/discuss-phase.md"
- "sdk/prompts/agents/gsd-executor.md"
- "sdk/prompts/agents/gsd-phase-researcher.md"
- "sdk/prompts/agents/gsd-planner.md"

GSD-Task: S01/T02

* feat: Created prompt-sanitizer.ts, wired headless prompt loading into P…

- "sdk/src/prompt-sanitizer.ts"
- "sdk/src/phase-prompt.ts"
- "sdk/src/gsd-tools.ts"
- "sdk/src/gsd-tools.test.ts"
- "sdk/src/phase-runner-types.test.ts"

GSD-Task: S01/T01

* test: Added 111 unit tests covering sanitizePrompt(), headless prompt l…

- "sdk/src/prompt-sanitizer.test.ts"
- "sdk/src/headless-prompts.test.ts"
- "sdk/src/phase-prompt.test.ts"

GSD-Task: S01/T03

* feat: Wired sdkPromptsDir preference and sanitizePrompt into InitRunner…

- "sdk/src/init-runner.ts"
- "sdk/package.json"

GSD-Task: S02/T01

* feat: add --init flag to auto command for single-command PRD-to-execution

gsd-sdk auto --init @path/to/prd.md now bootstraps the project (init)
then immediately runs the autonomous phase execution loop.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* chore: add remaining headless prompt files and templates

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* test: Extended init-runner.test.ts with 7 sdkPromptsDir preference and…

- "sdk/src/init-runner.test.ts"

GSD-Task: S02/T03

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 22:40:34 -06:00

243 lines
9.1 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { sanitizePrompt } from './prompt-sanitizer.js';
describe('sanitizePrompt', () => {
// ─── Edge cases ──────────────────────────────────────────────────────────
describe('edge cases', () => {
it('returns empty string unchanged', () => {
expect(sanitizePrompt('')).toBe('');
});
it('returns undefined/null-ish input unchanged', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect(sanitizePrompt(undefined as any)).toBeUndefined();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect(sanitizePrompt(null as any)).toBeNull();
});
it('preserves clean content with no patterns', () => {
const clean = 'This is a clean prompt.\nIt has no interactive patterns.\n\nJust normal text.';
expect(sanitizePrompt(clean)).toBe(clean.trim());
});
});
// ─── @file: references ───────────────────────────────────────────────────
describe('@file: references', () => {
it('strips lines containing @file: references', () => {
const input = 'Before\nLoad @file:path/to/context.md for context\nAfter';
const result = sanitizePrompt(input);
expect(result).not.toContain('@file:');
expect(result).toContain('Before');
expect(result).toContain('After');
});
it('strips @file: with various path formats', () => {
const input = [
'@file:simple.md',
'@file:./relative/path.md',
'@file:/absolute/path/to/file.md',
'@file:~/.claude/get-shit-done/workflows/execute-plan.md',
].join('\n');
expect(sanitizePrompt(input)).toBe('');
});
});
// ─── /gsd: slash commands ────────────────────────────────────────────────
describe('/gsd: slash commands', () => {
it('strips lines containing /gsd: commands', () => {
const input = 'Before\nRun /gsd:execute-plan to proceed\nAfter';
const result = sanitizePrompt(input);
expect(result).not.toContain('/gsd:');
expect(result).toContain('Before');
expect(result).toContain('After');
});
it('strips various /gsd: command formats', () => {
const input = [
'Use /gsd:research-phase',
'Then /gsd:plan-phase --auto',
'Finally /gsd:verify-phase',
].join('\n');
expect(sanitizePrompt(input)).toBe('');
});
});
// ─── AskUserQuestion() calls ─────────────────────────────────────────────
describe('AskUserQuestion() calls', () => {
it('strips AskUserQuestion lines', () => {
const input = 'Before\nAskUserQuestion("What should we do?")\nAfter';
const result = sanitizePrompt(input);
expect(result).not.toContain('AskUserQuestion');
expect(result).toContain('Before');
expect(result).toContain('After');
});
it('strips AskUserQuestion with various argument styles', () => {
const input = [
'AskUserQuestion("simple")',
'AskUserQuestion( "with spaces" )',
' AskUserQuestion("indented")',
'Use AskUserQuestion("inline") here',
].join('\n');
expect(sanitizePrompt(input)).toBe('');
});
});
// ─── SlashCommand() calls ────────────────────────────────────────────────
describe('SlashCommand() calls', () => {
it('strips SlashCommand lines', () => {
const input = 'Before\nSlashCommand("/gsd:execute")\nAfter';
const result = sanitizePrompt(input);
expect(result).not.toContain('SlashCommand');
expect(result).toContain('Before');
expect(result).toContain('After');
});
it('strips SlashCommand with various forms', () => {
const input = [
'SlashCommand("proceed")',
'SlashCommand( "next" )',
' SlashCommand("indented")',
].join('\n');
expect(sanitizePrompt(input)).toBe('');
});
});
// ─── STOP directives ────────────────────────────────────────────────────
describe('STOP directives', () => {
it('strips "STOP and wait" lines', () => {
const input = 'Before\nSTOP and wait for user input\nAfter';
const result = sanitizePrompt(input);
expect(result).not.toContain('STOP');
expect(result).toContain('Before');
expect(result).toContain('After');
});
it('strips bare STOP lines', () => {
const input = 'Before\nSTOP\nAfter';
const result = sanitizePrompt(input);
expect(result).not.toContain('STOP');
});
it('strips STOP with trailing punctuation', () => {
const input = 'Before\nSTOP.\nAfter';
const result = sanitizePrompt(input);
expect(result).not.toContain('STOP');
});
it('strips "STOP here" and "STOP now"', () => {
const input = 'STOP here\nSTOP now\nSTOP and ask';
const result = sanitizePrompt(input);
expect(result).toBe('');
});
it('preserves STOP in normal prose (not as directive)', () => {
const input = 'Do not stop the build process.';
const result = sanitizePrompt(input);
// "stop" in lowercase in normal prose should be preserved
expect(result).toContain('stop the build');
});
});
// ─── 'wait for user' / 'ask the user' instructions ──────────────────────
describe('wait for user / ask the user', () => {
it('strips "wait for user" lines', () => {
const input = 'Before\nWait for user confirmation before proceeding\nAfter';
const result = sanitizePrompt(input);
expect(result).not.toMatch(/wait for.*user/i);
expect(result).toContain('Before');
expect(result).toContain('After');
});
it('strips "wait for the user" lines', () => {
const input = 'Before\nWait for the user to respond\nAfter';
const result = sanitizePrompt(input);
expect(result).not.toMatch(/wait for the user/i);
});
it('strips "ask the user" lines', () => {
const input = 'Before\nAsk the user for clarification\nAfter';
const result = sanitizePrompt(input);
expect(result).not.toMatch(/ask the user/i);
expect(result).toContain('Before');
expect(result).toContain('After');
});
it('is case-insensitive for wait/ask patterns', () => {
const input = [
'WAIT FOR USER input',
'wait for user approval',
'ASK THE USER what to do',
'ask the user for feedback',
].join('\n');
expect(sanitizePrompt(input)).toBe('');
});
});
// ─── Multiple patterns in one string ─────────────────────────────────────
describe('multiple patterns in one string', () => {
it('strips all pattern types from a mixed prompt', () => {
const input = [
'## Research Phase',
'',
'Investigate the codebase using @file:context.md for context.',
'',
'When done, run /gsd:plan-phase to proceed.',
'',
'If unclear, AskUserQuestion("What should I focus on?")',
'',
'STOP and wait for user input.',
'',
'Use SlashCommand("next") to continue.',
'',
'Wait for user confirmation before executing.',
'',
'This line is clean and should remain.',
].join('\n');
const result = sanitizePrompt(input);
expect(result).not.toContain('@file:');
expect(result).not.toContain('/gsd:');
expect(result).not.toContain('AskUserQuestion');
expect(result).not.toContain('SlashCommand');
expect(result).not.toMatch(/\bSTOP\b/);
expect(result).not.toMatch(/wait for user/i);
expect(result).toContain('## Research Phase');
expect(result).toContain('This line is clean and should remain.');
});
});
// ─── Blank line collapsing ───────────────────────────────────────────────
describe('blank line collapsing', () => {
it('collapses 3+ consecutive blank lines to 2', () => {
const input = 'Line 1\n\n\n\n\nLine 2';
const result = sanitizePrompt(input);
// After trim(), the result should have at most 2 consecutive newlines
expect(result).not.toMatch(/\n{3,}/);
expect(result).toContain('Line 1');
expect(result).toContain('Line 2');
});
it('collapses blanks left by stripped lines', () => {
const input = [
'Before',
'',
'AskUserQuestion("something")',
'',
'After',
].join('\n');
const result = sanitizePrompt(input);
expect(result).toBe('Before\n\nAfter');
});
});
});