smoke.ts
This file
Path
scripts/smoke.ts
173 lines · 4.7 KiB · TypeScript
Last changed by
Merge project/drop-proposals into mainoscabrielbefore adoptionb4ed08b
History of this fileTake it
The walk stopped after 200 commits: lines older than 10c8b54 point at it.
Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
line 1why, one step back1/**Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
lines 2–3why, one step back2 * Usage:3 * bun scripts/smoke.ts --stage <stage> [--url <origin>] [--web-url <origin>]Merge observability into mainoscabrielbefore adoption1985111
line 4why, one step back4 * [--since <instant>]Merge project/drop-proposals into mainoscabrielbefore adoptionb4ed08b
line 5why, one step back5 * bun scripts/smoke.ts --stage <stage> --loop [--as <handle>]Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
line 6why, one step back6 *Merge observability into mainoscabrielbefore adoption1985111
lines 7–11why, one step back7 * `--since` then reads the stage's Workers Logs since that instant, and fails8 * on any exception or error-level line: it needs infra's credentials, so the9 * deploy runs it through `varlock run --path infra/`. `--loop` reads10 * `OPERATOR_TOKEN` and `SESSION_TOKENS` (token to handle) from the env, never11 * from an argument.Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 12–29why, one step back12 */13import { BunRuntime, BunServices } from "@effect/platform-bun";14import { Handle, sameHandle } from "@repo/protocol/ids";15import {16 Config,17 Console,18 DateTime,19 Effect,20 Option,21 Redacted,22 Schema,23 Stdio,24} from "effect";2526import { main as probeMain } from "../infra/smoke.ts";27import {28 flagValue,29 platformOrigin,Add a prod stage served on the brand's own domainoscabrielbefore adoption78a5062
line 30why, one step back30 publicStages,Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 31–34why, one step back31 recordedSubdomain,32 stageOf,33 subdomainFile,34} from "../infra/stages.ts";Merge infra/drop-demo into mainoscabrielbefore adoptionc49b6bf
line 35why, one step back35import { PublicStageRefused, LoopFailed, smokeLoop } from "./lib/smoke/loop.ts";Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 36–37why, one step back3637export {Merge observability into mainoscabrielbefore adoption1985111
lines 38–40why, one step back38 BadSince,39 logProbes,40 NoLogCredentials,Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 41–48why, one step back41 NoOrigin,42 platformProbes,43 probes,44 smoke,45 SmokeFailed,46 webProbes,47} from "../infra/smoke.ts";48Merge observability into mainoscabrielbefore adoption1985111
line 49why, one step back49export type { LogsCheck, SmokeReport } from "../infra/smoke.ts";Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
line 50why, one step back50Merge infra/drop-demo into mainoscabrielbefore adoptionc49b6bf
line 51why, one step back51export { PublicStageRefused, LoopFailed, smokeLoop } from "./lib/smoke/loop.ts";Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 52–127why, one step back5253export type { SmokeLoopReport } from "./lib/smoke/loop.ts";5455const ANSWERER = "bram";5657const failed = (reason: string) => new LoopFailed({ reason, step: "people" });5859const runName = Effect.map(DateTime.now, (now) =>60 DateTime.formatIso(now)61 .slice(0, 19)62 .replaceAll(/[-:]/gu, "")63 .replace("T", "-")64);6566const decodeSessions = Schema.decodeUnknownEffect(67 Schema.fromJsonString(Schema.Record(Schema.String, Handle))68);6970const tokensFrom = (answerer: string) =>71 Effect.gen(function* () {72 const decider = yield* Config.Redacted("OPERATOR_TOKEN").pipe(73 Effect.mapError(() =>74 failed(75 "no OPERATOR_TOKEN in the environment: run with the stage's secrets in the env"76 )77 )78 );7980 const sessions = yield* Config.String("SESSION_TOKENS").pipe(81 Effect.mapError(() =>82 failed(83 "no SESSION_TOKENS in the environment: run with the stage's secrets in the env"84 )85 ),86 Effect.flatMap((raw) =>87 decodeSessions(raw).pipe(88 Effect.mapError(() =>89 failed("SESSION_TOKENS is not a JSON object from token to handle")90 )91 )92 )93 );9495 const token = Object.entries(sessions).find(([, handle]) =>96 sameHandle(handle, Handle.make(answerer))97 )?.[0];9899 return token === undefined100 ? yield* failed(101 `SESSION_TOKENS has no session for ${answerer}: name another with --as`102 )103 : { answerer: Redacted.make(token), decider };104 });105106const originOf = (107 argv: readonly string[],108 stage: Parameters<typeof platformOrigin>[0]109) =>110 Option.match(flagValue(argv, "--url"), {111 onNone: () =>112 recordedSubdomain.pipe(113 Effect.flatMap(Effect.fromOption),114 Effect.mapError(() =>115 failed(116 `no --url, and ${subdomainFile} doesn't record the account's workers.dev subdomain: run bun run bootstrap -- --stage ${stage} first`117 )118 ),119 Effect.map((subdomain) => platformOrigin(stage, subdomain))120 ),121 onSome: (url) => Effect.succeed(url.replace(/\/+$/u, "")),122 });123124const loopMain = (argv: readonly string[]) =>125 Effect.gen(function* () {126 const stage = yield* stageOf(argv);127Add a prod stage served on the brand's own domainoscabrielbefore adoption78a5062
line 128why, one step back128 yield* publicStages.has(stage)Merge infra/drop-demo into mainoscabrielbefore adoptionc49b6bf
line 129why, one step back129 ? Effect.fail(new PublicStageRefused({ stage }))Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 130–141why, one step back130 : Effect.void;131132 const answerer = Option.getOrElse(flagValue(argv, "--as"), () => ANSWERER);133 const tokens = yield* tokensFrom(answerer);134 const origin = yield* originOf(argv, stage);135136 yield* probeMain(argv.filter((arg) => arg !== "--loop"));137138 const run = yield* runName;139140 yield* Console.log(`smoke ${stage}: loop smoke-${run} at ${origin}`);141Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f
lines 142–148why, one step back142 const report = yield* smokeLoop({143 ...tokens,144 origin,145 run,146 stage,147 });148Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 149–156why, one step back149 const total = report.timings.reduce((sum, { ms }) => sum + ms, 0);150151 yield* Console.log(152 `smoke ${stage}: loop passed in ${total} ms: ${report.project} landed ${report.landed.slice(0, 12)} by ${report.author}, refused first with ${report.refusedWith.join(", ")}; trailers ${report.trailers.join("; ")}`153 );154 });155156export const main = (argv: readonly string[]) =>Merge branch 'build/910-draft-workflow'oscabrielbefore adoptiona0c8f4f
lines 157–163why, one step back157 Effect.gen(function* () {158 if (argv.includes("--loop")) {159 return yield* loopMain(argv);160 }161162 return yield* probeMain(argv);163 });Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 164–173why, one step back164165if (import.meta.main) {166 BunRuntime.runMain(167 Effect.gen(function* () {168 const args = yield* (yield* Stdio.Stdio).args;169170 return yield* main(args);171 }).pipe(Effect.provide(BunServices.layer))172 );173}