|
| 1 | +import { Effect } from "effect" |
| 2 | +import { looksJson } from "./assertions" |
| 3 | +import type { |
| 4 | + ActiveScenario, |
| 5 | + BuilderState, |
| 6 | + CallResult, |
| 7 | + Comparison, |
| 8 | + Method, |
| 9 | + ProjectOptions, |
| 10 | + ScenarioContext, |
| 11 | + SeededContext, |
| 12 | + TodoScenario, |
| 13 | +} from "./types" |
| 14 | + |
| 15 | +class ScenarioBuilder<S = undefined> { |
| 16 | + private readonly state: BuilderState<S> |
| 17 | + |
| 18 | + constructor(method: Method, path: string, name: string) { |
| 19 | + this.state = { |
| 20 | + method, |
| 21 | + path, |
| 22 | + name, |
| 23 | + project: { git: true }, |
| 24 | + seed: () => Effect.succeed(undefined as S), |
| 25 | + request: (ctx) => ({ path, headers: ctx.headers() }), |
| 26 | + capture: "full", |
| 27 | + mutates: false, |
| 28 | + reset: true, |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + global() { |
| 33 | + return this.clone({ project: undefined, request: () => ({ path: this.state.path }) }) |
| 34 | + } |
| 35 | + |
| 36 | + inProject(project: ProjectOptions = { git: true }) { |
| 37 | + return this.clone({ project }) |
| 38 | + } |
| 39 | + |
| 40 | + withLlm() { |
| 41 | + return this.clone({ project: { ...(this.state.project ?? { git: true }), llm: true } }) |
| 42 | + } |
| 43 | + |
| 44 | + at(request: BuilderState<S>["request"]) { |
| 45 | + return this.clone({ request }) |
| 46 | + } |
| 47 | + |
| 48 | + mutating() { |
| 49 | + return this.clone({ mutates: true }) |
| 50 | + } |
| 51 | + |
| 52 | + preserveDatabase() { |
| 53 | + return this.clone({ reset: false }) |
| 54 | + } |
| 55 | + |
| 56 | + stream() { |
| 57 | + return this.clone({ capture: "stream" }) |
| 58 | + } |
| 59 | + |
| 60 | + /** Assert a non-JSON or shape-only response. */ |
| 61 | + ok(status = 200, compare: Comparison = "status") { |
| 62 | + return this.done(compare, (_ctx, result) => |
| 63 | + Effect.sync(() => { |
| 64 | + if (result.status !== status) throw new Error(`expected ${status}, got ${result.status}: ${result.text}`) |
| 65 | + }), |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + status( |
| 70 | + status = 200, |
| 71 | + inspect?: (ctx: SeededContext<S>, result: CallResult) => Effect.Effect<void>, |
| 72 | + compare: Comparison = "status", |
| 73 | + ) { |
| 74 | + return this.done(compare, (ctx, result) => |
| 75 | + Effect.gen(function* () { |
| 76 | + if (result.status !== status) throw new Error(`expected ${status}, got ${result.status}: ${result.text}`) |
| 77 | + if (inspect) yield* inspect(ctx, result) |
| 78 | + }), |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + /** Assert JSON status/content-type plus an optional synchronous body check. */ |
| 83 | + json(status = 200, inspect?: (body: unknown, ctx: SeededContext<S>) => void, compare: Comparison = "json") { |
| 84 | + return this.jsonEffect(status, inspect ? (body, ctx) => Effect.sync(() => inspect(body, ctx)) : undefined, compare) |
| 85 | + } |
| 86 | + |
| 87 | + /** Assert JSON status/content-type plus optional Effect assertions, e.g. DB side effects. */ |
| 88 | + jsonEffect( |
| 89 | + status = 200, |
| 90 | + inspect?: (body: unknown, ctx: SeededContext<S>) => Effect.Effect<void>, |
| 91 | + compare: Comparison = "json", |
| 92 | + ) { |
| 93 | + return this.done(compare, (ctx, result) => |
| 94 | + Effect.gen(function* () { |
| 95 | + if (result.status !== status) throw new Error(`expected ${status}, got ${result.status}: ${result.text}`) |
| 96 | + if (!looksJson(result)) |
| 97 | + throw new Error(`expected JSON response, got ${result.contentType || "no content-type"}`) |
| 98 | + if (inspect) yield* inspect(result.body, ctx) |
| 99 | + }), |
| 100 | + ) |
| 101 | + } |
| 102 | + |
| 103 | + private clone(next: Partial<BuilderState<S>>) { |
| 104 | + const builder = new ScenarioBuilder<S>(this.state.method, this.state.path, this.state.name) |
| 105 | + Object.assign(builder.state, this.state, next) |
| 106 | + return builder |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Seed typed state before the HTTP request. The returned value becomes `ctx.state` |
| 111 | + * for `.at(...)` and assertions, giving stateful route tests type-safe setup. |
| 112 | + */ |
| 113 | + seeded<Next>(seed: (ctx: ScenarioContext) => Effect.Effect<Next>) { |
| 114 | + const builder = new ScenarioBuilder<Next>(this.state.method, this.state.path, this.state.name) |
| 115 | + Object.assign(builder.state, this.state, { seed }) |
| 116 | + return builder |
| 117 | + } |
| 118 | + |
| 119 | + private done( |
| 120 | + compare: Comparison, |
| 121 | + expect: (ctx: SeededContext<S>, result: CallResult) => Effect.Effect<void>, |
| 122 | + ): ActiveScenario { |
| 123 | + const state = this.state |
| 124 | + return { |
| 125 | + kind: "active", |
| 126 | + method: state.method, |
| 127 | + path: state.path, |
| 128 | + name: state.name, |
| 129 | + project: state.project, |
| 130 | + seed: state.seed, |
| 131 | + request: (ctx, seeded) => state.request({ ...ctx, state: seeded as S }), |
| 132 | + expect: (ctx, seeded, result) => expect({ ...ctx, state: seeded as S }, result), |
| 133 | + compare, |
| 134 | + capture: state.capture, |
| 135 | + mutates: state.mutates, |
| 136 | + reset: state.reset, |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +export const http = { |
| 142 | + get: (path: string, name: string) => new ScenarioBuilder("GET", path, name), |
| 143 | + post: (path: string, name: string) => new ScenarioBuilder("POST", path, name), |
| 144 | + put: (path: string, name: string) => new ScenarioBuilder("PUT", path, name), |
| 145 | + patch: (path: string, name: string) => new ScenarioBuilder("PATCH", path, name), |
| 146 | + delete: (path: string, name: string) => new ScenarioBuilder("DELETE", path, name), |
| 147 | +} |
| 148 | + |
| 149 | +export const pending = (method: Method, path: string, name: string, reason: string): TodoScenario => ({ |
| 150 | + kind: "todo", |
| 151 | + method, |
| 152 | + path, |
| 153 | + name, |
| 154 | + reason, |
| 155 | +}) |
| 156 | + |
| 157 | +export function route(template: string, params: Record<string, string>) { |
| 158 | + return Object.entries(params).reduce( |
| 159 | + (next, [key, value]) => next.replaceAll(`{${key}}`, value).replaceAll(`:${key}`, value), |
| 160 | + template, |
| 161 | + ) |
| 162 | +} |
| 163 | + |
| 164 | +export function controlledPtyInput(title: string | undefined) { |
| 165 | + return { |
| 166 | + command: "/bin/sh", |
| 167 | + args: ["-c", "sleep 30"], |
| 168 | + ...(title ? { title } : {}), |
| 169 | + } |
| 170 | +} |
0 commit comments