|
| 1 | +/** |
| 2 | + * State persistence for CLI commands |
| 3 | + * |
| 4 | + * Manages the `.probitas/` directory and stores run state between executions. |
| 5 | + * |
| 6 | + * @module |
| 7 | + */ |
| 8 | + |
| 9 | +import { join } from "@std/path"; |
| 10 | +import { relative } from "@std/path/relative"; |
| 11 | +import type { RunResult } from "@probitas/runner"; |
| 12 | + |
| 13 | +/** |
| 14 | + * A scenario that failed in a previous run |
| 15 | + */ |
| 16 | +export interface FailedScenario { |
| 17 | + /** Scenario name */ |
| 18 | + readonly name: string; |
| 19 | + /** Relative file path from project root */ |
| 20 | + readonly file: string; |
| 21 | + /** Error message (optional, for display purposes) */ |
| 22 | + readonly error?: string; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * State of the last run |
| 27 | + */ |
| 28 | +export interface LastRunState { |
| 29 | + /** Schema version for forward compatibility */ |
| 30 | + readonly version: 1; |
| 31 | + /** ISO timestamp of when the run completed */ |
| 32 | + readonly timestamp: string; |
| 33 | + /** List of failed scenarios */ |
| 34 | + readonly failed: readonly FailedScenario[]; |
| 35 | +} |
| 36 | + |
| 37 | +const STATE_DIR_NAME = ".probitas"; |
| 38 | +const LAST_RUN_FILE = "last-run.json"; |
| 39 | +const CURRENT_VERSION = 1; |
| 40 | + |
| 41 | +/** |
| 42 | + * Get the state directory path, creating it if it doesn't exist |
| 43 | + * |
| 44 | + * @param cwd - Project root directory |
| 45 | + * @returns Path to the state directory |
| 46 | + */ |
| 47 | +export async function getStateDir(cwd: string): Promise<string> { |
| 48 | + const stateDir = join(cwd, STATE_DIR_NAME); |
| 49 | + try { |
| 50 | + await Deno.mkdir(stateDir, { recursive: true }); |
| 51 | + } catch (error) { |
| 52 | + // Ignore if already exists |
| 53 | + if (!(error instanceof Deno.errors.AlreadyExists)) { |
| 54 | + throw error; |
| 55 | + } |
| 56 | + } |
| 57 | + return stateDir; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Save the last run state to disk |
| 62 | + * |
| 63 | + * Extracts failed scenarios from the run result and persists them |
| 64 | + * to `.probitas/last-run.json`. |
| 65 | + * |
| 66 | + * @param cwd - Project root directory |
| 67 | + * @param result - Run result from scenario execution |
| 68 | + */ |
| 69 | +export async function saveLastRunState( |
| 70 | + cwd: string, |
| 71 | + result: RunResult, |
| 72 | +): Promise<void> { |
| 73 | + const stateDir = await getStateDir(cwd); |
| 74 | + const statePath = join(stateDir, LAST_RUN_FILE); |
| 75 | + |
| 76 | + // Extract failed scenarios from result |
| 77 | + const failed: FailedScenario[] = []; |
| 78 | + for (const s of result.scenarios) { |
| 79 | + if (s.status !== "failed") continue; |
| 80 | + |
| 81 | + // Now TypeScript knows s.status is "failed" and s.error exists |
| 82 | + const metadata = s.metadata; |
| 83 | + const filePath = metadata.origin?.path ?? "unknown"; |
| 84 | + const relativeFile = filePath !== "unknown" |
| 85 | + ? relative(cwd, filePath) |
| 86 | + : "unknown"; |
| 87 | + |
| 88 | + failed.push({ |
| 89 | + name: metadata.name, |
| 90 | + file: relativeFile, |
| 91 | + error: s.error instanceof Error |
| 92 | + ? s.error.message |
| 93 | + : typeof s.error === "string" |
| 94 | + ? s.error |
| 95 | + : undefined, |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + const state: LastRunState = { |
| 100 | + version: CURRENT_VERSION, |
| 101 | + timestamp: new Date().toISOString(), |
| 102 | + failed, |
| 103 | + }; |
| 104 | + |
| 105 | + await Deno.writeTextFile(statePath, JSON.stringify(state, null, 2) + "\n"); |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Load the last run state from disk |
| 110 | + * |
| 111 | + * @param cwd - Project root directory |
| 112 | + * @returns The last run state, or undefined if no state file exists or it's invalid |
| 113 | + */ |
| 114 | +export async function loadLastRunState( |
| 115 | + cwd: string, |
| 116 | +): Promise<LastRunState | undefined> { |
| 117 | + const statePath = join(cwd, STATE_DIR_NAME, LAST_RUN_FILE); |
| 118 | + |
| 119 | + try { |
| 120 | + const content = await Deno.readTextFile(statePath); |
| 121 | + const state = JSON.parse(content); |
| 122 | + |
| 123 | + // Validate version |
| 124 | + if (state.version !== CURRENT_VERSION) { |
| 125 | + return undefined; |
| 126 | + } |
| 127 | + |
| 128 | + // Basic validation of required fields |
| 129 | + if ( |
| 130 | + typeof state.timestamp !== "string" || |
| 131 | + !Array.isArray(state.failed) |
| 132 | + ) { |
| 133 | + return undefined; |
| 134 | + } |
| 135 | + |
| 136 | + return state as LastRunState; |
| 137 | + } catch (error) { |
| 138 | + // File doesn't exist or can't be read |
| 139 | + if (error instanceof Deno.errors.NotFound) { |
| 140 | + return undefined; |
| 141 | + } |
| 142 | + // Invalid JSON or other error - treat as missing state |
| 143 | + if (error instanceof SyntaxError) { |
| 144 | + return undefined; |
| 145 | + } |
| 146 | + throw error; |
| 147 | + } |
| 148 | +} |
0 commit comments