@hasna/testers 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +196 -0
  3. package/dashboard/dist/assets/index-CDcHt94n.css +1 -0
  4. package/dashboard/dist/assets/index-DCNDCh61.js +49 -0
  5. package/dashboard/dist/index.html +13 -0
  6. package/dist/cli/index.d.ts +3 -0
  7. package/dist/cli/index.d.ts.map +1 -0
  8. package/dist/cli/index.js +4112 -0
  9. package/dist/db/agents.d.ts +10 -0
  10. package/dist/db/agents.d.ts.map +1 -0
  11. package/dist/db/database.d.ts +10 -0
  12. package/dist/db/database.d.ts.map +1 -0
  13. package/dist/db/projects.d.ts +11 -0
  14. package/dist/db/projects.d.ts.map +1 -0
  15. package/dist/db/results.d.ts +20 -0
  16. package/dist/db/results.d.ts.map +1 -0
  17. package/dist/db/runs.d.ts +9 -0
  18. package/dist/db/runs.d.ts.map +1 -0
  19. package/dist/db/scenarios.d.ts +8 -0
  20. package/dist/db/scenarios.d.ts.map +1 -0
  21. package/dist/db/screenshots.d.ts +13 -0
  22. package/dist/db/screenshots.d.ts.map +1 -0
  23. package/dist/index.d.ts +18 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +2515 -0
  26. package/dist/lib/ai-client.d.ts +66 -0
  27. package/dist/lib/ai-client.d.ts.map +1 -0
  28. package/dist/lib/browser.d.ts +64 -0
  29. package/dist/lib/browser.d.ts.map +1 -0
  30. package/dist/lib/config.d.ts +18 -0
  31. package/dist/lib/config.d.ts.map +1 -0
  32. package/dist/lib/reporter.d.ts +18 -0
  33. package/dist/lib/reporter.d.ts.map +1 -0
  34. package/dist/lib/runner.d.ts +36 -0
  35. package/dist/lib/runner.d.ts.map +1 -0
  36. package/dist/lib/screenshotter.d.ts +60 -0
  37. package/dist/lib/screenshotter.d.ts.map +1 -0
  38. package/dist/lib/todos-connector.d.ts +32 -0
  39. package/dist/lib/todos-connector.d.ts.map +1 -0
  40. package/dist/mcp/index.d.ts +3 -0
  41. package/dist/mcp/index.d.ts.map +1 -0
  42. package/dist/mcp/index.js +5903 -0
  43. package/dist/server/index.d.ts +3 -0
  44. package/dist/server/index.d.ts.map +1 -0
  45. package/dist/server/index.js +1654 -0
  46. package/dist/types/index.d.ts +276 -0
  47. package/dist/types/index.d.ts.map +1 -0
  48. package/package.json +78 -0
@@ -0,0 +1,1654 @@
1
+ #!/usr/bin/env bun
2
+ // @bun
3
+
4
+ // src/server/index.ts
5
+ import { existsSync as existsSync4 } from "fs";
6
+ import { join as join4 } from "path";
7
+ import { homedir as homedir4 } from "os";
8
+
9
+ // src/types/index.ts
10
+ var MODEL_MAP = {
11
+ quick: "claude-haiku-4-5-20251001",
12
+ thorough: "claude-sonnet-4-6-20260311",
13
+ deep: "claude-opus-4-6-20260311"
14
+ };
15
+ function scenarioFromRow(row) {
16
+ return {
17
+ id: row.id,
18
+ shortId: row.short_id,
19
+ projectId: row.project_id,
20
+ name: row.name,
21
+ description: row.description,
22
+ steps: JSON.parse(row.steps),
23
+ tags: JSON.parse(row.tags),
24
+ priority: row.priority,
25
+ model: row.model,
26
+ timeoutMs: row.timeout_ms,
27
+ targetPath: row.target_path,
28
+ requiresAuth: row.requires_auth === 1,
29
+ authConfig: row.auth_config ? JSON.parse(row.auth_config) : null,
30
+ metadata: row.metadata ? JSON.parse(row.metadata) : null,
31
+ version: row.version,
32
+ createdAt: row.created_at,
33
+ updatedAt: row.updated_at
34
+ };
35
+ }
36
+ function runFromRow(row) {
37
+ return {
38
+ id: row.id,
39
+ projectId: row.project_id,
40
+ status: row.status,
41
+ url: row.url,
42
+ model: row.model,
43
+ headed: row.headed === 1,
44
+ parallel: row.parallel,
45
+ total: row.total,
46
+ passed: row.passed,
47
+ failed: row.failed,
48
+ startedAt: row.started_at,
49
+ finishedAt: row.finished_at,
50
+ metadata: row.metadata ? JSON.parse(row.metadata) : null
51
+ };
52
+ }
53
+ function resultFromRow(row) {
54
+ return {
55
+ id: row.id,
56
+ runId: row.run_id,
57
+ scenarioId: row.scenario_id,
58
+ status: row.status,
59
+ reasoning: row.reasoning,
60
+ error: row.error,
61
+ stepsCompleted: row.steps_completed,
62
+ stepsTotal: row.steps_total,
63
+ durationMs: row.duration_ms,
64
+ model: row.model,
65
+ tokensUsed: row.tokens_used,
66
+ costCents: row.cost_cents,
67
+ metadata: row.metadata ? JSON.parse(row.metadata) : null,
68
+ createdAt: row.created_at
69
+ };
70
+ }
71
+ function screenshotFromRow(row) {
72
+ return {
73
+ id: row.id,
74
+ resultId: row.result_id,
75
+ stepNumber: row.step_number,
76
+ action: row.action,
77
+ filePath: row.file_path,
78
+ width: row.width,
79
+ height: row.height,
80
+ timestamp: row.timestamp
81
+ };
82
+ }
83
+ class VersionConflictError extends Error {
84
+ constructor(entity, id) {
85
+ super(`Version conflict on ${entity}: ${id}`);
86
+ this.name = "VersionConflictError";
87
+ }
88
+ }
89
+
90
+ class BrowserError extends Error {
91
+ constructor(message) {
92
+ super(message);
93
+ this.name = "BrowserError";
94
+ }
95
+ }
96
+
97
+ class AIClientError extends Error {
98
+ constructor(message) {
99
+ super(message);
100
+ this.name = "AIClientError";
101
+ }
102
+ }
103
+
104
+ // src/db/database.ts
105
+ import { Database } from "bun:sqlite";
106
+ import { mkdirSync, existsSync } from "fs";
107
+ import { dirname, join } from "path";
108
+ import { homedir } from "os";
109
+ var db = null;
110
+ function now() {
111
+ return new Date().toISOString();
112
+ }
113
+ function uuid() {
114
+ return crypto.randomUUID();
115
+ }
116
+ function shortUuid() {
117
+ return uuid().slice(0, 8);
118
+ }
119
+ function resolveDbPath() {
120
+ const envPath = process.env["TESTERS_DB_PATH"];
121
+ if (envPath)
122
+ return envPath;
123
+ const dir = join(homedir(), ".testers");
124
+ if (!existsSync(dir))
125
+ mkdirSync(dir, { recursive: true });
126
+ return join(dir, "testers.db");
127
+ }
128
+ var MIGRATIONS = [
129
+ `
130
+ CREATE TABLE IF NOT EXISTS projects (
131
+ id TEXT PRIMARY KEY,
132
+ name TEXT NOT NULL UNIQUE,
133
+ path TEXT UNIQUE,
134
+ description TEXT,
135
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
136
+ updated_at TEXT NOT NULL DEFAULT (datetime('now'))
137
+ );
138
+
139
+ CREATE TABLE IF NOT EXISTS agents (
140
+ id TEXT PRIMARY KEY,
141
+ name TEXT NOT NULL UNIQUE,
142
+ description TEXT,
143
+ role TEXT,
144
+ metadata TEXT DEFAULT '{}',
145
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
146
+ last_seen_at TEXT NOT NULL DEFAULT (datetime('now'))
147
+ );
148
+
149
+ CREATE TABLE IF NOT EXISTS scenarios (
150
+ id TEXT PRIMARY KEY,
151
+ short_id TEXT NOT NULL UNIQUE,
152
+ project_id TEXT REFERENCES projects(id) ON DELETE SET NULL,
153
+ name TEXT NOT NULL,
154
+ description TEXT NOT NULL DEFAULT '',
155
+ steps TEXT NOT NULL DEFAULT '[]',
156
+ tags TEXT NOT NULL DEFAULT '[]',
157
+ priority TEXT NOT NULL DEFAULT 'medium' CHECK(priority IN ('low','medium','high','critical')),
158
+ model TEXT,
159
+ timeout_ms INTEGER,
160
+ target_path TEXT,
161
+ requires_auth INTEGER NOT NULL DEFAULT 0,
162
+ auth_config TEXT,
163
+ metadata TEXT DEFAULT '{}',
164
+ version INTEGER NOT NULL DEFAULT 1,
165
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
166
+ updated_at TEXT NOT NULL DEFAULT (datetime('now'))
167
+ );
168
+
169
+ CREATE TABLE IF NOT EXISTS runs (
170
+ id TEXT PRIMARY KEY,
171
+ project_id TEXT REFERENCES projects(id) ON DELETE SET NULL,
172
+ status TEXT NOT NULL DEFAULT 'pending' CHECK(status IN ('pending','running','passed','failed','cancelled')),
173
+ url TEXT NOT NULL,
174
+ model TEXT NOT NULL,
175
+ headed INTEGER NOT NULL DEFAULT 0,
176
+ parallel INTEGER NOT NULL DEFAULT 1,
177
+ total INTEGER NOT NULL DEFAULT 0,
178
+ passed INTEGER NOT NULL DEFAULT 0,
179
+ failed INTEGER NOT NULL DEFAULT 0,
180
+ started_at TEXT NOT NULL DEFAULT (datetime('now')),
181
+ finished_at TEXT,
182
+ metadata TEXT DEFAULT '{}'
183
+ );
184
+
185
+ CREATE TABLE IF NOT EXISTS results (
186
+ id TEXT PRIMARY KEY,
187
+ run_id TEXT NOT NULL REFERENCES runs(id) ON DELETE CASCADE,
188
+ scenario_id TEXT NOT NULL REFERENCES scenarios(id) ON DELETE CASCADE,
189
+ status TEXT NOT NULL DEFAULT 'skipped' CHECK(status IN ('passed','failed','error','skipped')),
190
+ reasoning TEXT,
191
+ error TEXT,
192
+ steps_completed INTEGER NOT NULL DEFAULT 0,
193
+ steps_total INTEGER NOT NULL DEFAULT 0,
194
+ duration_ms INTEGER NOT NULL DEFAULT 0,
195
+ model TEXT NOT NULL,
196
+ tokens_used INTEGER NOT NULL DEFAULT 0,
197
+ cost_cents REAL NOT NULL DEFAULT 0,
198
+ metadata TEXT DEFAULT '{}',
199
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
200
+ );
201
+
202
+ CREATE TABLE IF NOT EXISTS screenshots (
203
+ id TEXT PRIMARY KEY,
204
+ result_id TEXT NOT NULL REFERENCES results(id) ON DELETE CASCADE,
205
+ step_number INTEGER NOT NULL,
206
+ action TEXT NOT NULL,
207
+ file_path TEXT NOT NULL,
208
+ width INTEGER NOT NULL DEFAULT 0,
209
+ height INTEGER NOT NULL DEFAULT 0,
210
+ timestamp TEXT NOT NULL DEFAULT (datetime('now'))
211
+ );
212
+
213
+ CREATE TABLE IF NOT EXISTS _migrations (
214
+ id INTEGER PRIMARY KEY,
215
+ applied_at TEXT NOT NULL DEFAULT (datetime('now'))
216
+ );
217
+ `,
218
+ `
219
+ CREATE INDEX IF NOT EXISTS idx_scenarios_project ON scenarios(project_id);
220
+ CREATE INDEX IF NOT EXISTS idx_scenarios_priority ON scenarios(priority);
221
+ CREATE INDEX IF NOT EXISTS idx_scenarios_short_id ON scenarios(short_id);
222
+ CREATE INDEX IF NOT EXISTS idx_runs_project ON runs(project_id);
223
+ CREATE INDEX IF NOT EXISTS idx_runs_status ON runs(status);
224
+ CREATE INDEX IF NOT EXISTS idx_results_run ON results(run_id);
225
+ CREATE INDEX IF NOT EXISTS idx_results_scenario ON results(scenario_id);
226
+ CREATE INDEX IF NOT EXISTS idx_results_status ON results(status);
227
+ CREATE INDEX IF NOT EXISTS idx_screenshots_result ON screenshots(result_id);
228
+ `,
229
+ `
230
+ ALTER TABLE projects ADD COLUMN scenario_prefix TEXT DEFAULT 'TST';
231
+ ALTER TABLE projects ADD COLUMN scenario_counter INTEGER DEFAULT 0;
232
+ `
233
+ ];
234
+ function applyMigrations(database) {
235
+ const applied = database.query("SELECT id FROM _migrations ORDER BY id").all();
236
+ const appliedIds = new Set(applied.map((r) => r.id));
237
+ for (let i = 0;i < MIGRATIONS.length; i++) {
238
+ const migrationId = i + 1;
239
+ if (appliedIds.has(migrationId))
240
+ continue;
241
+ const migration = MIGRATIONS[i];
242
+ database.exec(migration);
243
+ database.query("INSERT INTO _migrations (id, applied_at) VALUES (?, ?)").run(migrationId, now());
244
+ }
245
+ }
246
+ function getDatabase() {
247
+ if (db)
248
+ return db;
249
+ const dbPath = resolveDbPath();
250
+ const dir = dirname(dbPath);
251
+ if (dbPath !== ":memory:" && !existsSync(dir)) {
252
+ mkdirSync(dir, { recursive: true });
253
+ }
254
+ db = new Database(dbPath);
255
+ db.exec("PRAGMA journal_mode = WAL");
256
+ db.exec("PRAGMA foreign_keys = ON");
257
+ db.exec("PRAGMA busy_timeout = 5000");
258
+ db.exec(`
259
+ CREATE TABLE IF NOT EXISTS _migrations (
260
+ id INTEGER PRIMARY KEY,
261
+ applied_at TEXT NOT NULL DEFAULT (datetime('now'))
262
+ );
263
+ `);
264
+ applyMigrations(db);
265
+ return db;
266
+ }
267
+ function resolvePartialId(table, partialId) {
268
+ const database = getDatabase();
269
+ const rows = database.query(`SELECT id FROM ${table} WHERE id LIKE ? || '%'`).all(partialId);
270
+ if (rows.length === 1)
271
+ return rows[0].id;
272
+ return null;
273
+ }
274
+
275
+ // src/db/scenarios.ts
276
+ function nextShortId(projectId) {
277
+ const db2 = getDatabase();
278
+ if (projectId) {
279
+ const project = db2.query("SELECT scenario_prefix, scenario_counter FROM projects WHERE id = ?").get(projectId);
280
+ if (project) {
281
+ const next = project.scenario_counter + 1;
282
+ db2.query("UPDATE projects SET scenario_counter = ? WHERE id = ?").run(next, projectId);
283
+ return `${project.scenario_prefix}-${next}`;
284
+ }
285
+ }
286
+ return shortUuid();
287
+ }
288
+ function createScenario(input) {
289
+ const db2 = getDatabase();
290
+ const id = uuid();
291
+ const short_id = nextShortId(input.projectId);
292
+ const timestamp = now();
293
+ db2.query(`
294
+ INSERT INTO scenarios (id, short_id, project_id, name, description, steps, tags, priority, model, timeout_ms, target_path, requires_auth, auth_config, metadata, version, created_at, updated_at)
295
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?)
296
+ `).run(id, short_id, input.projectId ?? null, input.name, input.description, JSON.stringify(input.steps ?? []), JSON.stringify(input.tags ?? []), input.priority ?? "medium", input.model ?? null, input.timeoutMs ?? null, input.targetPath ?? null, input.requiresAuth ? 1 : 0, input.authConfig ? JSON.stringify(input.authConfig) : null, input.metadata ? JSON.stringify(input.metadata) : null, timestamp, timestamp);
297
+ return getScenario(id);
298
+ }
299
+ function getScenario(id) {
300
+ const db2 = getDatabase();
301
+ let row = db2.query("SELECT * FROM scenarios WHERE id = ?").get(id);
302
+ if (row)
303
+ return scenarioFromRow(row);
304
+ row = db2.query("SELECT * FROM scenarios WHERE short_id = ?").get(id);
305
+ if (row)
306
+ return scenarioFromRow(row);
307
+ const fullId = resolvePartialId("scenarios", id);
308
+ if (fullId) {
309
+ row = db2.query("SELECT * FROM scenarios WHERE id = ?").get(fullId);
310
+ if (row)
311
+ return scenarioFromRow(row);
312
+ }
313
+ return null;
314
+ }
315
+ function getScenarioByShortId(shortId) {
316
+ const db2 = getDatabase();
317
+ const row = db2.query("SELECT * FROM scenarios WHERE short_id = ?").get(shortId);
318
+ return row ? scenarioFromRow(row) : null;
319
+ }
320
+ function listScenarios(filter) {
321
+ const db2 = getDatabase();
322
+ const conditions = [];
323
+ const params = [];
324
+ if (filter?.projectId) {
325
+ conditions.push("project_id = ?");
326
+ params.push(filter.projectId);
327
+ }
328
+ if (filter?.tags && filter.tags.length > 0) {
329
+ for (const tag of filter.tags) {
330
+ conditions.push("tags LIKE ?");
331
+ params.push(`%"${tag}"%`);
332
+ }
333
+ }
334
+ if (filter?.priority) {
335
+ conditions.push("priority = ?");
336
+ params.push(filter.priority);
337
+ }
338
+ if (filter?.search) {
339
+ conditions.push("(name LIKE ? OR description LIKE ?)");
340
+ const term = `%${filter.search}%`;
341
+ params.push(term, term);
342
+ }
343
+ let sql = "SELECT * FROM scenarios";
344
+ if (conditions.length > 0) {
345
+ sql += " WHERE " + conditions.join(" AND ");
346
+ }
347
+ sql += " ORDER BY created_at DESC";
348
+ if (filter?.limit) {
349
+ sql += " LIMIT ?";
350
+ params.push(filter.limit);
351
+ }
352
+ if (filter?.offset) {
353
+ sql += " OFFSET ?";
354
+ params.push(filter.offset);
355
+ }
356
+ const rows = db2.query(sql).all(...params);
357
+ return rows.map(scenarioFromRow);
358
+ }
359
+ function updateScenario(id, input, version) {
360
+ const db2 = getDatabase();
361
+ const existing = getScenario(id);
362
+ if (!existing) {
363
+ throw new Error(`Scenario not found: ${id}`);
364
+ }
365
+ if (existing.version !== version) {
366
+ throw new VersionConflictError("scenario", existing.id);
367
+ }
368
+ const sets = [];
369
+ const params = [];
370
+ if (input.name !== undefined) {
371
+ sets.push("name = ?");
372
+ params.push(input.name);
373
+ }
374
+ if (input.description !== undefined) {
375
+ sets.push("description = ?");
376
+ params.push(input.description);
377
+ }
378
+ if (input.steps !== undefined) {
379
+ sets.push("steps = ?");
380
+ params.push(JSON.stringify(input.steps));
381
+ }
382
+ if (input.tags !== undefined) {
383
+ sets.push("tags = ?");
384
+ params.push(JSON.stringify(input.tags));
385
+ }
386
+ if (input.priority !== undefined) {
387
+ sets.push("priority = ?");
388
+ params.push(input.priority);
389
+ }
390
+ if (input.model !== undefined) {
391
+ sets.push("model = ?");
392
+ params.push(input.model);
393
+ }
394
+ if (input.timeoutMs !== undefined) {
395
+ sets.push("timeout_ms = ?");
396
+ params.push(input.timeoutMs);
397
+ }
398
+ if (input.targetPath !== undefined) {
399
+ sets.push("target_path = ?");
400
+ params.push(input.targetPath);
401
+ }
402
+ if (input.requiresAuth !== undefined) {
403
+ sets.push("requires_auth = ?");
404
+ params.push(input.requiresAuth ? 1 : 0);
405
+ }
406
+ if (input.authConfig !== undefined) {
407
+ sets.push("auth_config = ?");
408
+ params.push(JSON.stringify(input.authConfig));
409
+ }
410
+ if (input.metadata !== undefined) {
411
+ sets.push("metadata = ?");
412
+ params.push(JSON.stringify(input.metadata));
413
+ }
414
+ if (sets.length === 0) {
415
+ return existing;
416
+ }
417
+ sets.push("version = ?");
418
+ params.push(version + 1);
419
+ sets.push("updated_at = ?");
420
+ params.push(now());
421
+ params.push(existing.id);
422
+ params.push(version);
423
+ const result = db2.query(`UPDATE scenarios SET ${sets.join(", ")} WHERE id = ? AND version = ?`).run(...params);
424
+ if (result.changes === 0) {
425
+ throw new VersionConflictError("scenario", existing.id);
426
+ }
427
+ return getScenario(existing.id);
428
+ }
429
+ function deleteScenario(id) {
430
+ const db2 = getDatabase();
431
+ const scenario = getScenario(id);
432
+ if (!scenario)
433
+ return false;
434
+ const result = db2.query("DELETE FROM scenarios WHERE id = ?").run(scenario.id);
435
+ return result.changes > 0;
436
+ }
437
+
438
+ // src/db/runs.ts
439
+ function createRun(input) {
440
+ const db2 = getDatabase();
441
+ const id = uuid();
442
+ const timestamp = now();
443
+ db2.query(`
444
+ INSERT INTO runs (id, project_id, status, url, model, headed, parallel, total, passed, failed, started_at, finished_at, metadata)
445
+ VALUES (?, ?, 'pending', ?, ?, ?, ?, 0, 0, 0, ?, NULL, ?)
446
+ `).run(id, input.projectId ?? null, input.url, input.model, input.headed ? 1 : 0, input.parallel ?? 1, timestamp, input.model ? JSON.stringify({}) : null);
447
+ return getRun(id);
448
+ }
449
+ function getRun(id) {
450
+ const db2 = getDatabase();
451
+ let row = db2.query("SELECT * FROM runs WHERE id = ?").get(id);
452
+ if (row)
453
+ return runFromRow(row);
454
+ const fullId = resolvePartialId("runs", id);
455
+ if (fullId) {
456
+ row = db2.query("SELECT * FROM runs WHERE id = ?").get(fullId);
457
+ if (row)
458
+ return runFromRow(row);
459
+ }
460
+ return null;
461
+ }
462
+ function listRuns(filter) {
463
+ const db2 = getDatabase();
464
+ const conditions = [];
465
+ const params = [];
466
+ if (filter?.projectId) {
467
+ conditions.push("project_id = ?");
468
+ params.push(filter.projectId);
469
+ }
470
+ if (filter?.status) {
471
+ conditions.push("status = ?");
472
+ params.push(filter.status);
473
+ }
474
+ let sql = "SELECT * FROM runs";
475
+ if (conditions.length > 0) {
476
+ sql += " WHERE " + conditions.join(" AND ");
477
+ }
478
+ sql += " ORDER BY started_at DESC";
479
+ if (filter?.limit) {
480
+ sql += " LIMIT ?";
481
+ params.push(filter.limit);
482
+ }
483
+ if (filter?.offset) {
484
+ sql += " OFFSET ?";
485
+ params.push(filter.offset);
486
+ }
487
+ const rows = db2.query(sql).all(...params);
488
+ return rows.map(runFromRow);
489
+ }
490
+ function updateRun(id, updates) {
491
+ const db2 = getDatabase();
492
+ const existing = getRun(id);
493
+ if (!existing) {
494
+ throw new Error(`Run not found: ${id}`);
495
+ }
496
+ const sets = [];
497
+ const params = [];
498
+ if (updates.status !== undefined) {
499
+ sets.push("status = ?");
500
+ params.push(updates.status);
501
+ }
502
+ if (updates.url !== undefined) {
503
+ sets.push("url = ?");
504
+ params.push(updates.url);
505
+ }
506
+ if (updates.model !== undefined) {
507
+ sets.push("model = ?");
508
+ params.push(updates.model);
509
+ }
510
+ if (updates.headed !== undefined) {
511
+ sets.push("headed = ?");
512
+ params.push(updates.headed);
513
+ }
514
+ if (updates.parallel !== undefined) {
515
+ sets.push("parallel = ?");
516
+ params.push(updates.parallel);
517
+ }
518
+ if (updates.total !== undefined) {
519
+ sets.push("total = ?");
520
+ params.push(updates.total);
521
+ }
522
+ if (updates.passed !== undefined) {
523
+ sets.push("passed = ?");
524
+ params.push(updates.passed);
525
+ }
526
+ if (updates.failed !== undefined) {
527
+ sets.push("failed = ?");
528
+ params.push(updates.failed);
529
+ }
530
+ if (updates.started_at !== undefined) {
531
+ sets.push("started_at = ?");
532
+ params.push(updates.started_at);
533
+ }
534
+ if (updates.finished_at !== undefined) {
535
+ sets.push("finished_at = ?");
536
+ params.push(updates.finished_at);
537
+ }
538
+ if (updates.metadata !== undefined) {
539
+ sets.push("metadata = ?");
540
+ params.push(updates.metadata);
541
+ }
542
+ if (sets.length === 0) {
543
+ return existing;
544
+ }
545
+ params.push(existing.id);
546
+ db2.query(`UPDATE runs SET ${sets.join(", ")} WHERE id = ?`).run(...params);
547
+ return getRun(existing.id);
548
+ }
549
+
550
+ // src/db/results.ts
551
+ function createResult(input) {
552
+ const db2 = getDatabase();
553
+ const id = uuid();
554
+ const timestamp = now();
555
+ db2.query(`
556
+ INSERT INTO results (id, run_id, scenario_id, status, reasoning, error, steps_completed, steps_total, duration_ms, model, tokens_used, cost_cents, metadata, created_at)
557
+ VALUES (?, ?, ?, 'skipped', NULL, NULL, 0, ?, 0, ?, 0, 0, '{}', ?)
558
+ `).run(id, input.runId, input.scenarioId, input.stepsTotal, input.model, timestamp);
559
+ return getResult(id);
560
+ }
561
+ function getResult(id) {
562
+ const db2 = getDatabase();
563
+ let row = db2.query("SELECT * FROM results WHERE id = ?").get(id);
564
+ if (row)
565
+ return resultFromRow(row);
566
+ const fullId = resolvePartialId("results", id);
567
+ if (fullId) {
568
+ row = db2.query("SELECT * FROM results WHERE id = ?").get(fullId);
569
+ if (row)
570
+ return resultFromRow(row);
571
+ }
572
+ return null;
573
+ }
574
+ function listResults(runId) {
575
+ const db2 = getDatabase();
576
+ const rows = db2.query("SELECT * FROM results WHERE run_id = ? ORDER BY created_at ASC").all(runId);
577
+ return rows.map(resultFromRow);
578
+ }
579
+ function updateResult(id, updates) {
580
+ const db2 = getDatabase();
581
+ const existing = getResult(id);
582
+ if (!existing) {
583
+ throw new Error(`Result not found: ${id}`);
584
+ }
585
+ const sets = [];
586
+ const params = [];
587
+ if (updates.status !== undefined) {
588
+ sets.push("status = ?");
589
+ params.push(updates.status);
590
+ }
591
+ if (updates.reasoning !== undefined) {
592
+ sets.push("reasoning = ?");
593
+ params.push(updates.reasoning);
594
+ }
595
+ if (updates.error !== undefined) {
596
+ sets.push("error = ?");
597
+ params.push(updates.error);
598
+ }
599
+ if (updates.stepsCompleted !== undefined) {
600
+ sets.push("steps_completed = ?");
601
+ params.push(updates.stepsCompleted);
602
+ }
603
+ if (updates.durationMs !== undefined) {
604
+ sets.push("duration_ms = ?");
605
+ params.push(updates.durationMs);
606
+ }
607
+ if (updates.tokensUsed !== undefined) {
608
+ sets.push("tokens_used = ?");
609
+ params.push(updates.tokensUsed);
610
+ }
611
+ if (updates.costCents !== undefined) {
612
+ sets.push("cost_cents = ?");
613
+ params.push(updates.costCents);
614
+ }
615
+ if (sets.length === 0) {
616
+ return existing;
617
+ }
618
+ params.push(existing.id);
619
+ db2.query(`UPDATE results SET ${sets.join(", ")} WHERE id = ?`).run(...params);
620
+ return getResult(existing.id);
621
+ }
622
+ function getResultsByRun(runId) {
623
+ return listResults(runId);
624
+ }
625
+
626
+ // src/db/screenshots.ts
627
+ function createScreenshot(input) {
628
+ const db2 = getDatabase();
629
+ const id = uuid();
630
+ const timestamp = now();
631
+ db2.query(`
632
+ INSERT INTO screenshots (id, result_id, step_number, action, file_path, width, height, timestamp)
633
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
634
+ `).run(id, input.resultId, input.stepNumber, input.action, input.filePath, input.width, input.height, timestamp);
635
+ return getScreenshot(id);
636
+ }
637
+ function getScreenshot(id) {
638
+ const db2 = getDatabase();
639
+ const row = db2.query("SELECT * FROM screenshots WHERE id = ?").get(id);
640
+ return row ? screenshotFromRow(row) : null;
641
+ }
642
+ function listScreenshots(resultId) {
643
+ const db2 = getDatabase();
644
+ const rows = db2.query("SELECT * FROM screenshots WHERE result_id = ? ORDER BY step_number ASC").all(resultId);
645
+ return rows.map(screenshotFromRow);
646
+ }
647
+
648
+ // src/lib/browser.ts
649
+ import { chromium } from "playwright";
650
+ var DEFAULT_VIEWPORT = { width: 1280, height: 720 };
651
+ async function launchBrowser(options) {
652
+ const headless = options?.headless ?? true;
653
+ const viewport = options?.viewport ?? DEFAULT_VIEWPORT;
654
+ try {
655
+ const browser = await chromium.launch({
656
+ headless,
657
+ args: [
658
+ `--window-size=${viewport.width},${viewport.height}`
659
+ ]
660
+ });
661
+ return browser;
662
+ } catch (error) {
663
+ const message = error instanceof Error ? error.message : String(error);
664
+ throw new BrowserError(`Failed to launch browser: ${message}`);
665
+ }
666
+ }
667
+ async function getPage(browser, options) {
668
+ const viewport = options?.viewport ?? DEFAULT_VIEWPORT;
669
+ try {
670
+ const context = await browser.newContext({
671
+ viewport,
672
+ userAgent: options?.userAgent,
673
+ locale: options?.locale
674
+ });
675
+ const page = await context.newPage();
676
+ return page;
677
+ } catch (error) {
678
+ const message = error instanceof Error ? error.message : String(error);
679
+ throw new BrowserError(`Failed to create page: ${message}`);
680
+ }
681
+ }
682
+ async function closeBrowser(browser) {
683
+ try {
684
+ await browser.close();
685
+ } catch (error) {
686
+ const message = error instanceof Error ? error.message : String(error);
687
+ throw new BrowserError(`Failed to close browser: ${message}`);
688
+ }
689
+ }
690
+
691
+ // src/lib/screenshotter.ts
692
+ import { mkdirSync as mkdirSync2, existsSync as existsSync2 } from "fs";
693
+ import { join as join2 } from "path";
694
+ import { homedir as homedir2 } from "os";
695
+ function slugify(text) {
696
+ return text.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
697
+ }
698
+ function generateFilename(stepNumber, action) {
699
+ const padded = String(stepNumber).padStart(3, "0");
700
+ const slug = slugify(action);
701
+ return `${padded}-${slug}.png`;
702
+ }
703
+ function getScreenshotDir(baseDir, runId, scenarioSlug) {
704
+ return join2(baseDir, runId, scenarioSlug);
705
+ }
706
+ function ensureDir(dirPath) {
707
+ if (!existsSync2(dirPath)) {
708
+ mkdirSync2(dirPath, { recursive: true });
709
+ }
710
+ }
711
+ var DEFAULT_BASE_DIR = join2(homedir2(), ".testers", "screenshots");
712
+
713
+ class Screenshotter {
714
+ baseDir;
715
+ format;
716
+ quality;
717
+ fullPage;
718
+ constructor(options = {}) {
719
+ this.baseDir = options.baseDir ?? DEFAULT_BASE_DIR;
720
+ this.format = options.format ?? "png";
721
+ this.quality = options.quality ?? 90;
722
+ this.fullPage = options.fullPage ?? false;
723
+ }
724
+ async capture(page, options) {
725
+ const dir = getScreenshotDir(this.baseDir, options.runId, options.scenarioSlug);
726
+ const filename = generateFilename(options.stepNumber, options.action);
727
+ const filePath = join2(dir, filename);
728
+ ensureDir(dir);
729
+ await page.screenshot({
730
+ path: filePath,
731
+ fullPage: this.fullPage,
732
+ type: this.format,
733
+ quality: this.format === "jpeg" ? this.quality : undefined
734
+ });
735
+ const viewport = page.viewportSize() ?? { width: 0, height: 0 };
736
+ return {
737
+ filePath,
738
+ width: viewport.width,
739
+ height: viewport.height,
740
+ timestamp: new Date().toISOString()
741
+ };
742
+ }
743
+ async captureFullPage(page, options) {
744
+ const dir = getScreenshotDir(this.baseDir, options.runId, options.scenarioSlug);
745
+ const filename = generateFilename(options.stepNumber, options.action);
746
+ const filePath = join2(dir, filename);
747
+ ensureDir(dir);
748
+ await page.screenshot({
749
+ path: filePath,
750
+ fullPage: true,
751
+ type: this.format,
752
+ quality: this.format === "jpeg" ? this.quality : undefined
753
+ });
754
+ const viewport = page.viewportSize() ?? { width: 0, height: 0 };
755
+ return {
756
+ filePath,
757
+ width: viewport.width,
758
+ height: viewport.height,
759
+ timestamp: new Date().toISOString()
760
+ };
761
+ }
762
+ async captureElement(page, selector, options) {
763
+ const dir = getScreenshotDir(this.baseDir, options.runId, options.scenarioSlug);
764
+ const filename = generateFilename(options.stepNumber, options.action);
765
+ const filePath = join2(dir, filename);
766
+ ensureDir(dir);
767
+ await page.locator(selector).screenshot({
768
+ path: filePath,
769
+ type: this.format,
770
+ quality: this.format === "jpeg" ? this.quality : undefined
771
+ });
772
+ const viewport = page.viewportSize() ?? { width: 0, height: 0 };
773
+ return {
774
+ filePath,
775
+ width: viewport.width,
776
+ height: viewport.height,
777
+ timestamp: new Date().toISOString()
778
+ };
779
+ }
780
+ }
781
+
782
+ // src/lib/ai-client.ts
783
+ import Anthropic from "@anthropic-ai/sdk";
784
+ function resolveModel(nameOrPreset) {
785
+ if (nameOrPreset in MODEL_MAP) {
786
+ return MODEL_MAP[nameOrPreset];
787
+ }
788
+ return nameOrPreset;
789
+ }
790
+ var BROWSER_TOOLS = [
791
+ {
792
+ name: "navigate",
793
+ description: "Navigate the browser to a specific URL.",
794
+ input_schema: {
795
+ type: "object",
796
+ properties: {
797
+ url: { type: "string", description: "The URL to navigate to." }
798
+ },
799
+ required: ["url"]
800
+ }
801
+ },
802
+ {
803
+ name: "click",
804
+ description: "Click on an element matching the given CSS selector.",
805
+ input_schema: {
806
+ type: "object",
807
+ properties: {
808
+ selector: {
809
+ type: "string",
810
+ description: "CSS selector of the element to click."
811
+ }
812
+ },
813
+ required: ["selector"]
814
+ }
815
+ },
816
+ {
817
+ name: "fill",
818
+ description: "Fill an input field with the given value.",
819
+ input_schema: {
820
+ type: "object",
821
+ properties: {
822
+ selector: {
823
+ type: "string",
824
+ description: "CSS selector of the input field."
825
+ },
826
+ value: {
827
+ type: "string",
828
+ description: "The value to fill into the input."
829
+ }
830
+ },
831
+ required: ["selector", "value"]
832
+ }
833
+ },
834
+ {
835
+ name: "select_option",
836
+ description: "Select an option from a dropdown/select element.",
837
+ input_schema: {
838
+ type: "object",
839
+ properties: {
840
+ selector: {
841
+ type: "string",
842
+ description: "CSS selector of the select element."
843
+ },
844
+ value: {
845
+ type: "string",
846
+ description: "The value of the option to select."
847
+ }
848
+ },
849
+ required: ["selector", "value"]
850
+ }
851
+ },
852
+ {
853
+ name: "screenshot",
854
+ description: "Take a screenshot of the current page state.",
855
+ input_schema: {
856
+ type: "object",
857
+ properties: {},
858
+ required: []
859
+ }
860
+ },
861
+ {
862
+ name: "get_text",
863
+ description: "Get the text content of an element matching the selector.",
864
+ input_schema: {
865
+ type: "object",
866
+ properties: {
867
+ selector: {
868
+ type: "string",
869
+ description: "CSS selector of the element."
870
+ }
871
+ },
872
+ required: ["selector"]
873
+ }
874
+ },
875
+ {
876
+ name: "get_url",
877
+ description: "Get the current page URL.",
878
+ input_schema: {
879
+ type: "object",
880
+ properties: {},
881
+ required: []
882
+ }
883
+ },
884
+ {
885
+ name: "wait_for",
886
+ description: "Wait for an element matching the selector to appear on the page.",
887
+ input_schema: {
888
+ type: "object",
889
+ properties: {
890
+ selector: {
891
+ type: "string",
892
+ description: "CSS selector to wait for."
893
+ },
894
+ timeout: {
895
+ type: "number",
896
+ description: "Maximum time to wait in milliseconds (default: 10000)."
897
+ }
898
+ },
899
+ required: ["selector"]
900
+ }
901
+ },
902
+ {
903
+ name: "go_back",
904
+ description: "Navigate back to the previous page.",
905
+ input_schema: {
906
+ type: "object",
907
+ properties: {},
908
+ required: []
909
+ }
910
+ },
911
+ {
912
+ name: "press_key",
913
+ description: "Press a keyboard key (e.g., Enter, Tab, Escape, ArrowDown).",
914
+ input_schema: {
915
+ type: "object",
916
+ properties: {
917
+ key: {
918
+ type: "string",
919
+ description: "The key to press (e.g., 'Enter', 'Tab', 'Escape')."
920
+ }
921
+ },
922
+ required: ["key"]
923
+ }
924
+ },
925
+ {
926
+ name: "assert_visible",
927
+ description: "Assert that an element matching the selector is visible on the page. Returns 'true' or 'false'.",
928
+ input_schema: {
929
+ type: "object",
930
+ properties: {
931
+ selector: {
932
+ type: "string",
933
+ description: "CSS selector of the element to check."
934
+ }
935
+ },
936
+ required: ["selector"]
937
+ }
938
+ },
939
+ {
940
+ name: "assert_text",
941
+ description: "Assert that the given text is visible somewhere on the page. Returns 'true' or 'false'.",
942
+ input_schema: {
943
+ type: "object",
944
+ properties: {
945
+ text: {
946
+ type: "string",
947
+ description: "The text to search for on the page."
948
+ }
949
+ },
950
+ required: ["text"]
951
+ }
952
+ },
953
+ {
954
+ name: "report_result",
955
+ description: "Report the final test result. Call this when you have completed testing the scenario. This MUST be the last tool you call.",
956
+ input_schema: {
957
+ type: "object",
958
+ properties: {
959
+ status: {
960
+ type: "string",
961
+ enum: ["passed", "failed"],
962
+ description: "Whether the test scenario passed or failed."
963
+ },
964
+ reasoning: {
965
+ type: "string",
966
+ description: "Detailed explanation of why the test passed or failed, including any issues found."
967
+ }
968
+ },
969
+ required: ["status", "reasoning"]
970
+ }
971
+ }
972
+ ];
973
+ async function executeTool(page, screenshotter, toolName, toolInput, context) {
974
+ try {
975
+ switch (toolName) {
976
+ case "navigate": {
977
+ const url = toolInput.url;
978
+ await page.goto(url, { waitUntil: "domcontentloaded" });
979
+ const screenshot = await screenshotter.capture(page, {
980
+ runId: context.runId,
981
+ scenarioSlug: context.scenarioSlug,
982
+ stepNumber: context.stepNumber,
983
+ action: "navigate"
984
+ });
985
+ return {
986
+ result: `Navigated to ${url}`,
987
+ screenshot
988
+ };
989
+ }
990
+ case "click": {
991
+ const selector = toolInput.selector;
992
+ await page.click(selector);
993
+ const screenshot = await screenshotter.capture(page, {
994
+ runId: context.runId,
995
+ scenarioSlug: context.scenarioSlug,
996
+ stepNumber: context.stepNumber,
997
+ action: "click"
998
+ });
999
+ return {
1000
+ result: `Clicked element: ${selector}`,
1001
+ screenshot
1002
+ };
1003
+ }
1004
+ case "fill": {
1005
+ const selector = toolInput.selector;
1006
+ const value = toolInput.value;
1007
+ await page.fill(selector, value);
1008
+ return {
1009
+ result: `Filled "${selector}" with value`
1010
+ };
1011
+ }
1012
+ case "select_option": {
1013
+ const selector = toolInput.selector;
1014
+ const value = toolInput.value;
1015
+ await page.selectOption(selector, value);
1016
+ return {
1017
+ result: `Selected option "${value}" in ${selector}`
1018
+ };
1019
+ }
1020
+ case "screenshot": {
1021
+ const screenshot = await screenshotter.capture(page, {
1022
+ runId: context.runId,
1023
+ scenarioSlug: context.scenarioSlug,
1024
+ stepNumber: context.stepNumber,
1025
+ action: "screenshot"
1026
+ });
1027
+ return {
1028
+ result: "Screenshot captured",
1029
+ screenshot
1030
+ };
1031
+ }
1032
+ case "get_text": {
1033
+ const selector = toolInput.selector;
1034
+ const text = await page.locator(selector).textContent();
1035
+ return {
1036
+ result: text ?? "(no text content)"
1037
+ };
1038
+ }
1039
+ case "get_url": {
1040
+ return {
1041
+ result: page.url()
1042
+ };
1043
+ }
1044
+ case "wait_for": {
1045
+ const selector = toolInput.selector;
1046
+ const timeout = typeof toolInput.timeout === "number" ? toolInput.timeout : 1e4;
1047
+ await page.waitForSelector(selector, { timeout });
1048
+ return {
1049
+ result: `Element "${selector}" appeared`
1050
+ };
1051
+ }
1052
+ case "go_back": {
1053
+ await page.goBack();
1054
+ return {
1055
+ result: "Navigated back"
1056
+ };
1057
+ }
1058
+ case "press_key": {
1059
+ const key = toolInput.key;
1060
+ await page.keyboard.press(key);
1061
+ return {
1062
+ result: `Pressed key: ${key}`
1063
+ };
1064
+ }
1065
+ case "assert_visible": {
1066
+ const selector = toolInput.selector;
1067
+ try {
1068
+ const visible = await page.locator(selector).isVisible();
1069
+ return { result: visible ? "true" : "false" };
1070
+ } catch {
1071
+ return { result: "false" };
1072
+ }
1073
+ }
1074
+ case "assert_text": {
1075
+ const text = toolInput.text;
1076
+ try {
1077
+ const bodyText = await page.locator("body").textContent();
1078
+ const found = bodyText ? bodyText.includes(text) : false;
1079
+ return { result: found ? "true" : "false" };
1080
+ } catch {
1081
+ return { result: "false" };
1082
+ }
1083
+ }
1084
+ case "report_result": {
1085
+ const status = toolInput.status;
1086
+ const reasoning = toolInput.reasoning;
1087
+ return {
1088
+ result: `Test ${status}: ${reasoning}`
1089
+ };
1090
+ }
1091
+ default:
1092
+ return { result: `Unknown tool: ${toolName}` };
1093
+ }
1094
+ } catch (error) {
1095
+ const message = error instanceof Error ? error.message : String(error);
1096
+ return { result: `Error executing ${toolName}: ${message}` };
1097
+ }
1098
+ }
1099
+ async function runAgentLoop(options) {
1100
+ const {
1101
+ client,
1102
+ page,
1103
+ scenario,
1104
+ screenshotter,
1105
+ model,
1106
+ runId,
1107
+ maxTurns = 30
1108
+ } = options;
1109
+ const systemPrompt = [
1110
+ "You are a QA testing agent. Test the following scenario by interacting with the browser.",
1111
+ "Use the provided tools to navigate, click, fill forms, and verify results.",
1112
+ "When done, call report_result with your findings.",
1113
+ "Be methodical: navigate to the target page first, then follow the test steps.",
1114
+ "If a step fails, try reasonable alternatives before reporting failure.",
1115
+ "Always report a final result \u2014 never leave a test incomplete."
1116
+ ].join(" ");
1117
+ const userParts = [
1118
+ `**Scenario:** ${scenario.name}`,
1119
+ `**Description:** ${scenario.description}`
1120
+ ];
1121
+ if (scenario.targetPath) {
1122
+ userParts.push(`**Target Path:** ${scenario.targetPath}`);
1123
+ }
1124
+ if (scenario.steps.length > 0) {
1125
+ userParts.push("**Steps:**");
1126
+ for (let i = 0;i < scenario.steps.length; i++) {
1127
+ userParts.push(`${i + 1}. ${scenario.steps[i]}`);
1128
+ }
1129
+ }
1130
+ const userMessage = userParts.join(`
1131
+ `);
1132
+ const screenshots = [];
1133
+ let tokensUsed = 0;
1134
+ let stepNumber = 0;
1135
+ const scenarioSlug = scenario.name.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
1136
+ let messages = [
1137
+ { role: "user", content: userMessage }
1138
+ ];
1139
+ try {
1140
+ for (let turn = 0;turn < maxTurns; turn++) {
1141
+ const response = await client.messages.create({
1142
+ model,
1143
+ max_tokens: 4096,
1144
+ system: systemPrompt,
1145
+ tools: BROWSER_TOOLS,
1146
+ messages
1147
+ });
1148
+ if (response.usage) {
1149
+ tokensUsed += response.usage.input_tokens + response.usage.output_tokens;
1150
+ }
1151
+ const toolUseBlocks = response.content.filter((block) => block.type === "tool_use");
1152
+ if (toolUseBlocks.length === 0 && response.stop_reason === "end_turn") {
1153
+ const textBlocks = response.content.filter((block) => block.type === "text");
1154
+ const textReasoning = textBlocks.map((b) => b.text).join(`
1155
+ `);
1156
+ return {
1157
+ status: "error",
1158
+ reasoning: textReasoning || "Agent ended without calling report_result",
1159
+ stepsCompleted: stepNumber,
1160
+ tokensUsed,
1161
+ screenshots
1162
+ };
1163
+ }
1164
+ const toolResults = [];
1165
+ for (const toolBlock of toolUseBlocks) {
1166
+ stepNumber++;
1167
+ const toolInput = toolBlock.input;
1168
+ const execResult = await executeTool(page, screenshotter, toolBlock.name, toolInput, { runId, scenarioSlug, stepNumber });
1169
+ if (execResult.screenshot) {
1170
+ screenshots.push({
1171
+ ...execResult.screenshot,
1172
+ action: toolBlock.name,
1173
+ stepNumber
1174
+ });
1175
+ }
1176
+ toolResults.push({
1177
+ type: "tool_result",
1178
+ tool_use_id: toolBlock.id,
1179
+ content: execResult.result
1180
+ });
1181
+ if (toolBlock.name === "report_result") {
1182
+ const status = toolInput.status;
1183
+ const reasoning = toolInput.reasoning;
1184
+ return {
1185
+ status,
1186
+ reasoning,
1187
+ stepsCompleted: stepNumber,
1188
+ tokensUsed,
1189
+ screenshots
1190
+ };
1191
+ }
1192
+ }
1193
+ messages = [
1194
+ ...messages,
1195
+ { role: "assistant", content: response.content },
1196
+ { role: "user", content: toolResults }
1197
+ ];
1198
+ }
1199
+ return {
1200
+ status: "error",
1201
+ reasoning: `Agent reached maximum turn limit (${maxTurns}) without reporting a result`,
1202
+ stepsCompleted: stepNumber,
1203
+ tokensUsed,
1204
+ screenshots
1205
+ };
1206
+ } catch (error) {
1207
+ const message = error instanceof Error ? error.message : String(error);
1208
+ throw new AIClientError(`Agent loop failed: ${message}`);
1209
+ }
1210
+ }
1211
+ function createClient(apiKey) {
1212
+ const key = apiKey ?? process.env["ANTHROPIC_API_KEY"];
1213
+ if (!key) {
1214
+ throw new AIClientError("No Anthropic API key provided. Set ANTHROPIC_API_KEY or pass it explicitly.");
1215
+ }
1216
+ return new Anthropic({ apiKey: key });
1217
+ }
1218
+
1219
+ // src/lib/config.ts
1220
+ import { homedir as homedir3 } from "os";
1221
+ import { join as join3 } from "path";
1222
+ import { readFileSync, existsSync as existsSync3 } from "fs";
1223
+ var CONFIG_DIR = join3(homedir3(), ".testers");
1224
+ var CONFIG_PATH = join3(CONFIG_DIR, "config.json");
1225
+ function getDefaultConfig() {
1226
+ return {
1227
+ defaultModel: "claude-haiku-4-5-20251001",
1228
+ models: { ...MODEL_MAP },
1229
+ browser: {
1230
+ headless: true,
1231
+ viewport: { width: 1280, height: 720 },
1232
+ timeout: 60000
1233
+ },
1234
+ screenshots: {
1235
+ dir: join3(homedir3(), ".testers", "screenshots"),
1236
+ format: "png",
1237
+ quality: 90,
1238
+ fullPage: false
1239
+ }
1240
+ };
1241
+ }
1242
+ function loadConfig() {
1243
+ const defaults = getDefaultConfig();
1244
+ let fileConfig = {};
1245
+ if (existsSync3(CONFIG_PATH)) {
1246
+ try {
1247
+ const raw = readFileSync(CONFIG_PATH, "utf-8");
1248
+ fileConfig = JSON.parse(raw);
1249
+ } catch {}
1250
+ }
1251
+ const config = {
1252
+ defaultModel: fileConfig.defaultModel ?? defaults.defaultModel,
1253
+ models: fileConfig.models ? { ...defaults.models, ...fileConfig.models } : { ...defaults.models },
1254
+ browser: fileConfig.browser ? { ...defaults.browser, ...fileConfig.browser } : { ...defaults.browser },
1255
+ screenshots: fileConfig.screenshots ? { ...defaults.screenshots, ...fileConfig.screenshots } : { ...defaults.screenshots },
1256
+ anthropicApiKey: fileConfig.anthropicApiKey,
1257
+ todosDbPath: fileConfig.todosDbPath
1258
+ };
1259
+ const envModel = process.env["TESTERS_MODEL"];
1260
+ if (envModel) {
1261
+ config.defaultModel = envModel;
1262
+ }
1263
+ const envScreenshotsDir = process.env["TESTERS_SCREENSHOTS_DIR"];
1264
+ if (envScreenshotsDir) {
1265
+ config.screenshots.dir = envScreenshotsDir;
1266
+ }
1267
+ const envApiKey = process.env["ANTHROPIC_API_KEY"];
1268
+ if (envApiKey) {
1269
+ config.anthropicApiKey = envApiKey;
1270
+ }
1271
+ return config;
1272
+ }
1273
+
1274
+ // src/lib/runner.ts
1275
+ var eventHandler = null;
1276
+ function emit(event) {
1277
+ if (eventHandler)
1278
+ eventHandler(event);
1279
+ }
1280
+ async function runSingleScenario(scenario, runId, options) {
1281
+ const config = loadConfig();
1282
+ const model = resolveModel(options.model ?? scenario.model ?? config.defaultModel);
1283
+ const client = createClient(options.apiKey ?? config.anthropicApiKey);
1284
+ const screenshotter = new Screenshotter({
1285
+ baseDir: options.screenshotDir ?? config.screenshots.dir
1286
+ });
1287
+ const result = createResult({
1288
+ runId,
1289
+ scenarioId: scenario.id,
1290
+ model,
1291
+ stepsTotal: scenario.steps.length || 10
1292
+ });
1293
+ emit({ type: "scenario:start", scenarioId: scenario.id, scenarioName: scenario.name, resultId: result.id, runId });
1294
+ let browser = null;
1295
+ let page = null;
1296
+ try {
1297
+ browser = await launchBrowser({ headless: !(options.headed ?? false) });
1298
+ page = await getPage(browser, {
1299
+ viewport: config.browser.viewport
1300
+ });
1301
+ const targetUrl = scenario.targetPath ? `${options.url.replace(/\/$/, "")}${scenario.targetPath}` : options.url;
1302
+ await page.goto(targetUrl, { timeout: options.timeout ?? config.browser.timeout });
1303
+ const agentResult = await runAgentLoop({
1304
+ client,
1305
+ page,
1306
+ scenario,
1307
+ screenshotter,
1308
+ model,
1309
+ runId,
1310
+ maxTurns: 30
1311
+ });
1312
+ for (const ss of agentResult.screenshots) {
1313
+ createScreenshot({
1314
+ resultId: result.id,
1315
+ stepNumber: ss.stepNumber,
1316
+ action: ss.action,
1317
+ filePath: ss.filePath,
1318
+ width: ss.width,
1319
+ height: ss.height
1320
+ });
1321
+ emit({ type: "screenshot:captured", screenshotPath: ss.filePath, scenarioId: scenario.id, runId });
1322
+ }
1323
+ const updatedResult = updateResult(result.id, {
1324
+ status: agentResult.status,
1325
+ reasoning: agentResult.reasoning,
1326
+ stepsCompleted: agentResult.stepsCompleted,
1327
+ durationMs: Date.now() - new Date(result.createdAt).getTime(),
1328
+ tokensUsed: agentResult.tokensUsed,
1329
+ costCents: estimateCost(model, agentResult.tokensUsed)
1330
+ });
1331
+ const eventType = agentResult.status === "passed" ? "scenario:pass" : "scenario:fail";
1332
+ emit({ type: eventType, scenarioId: scenario.id, scenarioName: scenario.name, resultId: result.id, runId });
1333
+ return updatedResult;
1334
+ } catch (error) {
1335
+ const errorMsg = error instanceof Error ? error.message : String(error);
1336
+ const updatedResult = updateResult(result.id, {
1337
+ status: "error",
1338
+ error: errorMsg,
1339
+ durationMs: Date.now() - new Date(result.createdAt).getTime()
1340
+ });
1341
+ emit({ type: "scenario:error", scenarioId: scenario.id, scenarioName: scenario.name, error: errorMsg, runId });
1342
+ return updatedResult;
1343
+ } finally {
1344
+ if (browser)
1345
+ await closeBrowser(browser);
1346
+ }
1347
+ }
1348
+ async function runBatch(scenarios, options) {
1349
+ const config = loadConfig();
1350
+ const model = resolveModel(options.model ?? config.defaultModel);
1351
+ const parallel = options.parallel ?? 1;
1352
+ const run = createRun({
1353
+ url: options.url,
1354
+ model,
1355
+ headed: options.headed,
1356
+ parallel,
1357
+ projectId: options.projectId
1358
+ });
1359
+ updateRun(run.id, { status: "running", total: scenarios.length });
1360
+ const results = [];
1361
+ if (parallel <= 1) {
1362
+ for (const scenario of scenarios) {
1363
+ const result = await runSingleScenario(scenario, run.id, options);
1364
+ results.push(result);
1365
+ }
1366
+ } else {
1367
+ const queue = [...scenarios];
1368
+ const running = [];
1369
+ const processNext = async () => {
1370
+ const scenario = queue.shift();
1371
+ if (!scenario)
1372
+ return;
1373
+ const result = await runSingleScenario(scenario, run.id, options);
1374
+ results.push(result);
1375
+ await processNext();
1376
+ };
1377
+ const workers = Math.min(parallel, scenarios.length);
1378
+ for (let i = 0;i < workers; i++) {
1379
+ running.push(processNext());
1380
+ }
1381
+ await Promise.all(running);
1382
+ }
1383
+ const passed = results.filter((r) => r.status === "passed").length;
1384
+ const failed = results.filter((r) => r.status === "failed" || r.status === "error").length;
1385
+ const finalStatus = failed > 0 ? "failed" : "passed";
1386
+ const finalRun = updateRun(run.id, {
1387
+ status: finalStatus,
1388
+ passed,
1389
+ failed,
1390
+ total: scenarios.length,
1391
+ finished_at: new Date().toISOString()
1392
+ });
1393
+ emit({ type: "run:complete", runId: run.id });
1394
+ return { run: finalRun, results };
1395
+ }
1396
+ async function runByFilter(options) {
1397
+ let scenarios;
1398
+ if (options.scenarioIds && options.scenarioIds.length > 0) {
1399
+ const all = listScenarios({ projectId: options.projectId });
1400
+ scenarios = all.filter((s) => options.scenarioIds.includes(s.id) || options.scenarioIds.includes(s.shortId));
1401
+ } else {
1402
+ scenarios = listScenarios({
1403
+ projectId: options.projectId,
1404
+ tags: options.tags,
1405
+ priority: options.priority
1406
+ });
1407
+ }
1408
+ if (scenarios.length === 0) {
1409
+ const config = loadConfig();
1410
+ const model = resolveModel(options.model ?? config.defaultModel);
1411
+ const run = createRun({ url: options.url, model, projectId: options.projectId });
1412
+ updateRun(run.id, { status: "passed", total: 0, finished_at: new Date().toISOString() });
1413
+ return { run: getRun(run.id), results: [] };
1414
+ }
1415
+ return runBatch(scenarios, options);
1416
+ }
1417
+ function estimateCost(model, tokens) {
1418
+ const costs = {
1419
+ "claude-haiku-4-5-20251001": 0.1,
1420
+ "claude-sonnet-4-6-20260311": 0.9,
1421
+ "claude-opus-4-6-20260311": 3
1422
+ };
1423
+ const costPer1M = costs[model] ?? 0.5;
1424
+ return tokens / 1e6 * costPer1M * 100;
1425
+ }
1426
+
1427
+ // src/server/index.ts
1428
+ function parseUrl(req) {
1429
+ const url = new URL(req.url);
1430
+ return { pathname: url.pathname, searchParams: url.searchParams };
1431
+ }
1432
+ function jsonResponse(data, status = 200) {
1433
+ return new Response(JSON.stringify(data), {
1434
+ status,
1435
+ headers: {
1436
+ "Content-Type": "application/json",
1437
+ "Access-Control-Allow-Origin": "*",
1438
+ "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
1439
+ "Access-Control-Allow-Headers": "Content-Type, Authorization"
1440
+ }
1441
+ });
1442
+ }
1443
+ function errorResponse(message, status) {
1444
+ return jsonResponse({ error: message }, status);
1445
+ }
1446
+ var CONTENT_TYPES = {
1447
+ ".html": "text/html",
1448
+ ".css": "text/css",
1449
+ ".js": "application/javascript",
1450
+ ".mjs": "application/javascript",
1451
+ ".json": "application/json",
1452
+ ".png": "image/png",
1453
+ ".jpg": "image/jpeg",
1454
+ ".jpeg": "image/jpeg",
1455
+ ".gif": "image/gif",
1456
+ ".svg": "image/svg+xml",
1457
+ ".ico": "image/x-icon",
1458
+ ".woff": "font/woff",
1459
+ ".woff2": "font/woff2",
1460
+ ".ttf": "font/ttf",
1461
+ ".map": "application/json"
1462
+ };
1463
+ function getContentType(filePath) {
1464
+ const ext = filePath.slice(filePath.lastIndexOf("."));
1465
+ return CONTENT_TYPES[ext] ?? "application/octet-stream";
1466
+ }
1467
+ async function handleRequest(req) {
1468
+ const { pathname, searchParams } = parseUrl(req);
1469
+ const method = req.method;
1470
+ if (method === "OPTIONS") {
1471
+ return new Response(null, {
1472
+ status: 204,
1473
+ headers: {
1474
+ "Access-Control-Allow-Origin": "*",
1475
+ "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
1476
+ "Access-Control-Allow-Headers": "Content-Type, Authorization"
1477
+ }
1478
+ });
1479
+ }
1480
+ if (pathname === "/api/status" && method === "GET") {
1481
+ const config = loadConfig();
1482
+ getDatabase();
1483
+ const dbPath = process.env["TESTERS_DB_PATH"] ?? join4(homedir4(), ".testers", "testers.db");
1484
+ const scenarios = listScenarios();
1485
+ const runs = listRuns();
1486
+ return jsonResponse({
1487
+ dbPath,
1488
+ apiKeySet: !!config.anthropicApiKey,
1489
+ scenarioCount: scenarios.length,
1490
+ runCount: runs.length,
1491
+ version: "0.0.1"
1492
+ });
1493
+ }
1494
+ if (pathname === "/api/scenarios" && method === "GET") {
1495
+ const tag = searchParams.get("tag");
1496
+ const priority = searchParams.get("priority");
1497
+ const limit = searchParams.get("limit");
1498
+ const offset = searchParams.get("offset");
1499
+ const scenarios = listScenarios({
1500
+ tags: tag ? [tag] : undefined,
1501
+ priority,
1502
+ limit: limit ? parseInt(limit, 10) : undefined,
1503
+ offset: offset ? parseInt(offset, 10) : undefined
1504
+ });
1505
+ return jsonResponse(scenarios);
1506
+ }
1507
+ if (pathname === "/api/scenarios" && method === "POST") {
1508
+ try {
1509
+ const body = await req.json();
1510
+ const scenario = createScenario(body);
1511
+ return jsonResponse(scenario, 201);
1512
+ } catch (err) {
1513
+ const msg = err instanceof Error ? err.message : String(err);
1514
+ return errorResponse(msg, 400);
1515
+ }
1516
+ }
1517
+ const scenarioGetMatch = pathname.match(/^\/api\/scenarios\/([^/]+)$/);
1518
+ if (scenarioGetMatch && method === "GET") {
1519
+ const id = scenarioGetMatch[1];
1520
+ const scenario = getScenario(id) ?? getScenarioByShortId(id);
1521
+ if (!scenario)
1522
+ return errorResponse("Scenario not found", 404);
1523
+ return jsonResponse(scenario);
1524
+ }
1525
+ const scenarioUpdateMatch = pathname.match(/^\/api\/scenarios\/([^/]+)$/);
1526
+ if (scenarioUpdateMatch && method === "PUT") {
1527
+ const id = scenarioUpdateMatch[1];
1528
+ try {
1529
+ const body = await req.json();
1530
+ const { version, ...updates } = body;
1531
+ const scenario = updateScenario(id, updates, version);
1532
+ return jsonResponse(scenario);
1533
+ } catch (err) {
1534
+ if (err instanceof VersionConflictError) {
1535
+ return errorResponse(err.message, 409);
1536
+ }
1537
+ const msg = err instanceof Error ? err.message : String(err);
1538
+ return errorResponse(msg, 400);
1539
+ }
1540
+ }
1541
+ const scenarioDeleteMatch = pathname.match(/^\/api\/scenarios\/([^/]+)$/);
1542
+ if (scenarioDeleteMatch && method === "DELETE") {
1543
+ const id = scenarioDeleteMatch[1];
1544
+ const deleted = deleteScenario(id);
1545
+ if (!deleted)
1546
+ return errorResponse("Scenario not found", 404);
1547
+ return jsonResponse({ deleted: true });
1548
+ }
1549
+ if (pathname === "/api/runs" && method === "POST") {
1550
+ try {
1551
+ const body = await req.json();
1552
+ const runPromise = runByFilter(body);
1553
+ runPromise.then(() => {}).catch((err) => {
1554
+ console.error("Run failed:", err);
1555
+ });
1556
+ return jsonResponse({ status: "running", message: "Run started. Poll GET /api/runs to check status." }, 202);
1557
+ } catch (err) {
1558
+ const msg = err instanceof Error ? err.message : String(err);
1559
+ return errorResponse(msg, 400);
1560
+ }
1561
+ }
1562
+ if (pathname === "/api/runs" && method === "GET") {
1563
+ const status = searchParams.get("status");
1564
+ const limit = searchParams.get("limit");
1565
+ const runs = listRuns({
1566
+ status,
1567
+ limit: limit ? parseInt(limit, 10) : undefined
1568
+ });
1569
+ return jsonResponse(runs);
1570
+ }
1571
+ const runGetMatch = pathname.match(/^\/api\/runs\/([^/]+)$/);
1572
+ if (runGetMatch && method === "GET") {
1573
+ const id = runGetMatch[1];
1574
+ const run = getRun(id);
1575
+ if (!run)
1576
+ return errorResponse("Run not found", 404);
1577
+ const results = getResultsByRun(id);
1578
+ return jsonResponse({ ...run, results });
1579
+ }
1580
+ const resultGetMatch = pathname.match(/^\/api\/results\/([^/]+)$/);
1581
+ if (resultGetMatch && method === "GET") {
1582
+ const id = resultGetMatch[1];
1583
+ const result = getResult(id);
1584
+ if (!result)
1585
+ return errorResponse("Result not found", 404);
1586
+ const screenshots = listScreenshots(id);
1587
+ return jsonResponse({ ...result, screenshots });
1588
+ }
1589
+ const screenshotFileMatch = pathname.match(/^\/api\/screenshots\/([^/]+)\/file$/);
1590
+ if (screenshotFileMatch && method === "GET") {
1591
+ const id = screenshotFileMatch[1];
1592
+ const screenshot = getScreenshot(id);
1593
+ if (!screenshot)
1594
+ return errorResponse("Screenshot not found", 404);
1595
+ if (!existsSync4(screenshot.filePath)) {
1596
+ return errorResponse("Screenshot file not found on disk", 404);
1597
+ }
1598
+ const file = Bun.file(screenshot.filePath);
1599
+ return new Response(file, {
1600
+ headers: {
1601
+ "Content-Type": "image/png",
1602
+ "Access-Control-Allow-Origin": "*"
1603
+ }
1604
+ });
1605
+ }
1606
+ if (!pathname.startsWith("/api")) {
1607
+ const dashboardDir = join4(import.meta.dir, "..", "..", "dashboard", "dist");
1608
+ if (!existsSync4(dashboardDir)) {
1609
+ return new Response(`<!DOCTYPE html>
1610
+ <html>
1611
+ <head><title>Open Testers</title></head>
1612
+ <body style="font-family: system-ui, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background: #0a0a0a; color: #fafafa;">
1613
+ <div style="text-align: center;">
1614
+ <h1>Dashboard not built yet</h1>
1615
+ <p>Run: <code style="background: #1a1a1a; padding: 4px 8px; border-radius: 4px;">cd dashboard && bun run build</code></p>
1616
+ </div>
1617
+ </body>
1618
+ </html>`, {
1619
+ status: 200,
1620
+ headers: {
1621
+ "Content-Type": "text/html",
1622
+ "Access-Control-Allow-Origin": "*"
1623
+ }
1624
+ });
1625
+ }
1626
+ const filePath = join4(dashboardDir, pathname === "/" ? "index.html" : pathname);
1627
+ if (existsSync4(filePath)) {
1628
+ const file = Bun.file(filePath);
1629
+ return new Response(file, {
1630
+ headers: {
1631
+ "Content-Type": getContentType(filePath),
1632
+ "Access-Control-Allow-Origin": "*"
1633
+ }
1634
+ });
1635
+ }
1636
+ const indexPath = join4(dashboardDir, "index.html");
1637
+ if (existsSync4(indexPath)) {
1638
+ const file = Bun.file(indexPath);
1639
+ return new Response(file, {
1640
+ headers: {
1641
+ "Content-Type": "text/html",
1642
+ "Access-Control-Allow-Origin": "*"
1643
+ }
1644
+ });
1645
+ }
1646
+ }
1647
+ return errorResponse("Not found", 404);
1648
+ }
1649
+ var port = parseInt(process.env["TESTERS_PORT"] ?? "19450", 10);
1650
+ var server = Bun.serve({
1651
+ port,
1652
+ fetch: handleRequest
1653
+ });
1654
+ console.log(`Open Testers server running at http://localhost:${server.port}`);