Skip to content

Commit

Permalink
feat: add .after test chain
Browse files Browse the repository at this point in the history
  • Loading branch information
m2rads committed Dec 16, 2024
1 parent f539917 commit 23bb74c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/shortest/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ declare module '@antiwork/shortest' {
expect(description: string): TestChain;
expect(description: string, fn?: (context: TestContextProps) => Promise<void>): TestChain;
expect(description: string, payload?: any, fn?: (context: TestContextProps) => Promise<void>): TestChain;
after(fn: (context: TestContextProps) => void | Promise<void>): TestChain;
};

export type TestAPI = {
Expand Down
36 changes: 25 additions & 11 deletions packages/shortest/src/core/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,30 +194,44 @@ export class TestRunner {
].filter(Boolean).join('\n');

// Execute test with enhanced prompt
const result = await aiClient.processAction(prompt, browserTool, (content: Anthropic.Beta.Messages.BetaContentBlockParam) => {
if (content.type === 'text') {
// this.logger.reportStatus(`🤖 ${(content as Anthropic.Beta.Messages.BetaTextBlock).text}`);
}
});
const result = await aiClient.processAction(prompt, browserTool);

if (!result) {
throw new Error('AI processing failed: no result returned');
}

// Parse AI result first
const finalMessage = result.finalResponse.content.find(block =>
block.type === 'text' &&
(block as Anthropic.Beta.Messages.BetaTextBlock).text.includes('"result":')
);

if (finalMessage && finalMessage.type === 'text') {
const jsonMatch = (finalMessage as Anthropic.Beta.Messages.BetaTextBlock).text.match(/{[\s\S]*}/);
if (jsonMatch) {
const testResult = JSON.parse(jsonMatch[0]) as TestResult;
return testResult;
if (!finalMessage || finalMessage.type !== 'text') {
throw new Error('No test result found in AI response');
}

const jsonMatch = (finalMessage as Anthropic.Beta.Messages.BetaTextBlock).text.match(/{[\s\S]*}/);
if (!jsonMatch) {
throw new Error('Invalid test result format');
}

const aiResult = JSON.parse(jsonMatch[0]) as TestResult;

// Execute after function if present
if (test.afterFn) {
try {
await test.afterFn(testContext);
} catch (error) {
return {
result: 'fail' as const,
reason: aiResult.result === 'fail'
? `AI: ${aiResult.reason}, After: ${error instanceof Error ? error.message : String(error)}`
: error instanceof Error ? error.message : String(error)
};
}
}

throw new Error('No test result found in AI response');
return aiResult;
}

private async executeTestFile(file: string) {
Expand Down
7 changes: 7 additions & 0 deletions packages/shortest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ function createTestChain(
return {
expect: () => {
throw new Error('expect() cannot be called on direct execution test');
},
after: () => {
throw new Error('after() cannot be called on direct execution test');
}
};
}
Expand Down Expand Up @@ -167,6 +170,10 @@ function createTestChain(
fn: typeof payloadOrFn === 'function' ? payloadOrFn : fn
});
return chain;
},
after(fn: (context: TestContext) => void | Promise<void>) {
test.afterFn = (context) => Promise.resolve(fn(context));
return chain;
}
};

Expand Down
2 changes: 2 additions & 0 deletions packages/shortest/src/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ export interface TestFunction {
directExecution?: boolean;
}[];
directExecution?: boolean;
afterFn?: (context: TestContext) => void | Promise<void>;
}

export type TestChain = {
expect(fn: (context: TestContext) => Promise<void>): TestChain;
expect(description: string): TestChain;
expect(description: string, fn?: (context: TestContext) => Promise<void>): TestChain;
expect(description: string, payload?: any, fn?: (context: TestContext) => Promise<void>): TestChain;
after(fn: (context: TestContext) => void | Promise<void>): TestChain;
};

export type TestAPI = {
Expand Down
28 changes: 14 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 23bb74c

Please sign in to comment.