Skip to content

Commit

Permalink
Merge branch 'main' into feat/bash-tool
Browse files Browse the repository at this point in the history
  • Loading branch information
slavingia authored Jan 2, 2025
2 parents 201b867 + d5c61f9 commit 0b9a930
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ yarn shortest
export default {
headless: false,
baseUrl: 'http://localhost:3000',
testDir: 'app/__tests__',
testPattern: '**/*.test.ts',
anthropicKey: process.env.ANTHROPIC_API_KEY
} satisfies ShortestConfig;
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { shortest } from "@antiwork/shortest";

shortest("Verify that buttons are rounded on the landing page");
shortest(
"Login to the App using magic link. Use this email: 'mission-health@rdt7stzf.mailosaur.net'",
);
Expand Down
2 changes: 1 addition & 1 deletion license.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2024 Gumroad, Inc.
Copyright (c) 2025 Gumroad, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
2 changes: 1 addition & 1 deletion packages/shortest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type { ShortestConfig } from '@antiwork/shortest';
export default {
headless: false,
baseUrl: 'http://localhost:3000',
testDir: 'app/__tests__',
testPattern: "**/*.test.ts",
anthropicKey: process.env.ANTHROPIC_API_KEY
} satisfies ShortestConfig;
```
Expand Down
14 changes: 6 additions & 8 deletions packages/shortest/src/cli/bin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

import pc from "picocolors";
import { getConfig } from "..";
import { GitHubTool } from "../browser/integrations/github";
import { TestRunner } from "../core/runner";

Expand Down Expand Up @@ -132,7 +133,7 @@ async function main() {
const targetUrl = args
.find((arg) => arg.startsWith("--target="))
?.split("=")[1];
const testPattern = args.find((arg) => !arg.startsWith("--"));
const cliTestPattern = args.find((arg) => !arg.startsWith("--"));
const debugAI = args.includes("--debug-ai");
const noCache = args.includes("--no-cache");

Expand All @@ -146,12 +147,9 @@ async function main() {
noCache,
);
await runner.initialize();

if (testPattern) {
await runner.runFile(testPattern);
} else {
await runner.runAll();
}
const config = getConfig();
const testPattern = cliTestPattern || config.testPattern;
await runner.runTests(testPattern);
} catch (error) {
if (error instanceof Error) {
if (error.message.includes("Config")) {
Expand All @@ -164,7 +162,7 @@ async function main() {
);
console.error(pc.dim(" - headless: boolean"));
console.error(pc.dim(" - baseUrl: string"));
console.error(pc.dim(" - testDir: string | string[]"));
console.error(pc.dim(" - testPattern: string"));
console.error(pc.dim(" - anthropicKey: string"));
console.error();
} else {
Expand Down
58 changes: 10 additions & 48 deletions packages/shortest/src/core/runner/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { resolve } from "path";
import Anthropic from "@anthropic-ai/sdk";
import { glob } from "glob";
import pc from "picocolors";
Expand Down Expand Up @@ -83,37 +82,17 @@ export class TestRunner {
}

private async findTestFiles(pattern?: string): Promise<string[]> {
const testDirs = Array.isArray(this.config.testDir)
? this.config.testDir
: [this.config.testDir || "__tests__"];

const files = [];
for (const dir of testDirs) {
if (pattern) {
const cleanPattern = pattern
.replace(/\.ts$/, "")
.replace(/\.test$/, "")
.split("/")
.pop();

const globPattern = `${dir}/**/${cleanPattern}.test.ts`;
const matches = await glob(globPattern, {
cwd: this.cwd,
absolute: true,
});

files.push(...matches);
} else {
const globPattern = `${dir}/**/*.test.ts`;
const matches = await glob(globPattern, { cwd: this.cwd });
files.push(...matches.map((f) => resolve(this.cwd, f)));
}
}
const testPattern = pattern || this.config.testPattern || "**/*.test.ts";

const files = await glob(testPattern, {
cwd: this.cwd,
absolute: true,
});

if (files.length === 0) {
this.logger.error(
"Test Discovery",
`No test files found in directories: ${testDirs.join(", ")}`,
`No test files found matching: ${testPattern}`,
);
process.exit(1);
}
Expand Down Expand Up @@ -224,7 +203,7 @@ export class TestRunner {
}`,
),
]
: []),
: ["\nExpect:", `1. "${test.name}" expected to be successful`]),

"\nCurrent Page State:",
`URL: ${initialState.metadata?.window_info?.url || "unknown"}`,
Expand Down Expand Up @@ -386,14 +365,14 @@ export class TestRunner {
}
}

async runFile(pattern: string) {
async runTests(pattern?: string) {
await this.initialize();
const files = await this.findTestFiles(pattern);

if (files.length === 0) {
this.logger.error(
"Test Discovery",
`No test files found matching: ${pattern}`,
`No test files found matching the pattern: ${pattern || this.config.testPattern}`,
);
process.exit(1);
}
Expand All @@ -411,23 +390,6 @@ export class TestRunner {
}
}

async runAll() {
await this.initialize();
const files = await this.findTestFiles();

for (const file of files) {
await this.executeTestFile(file);
}

this.logger.summary();

if (this.exitOnSuccess && this.logger.allTestsPassed()) {
process.exit(0);
} else {
process.exit(1);
}
}

private async runCachedTest(
test: TestFunction,
browserTool: BrowserTool,
Expand Down
4 changes: 2 additions & 2 deletions packages/shortest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function validateConfig(config: Partial<ShortestConfig>) {

if (config.headless === undefined) missingFields.push("headless");
if (!config.baseUrl) missingFields.push("baseUrl");
if (!config.testDir) missingFields.push("testDir");
if (!config.testPattern) missingFields.push("testPattern");
if (!config.anthropicKey && !process.env.ANTHROPIC_API_KEY)
missingFields.push("anthropicKey");

Expand Down Expand Up @@ -98,7 +98,7 @@ export async function initialize() {
"Required fields:\n" +
" - headless: boolean\n" +
" - baseUrl: string\n" +
" - testDir: string | string[]\n" +
" - testPattern: string\n" +
" - anthropicKey: string",
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/shortest/src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface ShortestConfig {
headless: boolean;
baseUrl: string;
testDir: string | string[];
testPattern: string;
anthropicKey: string;
mailosaur?: {
apiKey: string;
Expand Down
2 changes: 1 addition & 1 deletion shortest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ShortestConfig } from "@antiwork/shortest";
export default {
headless: false,
baseUrl: "http://localhost:3000",
testDir: ["app/__tests__", "examples"],
testPattern: "**/*.test.ts",
anthropicKey: process.env.ANTHROPIC_API_KEY,
mailosaur: {
apiKey: process.env.MAILOSAUR_API_KEY,
Expand Down

0 comments on commit 0b9a930

Please sign in to comment.