index.ts 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. /**
  2. * End-to-end exerciser for the legacy Hono instance routes and the Effect HttpApi routes.
  3. *
  4. * The goal is not to be a normal unit test file. This is a route-coverage and parity
  5. * harness we can run while deleting Hono: every public route should eventually have a
  6. * small scenario that proves the Effect route decodes requests, uses the right instance
  7. * context, mutates storage when expected, and returns a compatible response shape.
  8. *
  9. * The script intentionally isolates `OPENCODE_DB` before importing modules that touch
  10. * storage. Scenarios may create/delete sessions and reset the database after each run,
  11. * so this must never point at a developer's real session database.
  12. *
  13. * DSL shape:
  14. * - `http.get/post/...` starts a scenario for one OpenAPI route key.
  15. * - `.seeded(...)` creates typed per-scenario state using Effect helpers on `ctx`.
  16. * - `.at(...)` builds the request from that typed state.
  17. * - `.json(...)` / `.jsonEffect(...)` assert response shape and optional side effects.
  18. * - `.mutating()` tells parity mode to run Effect and Hono in separate isolated contexts
  19. * so destructive routes compare equivalent fresh setups instead of sharing one DB.
  20. */
  21. import { Effect } from "effect"
  22. import { OpenApi } from "effect/unstable/httpapi"
  23. import { TestLLMServer } from "../../lib/llm-server"
  24. import path from "path"
  25. import { array, boolean, check, isRecord, message, object, stable } from "./assertions"
  26. import { controlledPtyInput, http, route } from "./dsl"
  27. import {
  28. cleanupExercisePaths,
  29. exerciseConfigDirectory,
  30. exerciseDataDirectory,
  31. exerciseDatabasePath,
  32. exerciseGlobalRoot,
  33. } from "./environment"
  34. import { color, printHeader, printResults } from "./report"
  35. import { coverageResult, matches, parseOptions, routeKey, routeKeys } from "./routing"
  36. import { runScenario } from "./runner"
  37. import { runtime } from "./runtime"
  38. import { type Scenario } from "./types"
  39. void (await import("@opencode-ai/core/util/log")).init({ print: false })
  40. const scenarios: Scenario[] = [
  41. http
  42. .get("/global/health", "global.health")
  43. .global()
  44. .json(200, (body) => {
  45. object(body)
  46. check(body.healthy === true, "server should report healthy")
  47. }),
  48. http
  49. .get("/global/event", "global.event")
  50. .global()
  51. .stream()
  52. .status(
  53. 200,
  54. (_ctx, result) =>
  55. Effect.sync(() => {
  56. check(result.contentType.includes("text/event-stream"), "global event should be an SSE stream")
  57. check(result.text.includes("server.connected"), "global event should emit initial connection event")
  58. }),
  59. "status",
  60. ),
  61. http.get("/global/config", "global.config.get").global().json(),
  62. http
  63. .patch("/global/config", "global.config.update")
  64. .global()
  65. .seeded(() =>
  66. Effect.promise(() =>
  67. Bun.write(
  68. path.join(exerciseConfigDirectory, "opencode.jsonc"),
  69. JSON.stringify({ username: "httpapi-global" }, null, 2),
  70. ),
  71. ),
  72. )
  73. .at(() => ({ path: "/global/config", body: { username: "httpapi-global" } }))
  74. .jsonEffect(
  75. 200,
  76. (body) =>
  77. Effect.gen(function* () {
  78. object(body)
  79. check(body.username === "httpapi-global", "global config update should return patched config")
  80. const text = yield* Effect.promise(() =>
  81. Bun.file(path.join(exerciseConfigDirectory, "opencode.jsonc")).text(),
  82. )
  83. check(text.includes('"username": "httpapi-global"'), "global config update should write isolated config file")
  84. }),
  85. "status",
  86. ),
  87. http
  88. .post("/global/dispose", "global.dispose")
  89. .global()
  90. .mutating()
  91. .json(
  92. 200,
  93. (body) => {
  94. check(body === true, "global dispose should return true")
  95. },
  96. "status",
  97. ),
  98. http.get("/path", "path.get").json(200, (body, ctx) => {
  99. object(body)
  100. check(body.directory === ctx.directory, "directory should resolve from x-opencode-directory")
  101. check(body.worktree === ctx.directory, "worktree should resolve from x-opencode-directory")
  102. }),
  103. http.get("/vcs", "vcs.get").json(),
  104. http
  105. .get("/vcs/diff", "vcs.diff")
  106. .at((ctx) => ({ path: "/vcs/diff?mode=git", headers: ctx.headers() }))
  107. .json(200, array),
  108. http.get("/command", "command.list").json(200, array, "status"),
  109. http.get("/agent", "app.agents").json(200, array, "status"),
  110. http.get("/skill", "app.skills").json(200, array, "status"),
  111. http.get("/lsp", "lsp.status").json(200, array),
  112. http.get("/formatter", "formatter.status").json(200, array),
  113. http.get("/config", "config.get").json(200, undefined, "status"),
  114. http
  115. .patch("/config", "config.update")
  116. .mutating()
  117. .at((ctx) => ({ path: "/config", headers: ctx.headers(), body: { username: "httpapi-local" } }))
  118. .json(
  119. 200,
  120. (body) => {
  121. object(body)
  122. check(body.username === "httpapi-local", "local config update should return patched config")
  123. },
  124. "status",
  125. ),
  126. http
  127. .patch("/config", "config.update.invalid")
  128. .at((ctx) => ({ path: "/config", headers: ctx.headers(), body: { username: 1 } }))
  129. .status(400),
  130. http.get("/config/providers", "config.providers").json(),
  131. http.get("/project", "project.list").json(200, array, "status"),
  132. http.get("/project/current", "project.current").json(
  133. 200,
  134. (body, ctx) => {
  135. object(body)
  136. check(body.worktree === ctx.directory, "current project should resolve from scenario directory")
  137. },
  138. "status",
  139. ),
  140. http
  141. .patch("/project/{projectID}", "project.update")
  142. .mutating()
  143. .seeded((ctx) => ctx.project())
  144. .at((ctx) => ({
  145. path: route("/project/{projectID}", { projectID: ctx.state.id }),
  146. headers: ctx.headers(),
  147. body: { name: "HTTP API Project", commands: { start: "bun --version" } },
  148. }))
  149. .json(
  150. 200,
  151. (body) => {
  152. object(body)
  153. check(body.name === "HTTP API Project", "project update should return patched name")
  154. check(
  155. isRecord(body.commands) && body.commands.start === "bun --version",
  156. "project update should return patched command",
  157. )
  158. },
  159. "status",
  160. ),
  161. http
  162. .post("/project/git/init", "project.initGit")
  163. .mutating()
  164. .inProject({ git: false })
  165. .json(
  166. 200,
  167. (body, ctx) => {
  168. object(body)
  169. check(body.worktree === ctx.directory, "git init should return current project")
  170. check(body.vcs === "git", "git init should mark the project as git-backed")
  171. },
  172. "status",
  173. ),
  174. http.get("/provider", "provider.list").json(),
  175. http.get("/provider/auth", "provider.auth").json(),
  176. http
  177. .post("/provider/{providerID}/oauth/authorize", "provider.oauth.authorize")
  178. .at((ctx) => ({
  179. path: route("/provider/{providerID}/oauth/authorize", { providerID: "httpapi" }),
  180. headers: ctx.headers(),
  181. body: { method: "bad" },
  182. }))
  183. .status(400),
  184. http
  185. .post("/provider/{providerID}/oauth/callback", "provider.oauth.callback")
  186. .at((ctx) => ({
  187. path: route("/provider/{providerID}/oauth/callback", { providerID: "httpapi" }),
  188. headers: ctx.headers(),
  189. body: { method: "bad" },
  190. }))
  191. .status(400),
  192. http.get("/permission", "permission.list").json(200, array),
  193. http
  194. .post("/permission/{requestID}/reply", "permission.reply.invalid")
  195. .at((ctx) => ({
  196. path: route("/permission/{requestID}/reply", { requestID: "per_httpapi" }),
  197. headers: ctx.headers(),
  198. body: { reply: "bad" },
  199. }))
  200. .status(400),
  201. http
  202. .post("/permission/{requestID}/reply", "permission.reply")
  203. .at((ctx) => ({
  204. path: route("/permission/{requestID}/reply", { requestID: "per_httpapi" }),
  205. headers: ctx.headers(),
  206. body: { reply: "once" },
  207. }))
  208. .json(200, (body) => {
  209. check(body === true, "permission reply should return true even when request is no longer pending")
  210. }),
  211. http.get("/question", "question.list").json(200, array),
  212. http
  213. .post("/question/{requestID}/reply", "question.reply.invalid")
  214. .at((ctx) => ({
  215. path: route("/question/{requestID}/reply", { requestID: "que_httpapi_reply" }),
  216. headers: ctx.headers(),
  217. body: { answers: "Yes" },
  218. }))
  219. .status(400),
  220. http
  221. .post("/question/{requestID}/reply", "question.reply")
  222. .at((ctx) => ({
  223. path: route("/question/{requestID}/reply", { requestID: "que_httpapi_reply" }),
  224. headers: ctx.headers(),
  225. body: { answers: [["Yes"]] },
  226. }))
  227. .json(200, (body) => {
  228. check(body === true, "question reply should return true even when request is no longer pending")
  229. }),
  230. http
  231. .post("/question/{requestID}/reject", "question.reject")
  232. .at((ctx) => ({
  233. path: route("/question/{requestID}/reject", { requestID: "que_httpapi_reject" }),
  234. headers: ctx.headers(),
  235. }))
  236. .json(200, (body) => {
  237. check(body === true, "question reject should return true even when request is no longer pending")
  238. }),
  239. http
  240. .get("/file", "file.list")
  241. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  242. .at((ctx) => ({ path: `/file?${new URLSearchParams({ path: "." })}`, headers: ctx.headers() }))
  243. .json(200, array),
  244. http
  245. .get("/file/content", "file.read")
  246. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  247. .at((ctx) => ({ path: `/file/content?${new URLSearchParams({ path: "hello.txt" })}`, headers: ctx.headers() }))
  248. .json(200, (body) => {
  249. object(body)
  250. check(body.content === "hello", `content should match seeded file: ${JSON.stringify(body)}`)
  251. }),
  252. http
  253. .get("/file/content", "file.read.missing")
  254. .at((ctx) => ({ path: `/file/content?${new URLSearchParams({ path: "missing.txt" })}`, headers: ctx.headers() }))
  255. .json(200, (body) => {
  256. object(body)
  257. check(body.type === "text" && body.content === "", "missing file content should return an empty text result")
  258. }),
  259. http.get("/file/status", "file.status").json(200, array),
  260. http
  261. .get("/find", "find.text")
  262. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  263. .at((ctx) => ({ path: `/find?${new URLSearchParams({ pattern: "hello" })}`, headers: ctx.headers() }))
  264. .json(200, array),
  265. http
  266. .get("/find/file", "find.files")
  267. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  268. .at((ctx) => ({
  269. path: `/find/file?${new URLSearchParams({ query: "hello", dirs: "false" })}`,
  270. headers: ctx.headers(),
  271. }))
  272. .json(200, array),
  273. http
  274. .get("/find/symbol", "find.symbols")
  275. .seeded((ctx) => ctx.file("hello.ts", "export const hello = 1\n"))
  276. .at((ctx) => ({ path: `/find/symbol?${new URLSearchParams({ query: "hello" })}`, headers: ctx.headers() }))
  277. .json(200, array),
  278. http
  279. .get("/event", "event.stream")
  280. .stream()
  281. .status(
  282. 200,
  283. (_ctx, result) =>
  284. Effect.sync(() => {
  285. check(result.contentType.includes("text/event-stream"), "event should be an SSE stream")
  286. check(result.text.includes("server.connected"), "event should emit initial connection event")
  287. }),
  288. "status",
  289. ),
  290. http.get("/mcp", "mcp.status").json(),
  291. http
  292. .post("/mcp", "mcp.add")
  293. .mutating()
  294. .at((ctx) => ({
  295. path: "/mcp",
  296. headers: ctx.headers(),
  297. body: { name: "httpapi-disabled", config: { type: "local", command: ["bun", "--version"], enabled: false } },
  298. }))
  299. .json(
  300. 200,
  301. (body) => {
  302. object(body)
  303. object(body["httpapi-disabled"])
  304. check(body["httpapi-disabled"].status === "disabled", "disabled MCP server should be added without spawning")
  305. },
  306. "status",
  307. ),
  308. http
  309. .post("/mcp", "mcp.add.invalid")
  310. .at((ctx) => ({
  311. path: "/mcp",
  312. headers: ctx.headers(),
  313. body: { name: "httpapi-invalid", config: { type: "invalid" } },
  314. }))
  315. .status(400),
  316. http
  317. .post("/mcp/{name}/auth", "mcp.auth.start")
  318. .at((ctx) => ({ path: route("/mcp/{name}/auth", { name: "httpapi-missing" }), headers: ctx.headers() }))
  319. .json(
  320. 400,
  321. (body) => {
  322. object(body)
  323. check(typeof body.error === "string", "unsupported MCP OAuth response should include error")
  324. },
  325. "status",
  326. ),
  327. http
  328. .delete("/mcp/{name}/auth", "mcp.auth.remove")
  329. .mutating()
  330. .at((ctx) => ({ path: route("/mcp/{name}/auth", { name: "httpapi-missing" }), headers: ctx.headers() }))
  331. .json(200, (body) => {
  332. object(body)
  333. check(body.success === true, "MCP auth removal should return success")
  334. }),
  335. http
  336. .post("/mcp/{name}/auth/authenticate", "mcp.auth.authenticate")
  337. .at((ctx) => ({
  338. path: route("/mcp/{name}/auth/authenticate", { name: "httpapi-missing" }),
  339. headers: ctx.headers(),
  340. }))
  341. .json(
  342. 400,
  343. (body) => {
  344. object(body)
  345. check(typeof body.error === "string", "unsupported MCP OAuth authenticate response should include error")
  346. },
  347. "status",
  348. ),
  349. http
  350. .post("/mcp/{name}/auth/callback", "mcp.auth.callback")
  351. .at((ctx) => ({
  352. path: route("/mcp/{name}/auth/callback", { name: "httpapi-missing" }),
  353. headers: ctx.headers(),
  354. body: { code: 1 },
  355. }))
  356. .status(400),
  357. http
  358. .post("/mcp/{name}/connect", "mcp.connect")
  359. .mutating()
  360. .at((ctx) => ({ path: route("/mcp/{name}/connect", { name: "httpapi-missing" }), headers: ctx.headers() }))
  361. .json(200, (body) => {
  362. check(body === true, "missing MCP connect should remain a no-op success")
  363. }),
  364. http
  365. .post("/mcp/{name}/disconnect", "mcp.disconnect")
  366. .mutating()
  367. .at((ctx) => ({ path: route("/mcp/{name}/disconnect", { name: "httpapi-missing" }), headers: ctx.headers() }))
  368. .json(200, (body) => {
  369. check(body === true, "missing MCP disconnect should remain a no-op success")
  370. }),
  371. http.get("/pty/shells", "pty.shells").json(200, array),
  372. http.get("/pty", "pty.list").json(200, array),
  373. http
  374. .post("/pty", "pty.create")
  375. .mutating()
  376. .at((ctx) => ({ path: "/pty", headers: ctx.headers(), body: controlledPtyInput("HTTP API PTY") }))
  377. .json(
  378. 200,
  379. (body, ctx) => {
  380. object(body)
  381. check(body.title === "HTTP API PTY", "PTY create should return requested title")
  382. check(body.command === "/bin/sh", "PTY create should use controlled shell command")
  383. check(body.cwd === ctx.directory, "PTY create should default cwd to scenario directory")
  384. },
  385. "status",
  386. ),
  387. http
  388. .post("/pty", "pty.create.invalid")
  389. .at((ctx) => ({ path: "/pty", headers: ctx.headers(), body: { command: 1 } }))
  390. .status(400),
  391. http
  392. .get("/pty/{ptyID}", "pty.get")
  393. .at((ctx) => ({ path: route("/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  394. .status(404),
  395. http
  396. .put("/pty/{ptyID}", "pty.update")
  397. .mutating()
  398. .at((ctx) => ({
  399. path: route("/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }),
  400. headers: ctx.headers(),
  401. body: { size: { rows: 0, cols: 0 } },
  402. }))
  403. .status(400),
  404. http
  405. .delete("/pty/{ptyID}", "pty.remove")
  406. .mutating()
  407. .at((ctx) => ({ path: route("/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  408. .json(200, (body) => {
  409. check(body === true, "PTY remove should return true")
  410. }),
  411. http
  412. .get("/pty/{ptyID}/connect", "pty.connect")
  413. .at((ctx) => ({ path: route("/pty/{ptyID}/connect", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  414. .status(404, undefined, "none"),
  415. http.get("/experimental/console", "experimental.console.get").json(),
  416. http.get("/experimental/console/orgs", "experimental.console.listOrgs").json(),
  417. http
  418. .post("/experimental/console/switch", "experimental.console.switchOrg")
  419. .at((ctx) => ({
  420. path: "/experimental/console/switch",
  421. headers: ctx.headers(),
  422. body: { accountID: "httpapi-account", orgID: "httpapi-org" },
  423. }))
  424. .status(400, undefined, "none"),
  425. http.get("/experimental/workspace/adapter", "experimental.workspace.adapter.list").json(200, array),
  426. http.get("/experimental/workspace", "experimental.workspace.list").json(200, array),
  427. http.get("/experimental/workspace/status", "experimental.workspace.status").json(200, array),
  428. http
  429. .post("/experimental/workspace", "experimental.workspace.create")
  430. .at((ctx) => ({ path: "/experimental/workspace", headers: ctx.headers(), body: {} }))
  431. .status(400),
  432. http
  433. .delete("/experimental/workspace/{id}", "experimental.workspace.remove")
  434. .mutating()
  435. .at((ctx) => ({
  436. path: route("/experimental/workspace/{id}", { id: "wrk_httpapi_missing" }),
  437. headers: ctx.headers(),
  438. }))
  439. .status(200),
  440. http
  441. .post("/experimental/workspace/warp", "experimental.workspace.warp")
  442. .at((ctx) => ({
  443. path: "/experimental/workspace/warp",
  444. headers: ctx.headers(),
  445. body: {},
  446. }))
  447. .status(400),
  448. http
  449. .get("/experimental/tool", "tool.list")
  450. .at((ctx) => ({
  451. path: `/experimental/tool?${new URLSearchParams({ provider: "opencode", model: "test" })}`,
  452. headers: ctx.headers(),
  453. }))
  454. .json(200, array, "status"),
  455. http.get("/experimental/tool/ids", "tool.ids").json(200, array),
  456. http.get("/experimental/worktree", "worktree.list").json(200, array),
  457. http
  458. .post("/experimental/worktree", "worktree.create")
  459. .mutating()
  460. .at((ctx) => ({ path: "/experimental/worktree", headers: ctx.headers(), body: { name: "api-dsl" } }))
  461. .jsonEffect(
  462. 200,
  463. (body, ctx) =>
  464. Effect.gen(function* () {
  465. object(body)
  466. check(typeof body.directory === "string", "created worktree should include directory")
  467. yield* ctx.worktreeRemove(body.directory)
  468. }),
  469. "status",
  470. ),
  471. http
  472. .post("/experimental/worktree", "worktree.create.invalid")
  473. .at((ctx) => ({ path: "/experimental/worktree", headers: ctx.headers(), body: { name: 1 } }))
  474. .status(400),
  475. http
  476. .delete("/experimental/worktree", "worktree.remove")
  477. .mutating()
  478. .seeded((ctx) => ctx.worktree({ name: "api-remove" }))
  479. .at((ctx) => ({ path: "/experimental/worktree", headers: ctx.headers(), body: { directory: ctx.state.directory } }))
  480. .json(200, (body) => {
  481. check(body === true, "worktree remove should return true")
  482. }),
  483. http
  484. .post("/experimental/worktree/reset", "worktree.reset")
  485. .mutating()
  486. .seeded((ctx) => ctx.worktree({ name: "api-reset" }))
  487. .at((ctx) => ({
  488. path: "/experimental/worktree/reset",
  489. headers: ctx.headers(),
  490. body: { directory: ctx.state.directory },
  491. }))
  492. .jsonEffect(200, (body, ctx) =>
  493. Effect.gen(function* () {
  494. check(body === true, "worktree reset should return true")
  495. yield* ctx.worktreeRemove(ctx.state.directory)
  496. }),
  497. ),
  498. http.get("/experimental/session", "experimental.session.list").json(200, array),
  499. http.get("/experimental/resource", "experimental.resource.list").json(),
  500. http
  501. .post("/sync/history", "sync.history.list")
  502. .at((ctx) => ({ path: "/sync/history", headers: ctx.headers(), body: {} }))
  503. .json(200, array),
  504. http
  505. .post("/sync/replay", "sync.replay")
  506. .at((ctx) => ({ path: "/sync/replay", headers: ctx.headers(), body: { directory: ctx.directory, events: [] } }))
  507. .status(400),
  508. http
  509. .post("/sync/start", "sync.start")
  510. .mutating()
  511. .preserveDatabase()
  512. .json(200, (body) => {
  513. check(body === true, "sync start should return true when no workspace sessions exist")
  514. }),
  515. http
  516. .post("/instance/dispose", "instance.dispose")
  517. .mutating()
  518. .json(200, (body) => {
  519. check(body === true, "instance dispose should return true")
  520. }),
  521. http
  522. .post("/log", "app.log")
  523. .global()
  524. .at(() => ({ path: "/log", body: { service: "httpapi-exercise", level: "info", message: "route coverage" } }))
  525. .json(200, (body) => {
  526. check(body === true, "log route should return true")
  527. }),
  528. http
  529. .put("/auth/{providerID}", "auth.set")
  530. .global()
  531. .at(() => ({ path: route("/auth/{providerID}", { providerID: "test" }), body: { type: "api", key: "test-key" } }))
  532. .jsonEffect(200, (body) =>
  533. Effect.gen(function* () {
  534. check(body === true, "auth set should return true")
  535. const auth = yield* Effect.promise(() => Bun.file(path.join(exerciseDataDirectory, "auth.json")).json())
  536. object(auth)
  537. check(isRecord(auth.test) && auth.test.key === "test-key", "auth set should write isolated auth file")
  538. }),
  539. ),
  540. http
  541. .delete("/auth/{providerID}", "auth.remove")
  542. .global()
  543. .seeded(() =>
  544. Effect.promise(() =>
  545. Bun.write(
  546. path.join(exerciseDataDirectory, "auth.json"),
  547. JSON.stringify({ test: { type: "api", key: "remove-me" } }),
  548. ),
  549. ),
  550. )
  551. .at(() => ({ path: route("/auth/{providerID}", { providerID: "test" }) }))
  552. .jsonEffect(200, (body) =>
  553. Effect.gen(function* () {
  554. check(body === true, "auth remove should return true")
  555. const auth = yield* Effect.promise(() => Bun.file(path.join(exerciseDataDirectory, "auth.json")).json())
  556. object(auth)
  557. check(auth.test === undefined, "auth remove should delete provider from isolated auth file")
  558. }),
  559. ),
  560. http
  561. .get("/session", "session.list")
  562. .seeded((ctx) => ctx.session({ title: "List me" }))
  563. .at((ctx) => ({ path: "/session?roots=true", headers: ctx.headers() }))
  564. .json(200, (body, ctx) => {
  565. array(body)
  566. check(
  567. body.some((item) => isRecord(item) && item.id === ctx.state.id && item.title === "List me"),
  568. "seeded session should be listed",
  569. )
  570. }),
  571. http
  572. .get("/session/status", "session.status")
  573. .seeded((ctx) => ctx.session({ title: "Status session" }))
  574. .json(200, object),
  575. http
  576. .post("/session", "session.create")
  577. .mutating()
  578. .at((ctx) => ({ path: "/session", headers: ctx.headers(), body: { title: "Created session" } }))
  579. .json(
  580. 200,
  581. (body, ctx) => {
  582. object(body)
  583. check(body.title === "Created session", "created session should use requested title")
  584. check(body.directory === ctx.directory, "created session should use scenario directory")
  585. },
  586. "status",
  587. ),
  588. http
  589. .get("/session/{sessionID}", "session.get")
  590. .seeded((ctx) => ctx.session({ title: "Get me" }))
  591. .at((ctx) => ({ path: route("/session/{sessionID}", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  592. .json(200, (body, ctx) => {
  593. object(body)
  594. check(body.id === ctx.state.id, "should return requested session")
  595. check(body.title === "Get me", "should preserve seeded title")
  596. }),
  597. http
  598. .get("/session/{sessionID}", "session.get.missing")
  599. .at((ctx) => ({
  600. path: route("/session/{sessionID}", { sessionID: "ses_httpapi_missing" }),
  601. headers: ctx.headers(),
  602. }))
  603. .status(404),
  604. http
  605. .patch("/session/{sessionID}", "session.update")
  606. .mutating()
  607. .seeded((ctx) => ctx.session({ title: "Before rename" }))
  608. .at((ctx) => ({
  609. path: route("/session/{sessionID}", { sessionID: ctx.state.id }),
  610. headers: ctx.headers(),
  611. body: { title: "After rename" },
  612. }))
  613. .json(
  614. 200,
  615. (body) => {
  616. object(body)
  617. check(body.title === "After rename", "updated session should use new title")
  618. },
  619. "status",
  620. ),
  621. http
  622. .patch("/session/{sessionID}", "session.update.invalid")
  623. .mutating()
  624. .at((ctx) => ({
  625. path: route("/session/{sessionID}", { sessionID: "ses_httpapi_missing" }),
  626. headers: ctx.headers(),
  627. body: { title: 1 },
  628. }))
  629. .status(400),
  630. http
  631. .delete("/session/{sessionID}", "session.delete")
  632. .mutating()
  633. .seeded((ctx) => ctx.session({ title: "Delete me" }))
  634. .at((ctx) => ({ path: route("/session/{sessionID}", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  635. .jsonEffect(200, (body, ctx) =>
  636. Effect.gen(function* () {
  637. check(body === true, "delete should return true")
  638. check((yield* ctx.sessionGet(ctx.state.id)) === undefined, "deleted session should not remain in storage")
  639. }),
  640. ),
  641. http
  642. .get("/session/{sessionID}/children", "session.children")
  643. .seeded((ctx) =>
  644. Effect.gen(function* () {
  645. const parent = yield* ctx.session({ title: "Parent" })
  646. const child = yield* ctx.session({ title: "Child", parentID: parent.id })
  647. return { parent, child }
  648. }),
  649. )
  650. .at((ctx) => ({
  651. path: route("/session/{sessionID}/children", { sessionID: ctx.state.parent.id }),
  652. headers: ctx.headers(),
  653. }))
  654. .json(200, (body, ctx) => {
  655. array(body)
  656. check(
  657. body.some((item) => isRecord(item) && item.id === ctx.state.child.id && item.parentID === ctx.state.parent.id),
  658. "children should include seeded child",
  659. )
  660. }),
  661. http
  662. .get("/session/{sessionID}/todo", "session.todo")
  663. .seeded((ctx) =>
  664. Effect.gen(function* () {
  665. const session = yield* ctx.session({ title: "Todo session" })
  666. const todos = [{ content: "cover session todo", status: "pending", priority: "high" }]
  667. yield* ctx.todos(session.id, todos)
  668. return { session, todos }
  669. }),
  670. )
  671. .at((ctx) => ({
  672. path: route("/session/{sessionID}/todo", { sessionID: ctx.state.session.id }),
  673. headers: ctx.headers(),
  674. }))
  675. .json(200, (body, ctx) => {
  676. check(stable(body) === stable(ctx.state.todos), "todos should match seeded state")
  677. }),
  678. http
  679. .get("/session/{sessionID}/diff", "session.diff")
  680. .seeded((ctx) => ctx.session({ title: "Diff session" }))
  681. .at((ctx) => ({ path: route("/session/{sessionID}/diff", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  682. .json(200, array),
  683. http
  684. .get("/session/{sessionID}/message", "session.messages")
  685. .seeded((ctx) => ctx.session({ title: "Messages session" }))
  686. .at((ctx) => ({ path: route("/session/{sessionID}/message", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  687. .json(200, (body) => {
  688. array(body)
  689. check(body.length === 0, "new session should have no messages")
  690. }),
  691. http
  692. .get("/session/{sessionID}/message/{messageID}", "session.message")
  693. .seeded((ctx) =>
  694. Effect.gen(function* () {
  695. const session = yield* ctx.session({ title: "Message get session" })
  696. const message = yield* ctx.message(session.id, { text: "read me" })
  697. return { session, message }
  698. }),
  699. )
  700. .at((ctx) => ({
  701. path: route("/session/{sessionID}/message/{messageID}", {
  702. sessionID: ctx.state.session.id,
  703. messageID: ctx.state.message.info.id,
  704. }),
  705. headers: ctx.headers(),
  706. }))
  707. .json(200, (body, ctx) => {
  708. object(body)
  709. check(isRecord(body.info) && body.info.id === ctx.state.message.info.id, "should return requested message")
  710. check(
  711. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.id === ctx.state.message.part.id),
  712. "message should include seeded part",
  713. )
  714. }),
  715. http
  716. .patch("/session/{sessionID}/message/{messageID}/part/{partID}", "part.update")
  717. .mutating()
  718. .seeded((ctx) =>
  719. Effect.gen(function* () {
  720. const session = yield* ctx.session({ title: "Part update session" })
  721. const message = yield* ctx.message(session.id, { text: "before" })
  722. return { session, message }
  723. }),
  724. )
  725. .at((ctx) => ({
  726. path: route("/session/{sessionID}/message/{messageID}/part/{partID}", {
  727. sessionID: ctx.state.session.id,
  728. messageID: ctx.state.message.info.id,
  729. partID: ctx.state.message.part.id,
  730. }),
  731. headers: ctx.headers(),
  732. body: { ...ctx.state.message.part, text: "after" },
  733. }))
  734. .json(
  735. 200,
  736. (body) => {
  737. object(body)
  738. check(body.type === "text" && body.text === "after", "updated part should be returned")
  739. },
  740. "status",
  741. ),
  742. http
  743. .delete("/session/{sessionID}/message/{messageID}/part/{partID}", "part.delete")
  744. .mutating()
  745. .seeded((ctx) =>
  746. Effect.gen(function* () {
  747. const session = yield* ctx.session({ title: "Part delete session" })
  748. const message = yield* ctx.message(session.id, { text: "delete part" })
  749. return { session, message }
  750. }),
  751. )
  752. .at((ctx) => ({
  753. path: route("/session/{sessionID}/message/{messageID}/part/{partID}", {
  754. sessionID: ctx.state.session.id,
  755. messageID: ctx.state.message.info.id,
  756. partID: ctx.state.message.part.id,
  757. }),
  758. headers: ctx.headers(),
  759. }))
  760. .jsonEffect(200, (body, ctx) =>
  761. Effect.gen(function* () {
  762. check(body === true, "delete part should return true")
  763. const messages = yield* ctx.messages(ctx.state.session.id)
  764. check(messages[0]?.parts.length === 0, "deleted part should not remain on message")
  765. }),
  766. ),
  767. http
  768. .delete("/session/{sessionID}/message/{messageID}", "session.deleteMessage")
  769. .mutating()
  770. .seeded((ctx) =>
  771. Effect.gen(function* () {
  772. const session = yield* ctx.session({ title: "Message delete session" })
  773. const message = yield* ctx.message(session.id, { text: "delete message" })
  774. return { session, message }
  775. }),
  776. )
  777. .at((ctx) => ({
  778. path: route("/session/{sessionID}/message/{messageID}", {
  779. sessionID: ctx.state.session.id,
  780. messageID: ctx.state.message.info.id,
  781. }),
  782. headers: ctx.headers(),
  783. }))
  784. .jsonEffect(200, (body, ctx) =>
  785. Effect.gen(function* () {
  786. check(body === true, "delete message should return true")
  787. check((yield* ctx.messages(ctx.state.session.id)).length === 0, "deleted message should not remain")
  788. }),
  789. ),
  790. http
  791. .post("/session/{sessionID}/fork", "session.fork")
  792. .mutating()
  793. .seeded((ctx) => ctx.session({ title: "Fork source" }))
  794. .at((ctx) => ({
  795. path: route("/session/{sessionID}/fork", { sessionID: ctx.state.id }),
  796. headers: ctx.headers(),
  797. body: {},
  798. }))
  799. .json(
  800. 200,
  801. (body) => {
  802. object(body)
  803. check(typeof body.id === "string", "fork should return a session")
  804. },
  805. "status",
  806. ),
  807. http
  808. .post("/session/{sessionID}/abort", "session.abort")
  809. .mutating()
  810. .seeded((ctx) => ctx.session({ title: "Abort session" }))
  811. .at((ctx) => ({ path: route("/session/{sessionID}/abort", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  812. .json(200, (body) => {
  813. check(body === true, "abort should return true")
  814. }),
  815. http
  816. .post("/session/{sessionID}/abort", "session.abort.missing")
  817. .at((ctx) => ({
  818. path: route("/session/{sessionID}/abort", { sessionID: "ses_httpapi_missing" }),
  819. headers: ctx.headers(),
  820. }))
  821. .json(200, (body) => {
  822. check(body === true, "missing session abort should remain a no-op success")
  823. }),
  824. http
  825. .post("/session/{sessionID}/init", "session.init")
  826. .preserveDatabase()
  827. .withLlm()
  828. .seeded((ctx) =>
  829. Effect.gen(function* () {
  830. const session = yield* ctx.session({ title: "Init session" })
  831. const message = yield* ctx.message(session.id, { text: "initialize" })
  832. yield* ctx.llmText("initialized")
  833. yield* ctx.llmText("initialized")
  834. return { session, message }
  835. }),
  836. )
  837. .at((ctx) => ({
  838. path: route("/session/{sessionID}/init", { sessionID: ctx.state.session.id }),
  839. headers: ctx.headers(),
  840. body: { providerID: "test", modelID: "test-model", messageID: ctx.state.message.info.id },
  841. }))
  842. .jsonEffect(200, (body, ctx) =>
  843. Effect.gen(function* () {
  844. check(body === true, "init should return true")
  845. yield* ctx.llmWait(1)
  846. }),
  847. ),
  848. http
  849. .post("/session/{sessionID}/message", "session.prompt")
  850. .preserveDatabase()
  851. .withLlm()
  852. .seeded((ctx) =>
  853. Effect.gen(function* () {
  854. const session = yield* ctx.session({ title: "LLM prompt session" })
  855. yield* ctx.llmText("fake assistant")
  856. yield* ctx.llmText("fake assistant")
  857. return session
  858. }),
  859. )
  860. .at((ctx) => ({
  861. path: route("/session/{sessionID}/message", { sessionID: ctx.state.id }),
  862. headers: ctx.headers(),
  863. body: {
  864. agent: "build",
  865. model: { providerID: "test", modelID: "test-model" },
  866. parts: [{ type: "text", text: "hello llm" }],
  867. },
  868. }))
  869. .jsonEffect(
  870. 200,
  871. (body, ctx) =>
  872. Effect.gen(function* () {
  873. object(body)
  874. check(isRecord(body.info) && body.info.role === "assistant", "prompt should return assistant message")
  875. check(
  876. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.text === "fake assistant"),
  877. "assistant message should use fake LLM text",
  878. )
  879. yield* ctx.llmWait(1)
  880. }),
  881. "status",
  882. ),
  883. http
  884. .post("/session/{sessionID}/prompt_async", "session.prompt_async")
  885. .preserveDatabase()
  886. .withLlm()
  887. .seeded((ctx) =>
  888. Effect.gen(function* () {
  889. const session = yield* ctx.session({ title: "Async prompt session" })
  890. yield* ctx.llmText("fake async assistant")
  891. yield* ctx.llmText("fake async assistant")
  892. return session
  893. }),
  894. )
  895. .at((ctx) => ({
  896. path: route("/session/{sessionID}/prompt_async", { sessionID: ctx.state.id }),
  897. headers: ctx.headers(),
  898. body: {
  899. agent: "build",
  900. model: { providerID: "test", modelID: "test-model" },
  901. parts: [{ type: "text", text: "hello async" }],
  902. },
  903. }))
  904. .status(204, (ctx) =>
  905. Effect.gen(function* () {
  906. yield* ctx.llmWait(1)
  907. }),
  908. ),
  909. http
  910. .post("/session/{sessionID}/command", "session.command")
  911. .preserveDatabase()
  912. .withLlm()
  913. .seeded((ctx) =>
  914. Effect.gen(function* () {
  915. const session = yield* ctx.session({ title: "Command session" })
  916. yield* ctx.llmText("command done")
  917. yield* ctx.llmText("command done")
  918. return session
  919. }),
  920. )
  921. .at((ctx) => ({
  922. path: route("/session/{sessionID}/command", { sessionID: ctx.state.id }),
  923. headers: ctx.headers(),
  924. body: { command: "init", arguments: "", model: "test/test-model" },
  925. }))
  926. .jsonEffect(
  927. 200,
  928. (body, ctx) =>
  929. Effect.gen(function* () {
  930. object(body)
  931. check(isRecord(body.info) && body.info.role === "assistant", "command should return assistant message")
  932. yield* ctx.llmWait(1)
  933. }),
  934. "status",
  935. ),
  936. http
  937. .post("/session/{sessionID}/shell", "session.shell")
  938. .preserveDatabase()
  939. .mutating()
  940. .seeded((ctx) => ctx.session({ title: "Shell session" }))
  941. .at((ctx) => ({
  942. path: route("/session/{sessionID}/shell", { sessionID: ctx.state.id }),
  943. headers: ctx.headers(),
  944. body: { agent: "build", model: { providerID: "test", modelID: "test-model" }, command: "printf shell-ok" },
  945. }))
  946. .json(
  947. 200,
  948. (body) => {
  949. object(body)
  950. check(isRecord(body.info) && body.info.role === "assistant", "shell should return assistant message")
  951. check(
  952. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.type === "tool"),
  953. "shell should return a tool part",
  954. )
  955. },
  956. "status",
  957. ),
  958. http
  959. .post("/session/{sessionID}/summarize", "session.summarize")
  960. .preserveDatabase()
  961. .withLlm()
  962. .seeded((ctx) =>
  963. Effect.gen(function* () {
  964. const session = yield* ctx.session({ title: "Summarize session" })
  965. yield* ctx.message(session.id, { text: "summarize this work" })
  966. const summary = [
  967. "## Goal",
  968. "- Exercise session summarize.",
  969. "",
  970. "## Constraints & Preferences",
  971. "- Use fake LLM.",
  972. "",
  973. "## Progress",
  974. "### Done",
  975. "- Summary generated.",
  976. "",
  977. "### In Progress",
  978. "- (none)",
  979. "",
  980. "### Blocked",
  981. "- (none)",
  982. "",
  983. "## Key Decisions",
  984. "- Keep route local.",
  985. "",
  986. "## Next Steps",
  987. "- (none)",
  988. "",
  989. "## Critical Context",
  990. "- Test fixture.",
  991. "",
  992. "## Relevant Files",
  993. "- test/server/httpapi-exercise/index.ts: scenario",
  994. ].join("\n")
  995. yield* ctx.llmText(summary)
  996. yield* ctx.llmText(summary)
  997. return session
  998. }),
  999. )
  1000. .at((ctx) => ({
  1001. path: route("/session/{sessionID}/summarize", { sessionID: ctx.state.id }),
  1002. headers: ctx.headers(),
  1003. body: { providerID: "test", modelID: "test-model", auto: false },
  1004. }))
  1005. .jsonEffect(
  1006. 200,
  1007. (body, ctx) =>
  1008. Effect.gen(function* () {
  1009. check(body === true, "summarize should return true")
  1010. const messages = yield* ctx.messages(ctx.state.id)
  1011. check(
  1012. messages.some((message) => message.info.role === "assistant" && message.info.summary === true),
  1013. "summarize should create a summary assistant message",
  1014. )
  1015. yield* ctx.llmWait(1)
  1016. }),
  1017. "status",
  1018. ),
  1019. http
  1020. .post("/session/{sessionID}/revert", "session.revert")
  1021. .mutating()
  1022. .seeded((ctx) =>
  1023. Effect.gen(function* () {
  1024. const session = yield* ctx.session({ title: "Revert session" })
  1025. const message = yield* ctx.message(session.id, { text: "revert me" })
  1026. return { session, message }
  1027. }),
  1028. )
  1029. .at((ctx) => ({
  1030. path: route("/session/{sessionID}/revert", { sessionID: ctx.state.session.id }),
  1031. headers: ctx.headers(),
  1032. body: { messageID: ctx.state.message.info.id },
  1033. }))
  1034. .json(
  1035. 200,
  1036. (body, ctx) => {
  1037. object(body)
  1038. check(body.id === ctx.state.session.id, "revert should return the session")
  1039. check(
  1040. isRecord(body.revert) && body.revert.messageID === ctx.state.message.info.id,
  1041. "revert should record reverted message",
  1042. )
  1043. },
  1044. "status",
  1045. ),
  1046. http
  1047. .post("/session/{sessionID}/unrevert", "session.unrevert")
  1048. .mutating()
  1049. .seeded((ctx) => ctx.session({ title: "Unrevert session" }))
  1050. .at((ctx) => ({
  1051. path: route("/session/{sessionID}/unrevert", { sessionID: ctx.state.id }),
  1052. headers: ctx.headers(),
  1053. }))
  1054. .json(
  1055. 200,
  1056. (body, ctx) => {
  1057. object(body)
  1058. check(body.id === ctx.state.id, "unrevert should return the session")
  1059. },
  1060. "status",
  1061. ),
  1062. http
  1063. .post("/session/{sessionID}/permissions/{permissionID}", "permission.respond")
  1064. .seeded((ctx) => ctx.session({ title: "Deprecated permission session" }))
  1065. .at((ctx) => ({
  1066. path: route("/session/{sessionID}/permissions/{permissionID}", {
  1067. sessionID: ctx.state.id,
  1068. permissionID: "per_httpapi_deprecated",
  1069. }),
  1070. headers: ctx.headers(),
  1071. body: { response: "once" },
  1072. }))
  1073. .json(200, (body) => {
  1074. check(body === true, "deprecated permission response should return true")
  1075. }),
  1076. http
  1077. .post("/session/{sessionID}/share", "session.share")
  1078. .mutating()
  1079. .seeded((ctx) => ctx.session({ title: "Share session" }))
  1080. .at((ctx) => ({ path: route("/session/{sessionID}/share", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1081. .json(
  1082. 200,
  1083. (body, ctx) => {
  1084. object(body)
  1085. check(body.id === ctx.state.id, "share should return the session")
  1086. },
  1087. "status",
  1088. ),
  1089. http
  1090. .delete("/session/{sessionID}/share", "session.unshare")
  1091. .mutating()
  1092. .seeded((ctx) => ctx.session({ title: "Unshare session" }))
  1093. .at((ctx) => ({ path: route("/session/{sessionID}/share", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1094. .json(
  1095. 200,
  1096. (body, ctx) => {
  1097. object(body)
  1098. check(body.id === ctx.state.id, "unshare should return the session")
  1099. },
  1100. "status",
  1101. ),
  1102. http
  1103. .post("/tui/append-prompt", "tui.appendPrompt")
  1104. .at((ctx) => ({ path: "/tui/append-prompt", headers: ctx.headers(), body: { text: "hello" } }))
  1105. .json(200, boolean, "status"),
  1106. http
  1107. .post("/tui/select-session", "tui.selectSession.invalid")
  1108. .at((ctx) => ({ path: "/tui/select-session", headers: ctx.headers(), body: { sessionID: "invalid" } }))
  1109. .status(400),
  1110. http.post("/tui/open-help", "tui.openHelp").json(200, boolean, "status"),
  1111. http.post("/tui/open-sessions", "tui.openSessions").json(200, boolean, "status"),
  1112. http.post("/tui/open-themes", "tui.openThemes").json(200, boolean, "status"),
  1113. http.post("/tui/open-models", "tui.openModels").json(200, boolean, "status"),
  1114. http.post("/tui/submit-prompt", "tui.submitPrompt").json(200, boolean, "status"),
  1115. http.post("/tui/clear-prompt", "tui.clearPrompt").json(200, boolean, "status"),
  1116. http
  1117. .post("/tui/execute-command", "tui.executeCommand")
  1118. .at((ctx) => ({ path: "/tui/execute-command", headers: ctx.headers(), body: { command: "agent_cycle" } }))
  1119. .json(200, boolean, "status"),
  1120. http
  1121. .post("/tui/show-toast", "tui.showToast")
  1122. .at((ctx) => ({
  1123. path: "/tui/show-toast",
  1124. headers: ctx.headers(),
  1125. body: { title: "Exercise", message: "covered", variant: "info", duration: 1000 },
  1126. }))
  1127. .json(200, boolean, "status"),
  1128. http
  1129. .post("/tui/publish", "tui.publish")
  1130. .at((ctx) => ({
  1131. path: "/tui/publish",
  1132. headers: ctx.headers(),
  1133. body: { type: "tui.prompt.append", properties: { text: "published" } },
  1134. }))
  1135. .json(200, boolean, "status"),
  1136. http
  1137. .post("/tui/select-session", "tui.selectSession")
  1138. .seeded((ctx) => ctx.session({ title: "TUI select" }))
  1139. .at((ctx) => ({ path: "/tui/select-session", headers: ctx.headers(), body: { sessionID: ctx.state.id } }))
  1140. .json(200, boolean, "status"),
  1141. http
  1142. .post("/tui/control/response", "tui.control.response")
  1143. .at((ctx) => ({ path: "/tui/control/response", headers: ctx.headers(), body: { ok: true } }))
  1144. .json(200, boolean, "status"),
  1145. http
  1146. .get("/tui/control/next", "tui.control.next")
  1147. .mutating()
  1148. .seeded((ctx) => ctx.tuiRequest({ path: "/tui/exercise", body: { text: "queued" } }))
  1149. .json(
  1150. 200,
  1151. (body) => {
  1152. object(body)
  1153. check(body.path === "/tui/exercise", "control next should return queued path")
  1154. object(body.body)
  1155. check(body.body.text === "queued", "control next should return queued body")
  1156. },
  1157. "status",
  1158. ),
  1159. http
  1160. .post("/global/upgrade", "global.upgrade")
  1161. .global()
  1162. .at(() => ({ path: "/global/upgrade", body: { target: 1 } }))
  1163. .status(400),
  1164. ]
  1165. const main = Effect.gen(function* () {
  1166. yield* Effect.addFinalizer(() => cleanupExercisePaths)
  1167. const options = parseOptions(Bun.argv.slice(2))
  1168. const modules = yield* Effect.promise(() => runtime())
  1169. const effectRoutes = routeKeys(OpenApi.fromApi(modules.PublicApi))
  1170. const honoRoutes = routeKeys(yield* Effect.promise(() => modules.Server.openapiHono()))
  1171. const selected = scenarios.filter((scenario) => matches(options, scenario))
  1172. const missing = effectRoutes.filter((route) => !scenarios.some((scenario) => route === routeKey(scenario)))
  1173. const extra = scenarios.filter((scenario) => !effectRoutes.includes(routeKey(scenario)))
  1174. printHeader(options, effectRoutes, honoRoutes, selected, missing, extra, {
  1175. database: exerciseDatabasePath,
  1176. global: exerciseGlobalRoot,
  1177. })
  1178. const results =
  1179. options.mode === "coverage"
  1180. ? selected.map(coverageResult)
  1181. : yield* Effect.forEach(selected, runScenario(options), { concurrency: 1 })
  1182. printResults(results, missing, extra)
  1183. if (results.some((result) => result.status === "fail"))
  1184. return yield* Effect.fail(new Error("one or more scenarios failed"))
  1185. if (options.failOnSkip && results.some((result) => result.status === "skip"))
  1186. return yield* Effect.fail(new Error("one or more scenarios are skipped"))
  1187. if (options.failOnMissing && missing.length > 0)
  1188. return yield* Effect.fail(new Error("one or more routes have no scenario"))
  1189. return undefined
  1190. })
  1191. Effect.runPromise(main.pipe(Effect.provide(TestLLMServer.layer), Effect.scoped)).then(
  1192. () => process.exit(0),
  1193. (error: unknown) => {
  1194. console.error(`${color.red}${message(error)}${color.reset}`)
  1195. process.exit(1)
  1196. },
  1197. )