index.ts 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. /**
  2. * End-to-end exerciser for the Effect HttpApi routes.
  3. *
  4. * The goal is not to be a normal unit test file. This is a route-coverage harness:
  5. * every public route should have a small scenario that proves the route decodes
  6. * requests, uses the right instance context, mutates storage when expected, and
  7. * returns the expected 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.protected.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 the runner to reset isolated state after destructive routes.
  19. */
  20. import { Effect } from "effect"
  21. import { OpenApi } from "effect/unstable/httpapi"
  22. import { TestLLMServer } from "../../lib/llm-server"
  23. import path from "path"
  24. import { array, boolean, check, isRecord, message, object, stable } from "./assertions"
  25. import { controlledPtyInput, http, route } from "./dsl"
  26. import {
  27. cleanupExercisePaths,
  28. exerciseConfigDirectory,
  29. exerciseDataDirectory,
  30. exerciseDatabasePath,
  31. exerciseGlobalRoot,
  32. } from "./environment"
  33. import { color, printHeader, printResults } from "./report"
  34. import { coverageResult, parseOptions, routeKey, routeKeys, selectedScenarios } from "./routing"
  35. import { runScenario } from "./runner"
  36. import { disposeApps } from "./backend"
  37. import { runtime } from "./runtime"
  38. import { type Scenario } from "./types"
  39. function cursor(input: Record<string, unknown>) {
  40. return Buffer.from(JSON.stringify(input)).toString("base64url")
  41. }
  42. function data(validate: (value: any) => void) {
  43. return (body: any) => {
  44. object(body)
  45. validate(body.data)
  46. }
  47. }
  48. function locationData(validate: (value: any) => void) {
  49. return (body: any) => {
  50. object(body)
  51. object(body.location)
  52. object(body.location.project)
  53. validate(body.data)
  54. }
  55. }
  56. const scenarios: Scenario[] = [
  57. http.protected
  58. .get("/global/health", "global.health")
  59. .global()
  60. .json(200, (body) => {
  61. object(body)
  62. check(body.healthy === true, "server should report healthy")
  63. }),
  64. http.protected
  65. .get("/global/event", "global.event")
  66. .global()
  67. .stream()
  68. .status(
  69. 200,
  70. (_ctx, result) =>
  71. Effect.sync(() => {
  72. check(result.contentType.includes("text/event-stream"), "global event should be an SSE stream")
  73. check(result.text.includes("server.connected"), "global event should emit initial connection event")
  74. }),
  75. "status",
  76. ),
  77. http.protected.get("/global/config", "global.config.get").global().json(),
  78. http.protected
  79. .patch("/global/config", "global.config.update")
  80. .global()
  81. .seeded(() =>
  82. Effect.promise(() =>
  83. Bun.write(
  84. path.join(exerciseConfigDirectory, "opencode.jsonc"),
  85. JSON.stringify({ username: "httpapi-global" }, null, 2),
  86. ),
  87. ),
  88. )
  89. .at(() => ({ path: "/global/config", body: { username: "httpapi-global" } }))
  90. .jsonEffect(
  91. 200,
  92. (body) =>
  93. Effect.gen(function* () {
  94. object(body)
  95. check(body.username === "httpapi-global", "global config update should return patched config")
  96. const text = yield* Effect.promise(() =>
  97. Bun.file(path.join(exerciseConfigDirectory, "opencode.jsonc")).text(),
  98. )
  99. check(text.includes('"username": "httpapi-global"'), "global config update should write isolated config file")
  100. }),
  101. "status",
  102. ),
  103. http.protected
  104. .post("/global/dispose", "global.dispose")
  105. .global()
  106. .mutating()
  107. .json(
  108. 200,
  109. (body) => {
  110. check(body === true, "global dispose should return true")
  111. },
  112. "status",
  113. ),
  114. http.protected.get("/path", "path.get").json(200, (body, ctx) => {
  115. object(body)
  116. check(body.directory === ctx.directory, "directory should resolve from x-opencode-directory")
  117. check(body.worktree === ctx.directory, "worktree should resolve from x-opencode-directory")
  118. }),
  119. http.protected.get("/vcs", "vcs.get").json(),
  120. http.protected.get("/vcs/status", "vcs.status").json(200, array),
  121. http.protected
  122. .get("/vcs/diff", "vcs.diff")
  123. .at((ctx) => ({ path: "/vcs/diff?mode=git", headers: ctx.headers() }))
  124. .json(200, array),
  125. http.protected.get("/vcs/diff/raw", "vcs.diff.raw").status(
  126. 200,
  127. (_ctx, result) =>
  128. Effect.sync(() => {
  129. check(typeof result.text === "string", "raw VCS diff should return text")
  130. }),
  131. "status",
  132. ),
  133. http.protected
  134. .post("/vcs/apply", "vcs.apply")
  135. .inProject({ git: false })
  136. .at((ctx) => ({ path: "/vcs/apply", headers: ctx.headers(), body: { patch: "" } }))
  137. .status(400, undefined, "status"),
  138. http.protected.get("/command", "command.list").json(200, array, "status"),
  139. http.protected.get("/agent", "app.agents").json(200, array, "status"),
  140. http.protected.get("/skill", "app.skills").json(200, array, "status"),
  141. http.protected.get("/lsp", "lsp.status").json(200, array),
  142. http.protected.get("/formatter", "formatter.status").json(200, array),
  143. http.protected.get("/config", "config.get").json(200, undefined, "status"),
  144. http.protected
  145. .patch("/config", "config.update")
  146. .mutating()
  147. .at((ctx) => ({ path: "/config", headers: ctx.headers(), body: { username: "httpapi-local" } }))
  148. .json(
  149. 200,
  150. (body) => {
  151. object(body)
  152. check(body.username === "httpapi-local", "local config update should return patched config")
  153. },
  154. "status",
  155. ),
  156. http.protected
  157. .patch("/config", "config.update.invalid")
  158. .at((ctx) => ({ path: "/config", headers: ctx.headers(), body: { username: 1 } }))
  159. .status(400),
  160. http.protected.get("/config/providers", "config.providers").json(),
  161. http.protected.get("/project", "project.list").json(200, array, "status"),
  162. http.protected.get("/project/current", "project.current").json(
  163. 200,
  164. (body, ctx) => {
  165. object(body)
  166. check(body.worktree === ctx.directory, "current project should resolve from scenario directory")
  167. },
  168. "status",
  169. ),
  170. http.protected
  171. .patch("/project/{projectID}", "project.update")
  172. .mutating()
  173. .seeded((ctx) => ctx.project())
  174. .at((ctx) => ({
  175. path: route("/project/{projectID}", { projectID: ctx.state.id }),
  176. headers: ctx.headers(),
  177. body: { name: "HTTP API Project", commands: { start: "bun --version" } },
  178. }))
  179. .json(
  180. 200,
  181. (body) => {
  182. object(body)
  183. check(body.name === "HTTP API Project", "project update should return patched name")
  184. check(
  185. isRecord(body.commands) && body.commands.start === "bun --version",
  186. "project update should return patched command",
  187. )
  188. },
  189. "status",
  190. ),
  191. http.protected
  192. .patch("/project/{projectID}", "project.update.missing")
  193. .mutating()
  194. .at((ctx) => ({
  195. path: route("/project/{projectID}", { projectID: "project_httpapi_missing" }),
  196. headers: ctx.headers(),
  197. body: { name: "Missing Project" },
  198. }))
  199. .json(404, object, "status"),
  200. http.protected
  201. .post("/project/git/init", "project.initGit")
  202. .mutating()
  203. .inProject({ git: false })
  204. .json(
  205. 200,
  206. (body, ctx) => {
  207. object(body)
  208. check(body.worktree === ctx.directory, "git init should return current project")
  209. check(body.vcs === "git", "git init should mark the project as git-backed")
  210. },
  211. "status",
  212. ),
  213. http.protected
  214. .get("/project/{projectID}/directories", "project.directories")
  215. .seeded((ctx) => ctx.project())
  216. .at((ctx) => ({
  217. path: route("/project/{projectID}/directories", { projectID: ctx.state.id }),
  218. headers: ctx.headers(),
  219. }))
  220. .json(200, array, "status"),
  221. http.protected
  222. .post("/experimental/project/{projectID}/copy", "experimental.projectCopy.create")
  223. .seeded((ctx) => ctx.project())
  224. .at((ctx) => ({
  225. path: route("/experimental/project/{projectID}/copy", { projectID: ctx.state.id }),
  226. headers: ctx.headers(),
  227. body: {},
  228. }))
  229. .status(400),
  230. http.protected
  231. .delete("/experimental/project/{projectID}/copy", "experimental.projectCopy.remove")
  232. .seeded((ctx) => ctx.project())
  233. .at((ctx) => ({
  234. path: route("/experimental/project/{projectID}/copy", { projectID: ctx.state.id }),
  235. headers: ctx.headers(),
  236. body: {},
  237. }))
  238. .status(400),
  239. http.protected
  240. .post("/experimental/project/{projectID}/copy/refresh", "experimental.projectCopy.refresh")
  241. .mutating()
  242. .seeded((ctx) => ctx.project())
  243. .at((ctx) => ({
  244. path: route("/experimental/project/{projectID}/copy/refresh", { projectID: ctx.state.id }),
  245. headers: ctx.headers(),
  246. }))
  247. .status(204, undefined, "status"),
  248. http.protected.get("/provider", "provider.list").json(),
  249. http.protected.get("/provider/auth", "provider.auth").json(),
  250. http.protected
  251. .post("/provider/{providerID}/oauth/authorize", "provider.oauth.authorize")
  252. .at((ctx) => ({
  253. path: route("/provider/{providerID}/oauth/authorize", { providerID: "httpapi" }),
  254. headers: ctx.headers(),
  255. body: { method: "bad" },
  256. }))
  257. .status(400),
  258. http.protected
  259. .post("/provider/{providerID}/oauth/callback", "provider.oauth.callback")
  260. .at((ctx) => ({
  261. path: route("/provider/{providerID}/oauth/callback", { providerID: "httpapi" }),
  262. headers: ctx.headers(),
  263. body: { method: "bad" },
  264. }))
  265. .status(400),
  266. http.protected.get("/permission", "permission.list").json(200, array),
  267. http.protected
  268. .post("/permission/{requestID}/reply", "permission.reply.invalid")
  269. .at((ctx) => ({
  270. path: route("/permission/{requestID}/reply", { requestID: "per_httpapi" }),
  271. headers: ctx.headers(),
  272. body: { reply: "bad" },
  273. }))
  274. .status(400),
  275. http.protected
  276. .post("/permission/{requestID}/reply", "permission.reply")
  277. .at((ctx) => ({
  278. path: route("/permission/{requestID}/reply", { requestID: "per_httpapi" }),
  279. headers: ctx.headers(),
  280. body: { reply: "once" },
  281. }))
  282. .json(404, object, "status"),
  283. http.protected.get("/question", "question.list").json(200, array),
  284. http.protected
  285. .post("/question/{requestID}/reply", "question.reply.invalid")
  286. .at((ctx) => ({
  287. path: route("/question/{requestID}/reply", { requestID: "que_httpapi_reply" }),
  288. headers: ctx.headers(),
  289. body: { answers: "Yes" },
  290. }))
  291. .status(400),
  292. http.protected
  293. .post("/question/{requestID}/reply", "question.reply")
  294. .at((ctx) => ({
  295. path: route("/question/{requestID}/reply", { requestID: "que_httpapi_reply" }),
  296. headers: ctx.headers(),
  297. body: { answers: [["Yes"]] },
  298. }))
  299. .json(404, object, "status"),
  300. http.protected
  301. .post("/question/{requestID}/reject", "question.reject")
  302. .at((ctx) => ({
  303. path: route("/question/{requestID}/reject", { requestID: "que_httpapi_reject" }),
  304. headers: ctx.headers(),
  305. }))
  306. .json(404, object, "status"),
  307. http.protected
  308. .get("/file", "file.list")
  309. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  310. .at((ctx) => ({ path: `/file?${new URLSearchParams({ path: "." })}`, headers: ctx.headers() }))
  311. .json(200, array),
  312. http.protected
  313. .get("/file/content", "file.read")
  314. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  315. .at((ctx) => ({ path: `/file/content?${new URLSearchParams({ path: "hello.txt" })}`, headers: ctx.headers() }))
  316. .json(200, (body) => {
  317. object(body)
  318. check(body.content === "hello", `content should match seeded file: ${JSON.stringify(body)}`)
  319. }),
  320. http.protected
  321. .get("/file/content", "file.read.missing")
  322. .at((ctx) => ({ path: `/file/content?${new URLSearchParams({ path: "missing.txt" })}`, headers: ctx.headers() }))
  323. .json(200, (body) => {
  324. object(body)
  325. check(body.type === "text" && body.content === "", "missing file content should return an empty text result")
  326. }),
  327. http.protected.get("/file/status", "file.status").json(200, array),
  328. http.protected
  329. .get("/find", "find.text")
  330. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  331. .at((ctx) => ({ path: `/find?${new URLSearchParams({ pattern: "hello" })}`, headers: ctx.headers() }))
  332. .json(200, array),
  333. http.protected
  334. .get("/find/file", "find.files")
  335. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  336. .at((ctx) => ({
  337. path: `/find/file?${new URLSearchParams({ query: "hello", dirs: "false" })}`,
  338. headers: ctx.headers(),
  339. }))
  340. .json(200, array),
  341. http.protected
  342. .get("/find/symbol", "find.symbols")
  343. .seeded((ctx) => ctx.file("hello.ts", "export const hello = 1\n"))
  344. .at((ctx) => ({ path: `/find/symbol?${new URLSearchParams({ query: "hello" })}`, headers: ctx.headers() }))
  345. .json(200, array),
  346. http.protected
  347. .get("/event", "event.stream")
  348. .stream()
  349. .status(
  350. 200,
  351. (_ctx, result) =>
  352. Effect.sync(() => {
  353. check(result.contentType.includes("text/event-stream"), "event should be an SSE stream")
  354. check(result.text.includes("server.connected"), "event should emit initial connection event")
  355. }),
  356. "status",
  357. ),
  358. http.protected.get("/mcp", "mcp.status").json(),
  359. http.protected
  360. .post("/mcp", "mcp.add")
  361. .mutating()
  362. .at((ctx) => ({
  363. path: "/mcp",
  364. headers: ctx.headers(),
  365. body: { name: "httpapi-disabled", config: { type: "local", command: ["bun", "--version"], enabled: false } },
  366. }))
  367. .json(
  368. 200,
  369. (body) => {
  370. object(body)
  371. object(body["httpapi-disabled"])
  372. check(body["httpapi-disabled"].status === "disabled", "disabled MCP server should be added without spawning")
  373. },
  374. "status",
  375. ),
  376. http.protected
  377. .post("/mcp", "mcp.add.invalid")
  378. .at((ctx) => ({
  379. path: "/mcp",
  380. headers: ctx.headers(),
  381. body: { name: "httpapi-invalid", config: { type: "invalid" } },
  382. }))
  383. .status(400),
  384. http.protected
  385. .post("/mcp/{name}/auth", "mcp.auth.start")
  386. .at((ctx) => ({ path: route("/mcp/{name}/auth", { name: "httpapi-missing" }), headers: ctx.headers() }))
  387. .json(404, object, "status"),
  388. http.protected
  389. .delete("/mcp/{name}/auth", "mcp.auth.remove")
  390. .mutating()
  391. .at((ctx) => ({ path: route("/mcp/{name}/auth", { name: "httpapi-missing" }), headers: ctx.headers() }))
  392. .json(404, object, "status"),
  393. http.protected
  394. .post("/mcp/{name}/auth/authenticate", "mcp.auth.authenticate")
  395. .at((ctx) => ({
  396. path: route("/mcp/{name}/auth/authenticate", { name: "httpapi-missing" }),
  397. headers: ctx.headers(),
  398. }))
  399. .json(404, object, "status"),
  400. http.protected
  401. .post("/mcp/{name}/auth/callback", "mcp.auth.callback")
  402. .at((ctx) => ({
  403. path: route("/mcp/{name}/auth/callback", { name: "httpapi-missing" }),
  404. headers: ctx.headers(),
  405. body: { code: "code" },
  406. }))
  407. .json(404, object, "status"),
  408. http.protected
  409. .post("/mcp/{name}/connect", "mcp.connect")
  410. .mutating()
  411. .at((ctx) => ({ path: route("/mcp/{name}/connect", { name: "httpapi-missing" }), headers: ctx.headers() }))
  412. .json(404, object, "status"),
  413. http.protected
  414. .post("/mcp/{name}/disconnect", "mcp.disconnect")
  415. .mutating()
  416. .at((ctx) => ({ path: route("/mcp/{name}/disconnect", { name: "httpapi-missing" }), headers: ctx.headers() }))
  417. .json(404, object, "status"),
  418. http.protected.get("/pty/shells", "pty.shells").json(200, array),
  419. http.protected.get("/pty", "pty.list").json(200, array),
  420. http.protected
  421. .post("/pty", "pty.create")
  422. .mutating()
  423. .at((ctx) => ({ path: "/pty", headers: ctx.headers(), body: controlledPtyInput("HTTP API PTY") }))
  424. .json(
  425. 200,
  426. (body, ctx) => {
  427. object(body)
  428. check(body.title === "HTTP API PTY", "PTY create should return requested title")
  429. check(body.command === "/bin/sh", "PTY create should use controlled shell command")
  430. check(body.cwd === ctx.directory, "PTY create should default cwd to scenario directory")
  431. },
  432. "status",
  433. ),
  434. http.protected
  435. .post("/pty", "pty.create.invalid")
  436. .at((ctx) => ({ path: "/pty", headers: ctx.headers(), body: { command: 1 } }))
  437. .status(400),
  438. http.protected
  439. .post("/pty/{ptyID}/connect-token", "pty.connectToken.invalid")
  440. .at((ctx) => ({
  441. path: route("/pty/{ptyID}/connect-token", { ptyID: "pty_httpapi_missing" }),
  442. headers: ctx.headers(),
  443. }))
  444. .status(403, undefined, "status"),
  445. http.protected
  446. .get("/pty/{ptyID}", "pty.get")
  447. .at((ctx) => ({ path: route("/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  448. .status(404),
  449. http.protected
  450. .put("/pty/{ptyID}", "pty.update")
  451. .mutating()
  452. .at((ctx) => ({
  453. path: route("/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }),
  454. headers: ctx.headers(),
  455. body: { size: { rows: 0, cols: 0 } },
  456. }))
  457. .status(400),
  458. http.protected
  459. .delete("/pty/{ptyID}", "pty.remove")
  460. .mutating()
  461. .at((ctx) => ({ path: route("/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  462. .json(404, object, "status"),
  463. http.protected
  464. .get("/pty/{ptyID}/connect", "pty.connect")
  465. .at((ctx) => ({ path: route("/pty/{ptyID}/connect", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  466. .status(404, undefined, "none"),
  467. http.protected.get("/experimental/console", "experimental.console.get").json(),
  468. http.protected.get("/experimental/console/orgs", "experimental.console.listOrgs").json(),
  469. http.protected
  470. .post("/experimental/console/switch", "experimental.console.switchOrg")
  471. .at((ctx) => ({
  472. path: "/experimental/console/switch",
  473. headers: ctx.headers(),
  474. body: { accountID: "httpapi-account", orgID: "httpapi-org" },
  475. }))
  476. .status(400, undefined, "none"),
  477. http.protected.get("/experimental/workspace/adapter", "experimental.workspace.adapter.list").json(200, array),
  478. http.protected.get("/experimental/workspace", "experimental.workspace.list").json(200, array),
  479. http.protected.get("/experimental/workspace/status", "experimental.workspace.status").json(200, array),
  480. http.protected
  481. .post("/experimental/workspace", "experimental.workspace.create")
  482. .at((ctx) => ({ path: "/experimental/workspace", headers: ctx.headers(), body: {} }))
  483. .status(400),
  484. http.protected
  485. .post("/experimental/workspace/sync-list", "experimental.workspace.syncList")
  486. .status(204, undefined, "status"),
  487. http.protected
  488. .delete("/experimental/workspace/{id}", "experimental.workspace.remove")
  489. .mutating()
  490. .at((ctx) => ({
  491. path: route("/experimental/workspace/{id}", { id: "wrk_httpapi_missing" }),
  492. headers: ctx.headers(),
  493. }))
  494. .status(200),
  495. http.protected
  496. .post("/experimental/workspace/warp", "experimental.workspace.warp")
  497. .at((ctx) => ({
  498. path: "/experimental/workspace/warp",
  499. headers: ctx.headers(),
  500. body: {},
  501. }))
  502. .status(400),
  503. http.protected
  504. .post("/experimental/control-plane/move-session", "experimental.controlPlane.moveSession")
  505. .global()
  506. .at(() => ({
  507. path: "/experimental/control-plane/move-session",
  508. body: {},
  509. }))
  510. .status(400),
  511. http.protected
  512. .get("/experimental/tool", "tool.list")
  513. .at((ctx) => ({
  514. path: `/experimental/tool?${new URLSearchParams({ provider: "opencode", model: "test" })}`,
  515. headers: ctx.headers(),
  516. }))
  517. .json(200, array, "status"),
  518. http.protected.get("/experimental/tool/ids", "tool.ids").json(200, array),
  519. http.protected.get("/experimental/worktree", "worktree.list").json(200, array),
  520. http.protected
  521. .post("/experimental/worktree", "worktree.create")
  522. .mutating()
  523. .at((ctx) => ({ path: "/experimental/worktree", headers: ctx.headers(), body: { name: "api-dsl" } }))
  524. .jsonEffect(
  525. 200,
  526. (body, ctx) =>
  527. Effect.gen(function* () {
  528. object(body)
  529. check(typeof body.directory === "string", "created worktree should include directory")
  530. yield* ctx.worktreeRemove(body.directory)
  531. }),
  532. "status",
  533. ),
  534. http.protected
  535. .post("/experimental/worktree", "worktree.create.invalid")
  536. .at((ctx) => ({ path: "/experimental/worktree", headers: ctx.headers(), body: { name: 1 } }))
  537. .status(400),
  538. http.protected
  539. .delete("/experimental/worktree", "worktree.remove")
  540. .mutating()
  541. .seeded((ctx) => ctx.worktree({ name: "api-remove" }))
  542. .at((ctx) => ({ path: "/experimental/worktree", headers: ctx.headers(), body: { directory: ctx.state.directory } }))
  543. .json(200, (body) => {
  544. check(body === true, "worktree remove should return true")
  545. }),
  546. http.protected
  547. .post("/experimental/worktree/reset", "worktree.reset")
  548. .mutating()
  549. .seeded((ctx) => ctx.worktree({ name: "api-reset" }))
  550. .at((ctx) => ({
  551. path: "/experimental/worktree/reset",
  552. headers: ctx.headers(),
  553. body: { directory: ctx.state.directory },
  554. }))
  555. .jsonEffect(200, (body, ctx) =>
  556. Effect.gen(function* () {
  557. check(body === true, "worktree reset should return true")
  558. yield* ctx.worktreeRemove(ctx.state.directory)
  559. }),
  560. ),
  561. http.protected
  562. .get("/experimental/session", "experimental.session.list")
  563. .at((ctx) => ({ path: "/experimental/session?roots=false&archived=false", headers: ctx.headers() }))
  564. .json(200, array),
  565. http.protected
  566. .post("/experimental/session/{sessionID}/background", "experimental.session.background")
  567. .mutating()
  568. .seeded((ctx) => ctx.session({ title: "Background route owner" }))
  569. .at((ctx) => ({
  570. path: route("/experimental/session/{sessionID}/background", { sessionID: ctx.state.id }),
  571. headers: ctx.headers(),
  572. }))
  573. .json(200, (body) => {
  574. check(body === false, "background route should be a no-op without running subagents")
  575. }),
  576. http.protected.get("/experimental/resource", "experimental.resource.list").json(),
  577. http.protected
  578. .post("/sync/history", "sync.history.list")
  579. .at((ctx) => ({ path: "/sync/history", headers: ctx.headers(), body: {} }))
  580. .json(200, array),
  581. http.protected
  582. .post("/sync/replay", "sync.replay")
  583. .at((ctx) => ({ path: "/sync/replay", headers: ctx.headers(), body: { directory: ctx.directory, events: [] } }))
  584. .status(400),
  585. http.protected
  586. .post("/sync/steal", "sync.steal.invalid")
  587. .at((ctx) => ({ path: "/sync/steal", headers: ctx.headers(), body: {} }))
  588. .status(400, undefined, "status"),
  589. http.protected
  590. .post("/sync/start", "sync.start")
  591. .mutating()
  592. .preserveDatabase()
  593. .json(200, (body) => {
  594. check(body === true, "sync start should return true when no workspace sessions exist")
  595. }),
  596. http.protected
  597. .post("/instance/dispose", "instance.dispose")
  598. .mutating()
  599. .json(200, (body) => {
  600. check(body === true, "instance dispose should return true")
  601. }),
  602. http.protected
  603. .post("/log", "app.log")
  604. .global()
  605. .at(() => ({ path: "/log", body: { service: "httpapi-exercise", level: "info", message: "route coverage" } }))
  606. .json(200, (body) => {
  607. check(body === true, "log route should return true")
  608. }),
  609. http.protected
  610. .put("/auth/{providerID}", "auth.set")
  611. .global()
  612. .at(() => ({ path: route("/auth/{providerID}", { providerID: "test" }), body: { type: "api", key: "test-key" } }))
  613. .jsonEffect(200, (body) =>
  614. Effect.gen(function* () {
  615. check(body === true, "auth set should return true")
  616. const auth = yield* Effect.promise(() => Bun.file(path.join(exerciseDataDirectory, "auth.json")).json())
  617. object(auth)
  618. check(isRecord(auth.test) && auth.test.key === "test-key", "auth set should write isolated auth file")
  619. }),
  620. ),
  621. http.protected
  622. .delete("/auth/{providerID}", "auth.remove")
  623. .global()
  624. .seeded(() =>
  625. Effect.promise(() =>
  626. Bun.write(
  627. path.join(exerciseDataDirectory, "auth.json"),
  628. JSON.stringify({ test: { type: "api", key: "remove-me" } }),
  629. ),
  630. ),
  631. )
  632. .at(() => ({ path: route("/auth/{providerID}", { providerID: "test" }) }))
  633. .jsonEffect(200, (body) =>
  634. Effect.gen(function* () {
  635. check(body === true, "auth remove should return true")
  636. const auth = yield* Effect.promise(() => Bun.file(path.join(exerciseDataDirectory, "auth.json")).json())
  637. object(auth)
  638. check(auth.test === undefined, "auth remove should delete provider from isolated auth file")
  639. }),
  640. ),
  641. http.protected.get("/api/health", "v2.health.get").json(200, (body) => {
  642. object(body)
  643. check(body.healthy === true, "v2 server should report healthy")
  644. }),
  645. http.protected.get("/api/location", "v2.location.get").json(200, object),
  646. http.protected.get("/api/agent", "v2.agent.list").json(200, locationData(array)),
  647. http.protected.get("/api/model", "v2.model.list").json(200, locationData(array)),
  648. http.protected.get("/api/provider", "v2.provider.list").json(200, locationData(array)),
  649. http.protected.get("/api/command", "v2.command.list").json(200, locationData(array)),
  650. http.protected.get("/api/skill", "v2.skill.list").json(200, locationData(array)),
  651. http.protected
  652. .get("/api/event", "v2.event.subscribe")
  653. .stream()
  654. .status(
  655. 200,
  656. (ctx, result) =>
  657. Effect.sync(() => {
  658. check(result.contentType.includes("text/event-stream"), "v2 event should be an SSE stream")
  659. check(result.text.includes("server.connected"), "v2 event should emit initial connection event")
  660. check(!!ctx.directory && result.text.includes(ctx.directory), "v2 event should include the resolved location")
  661. }),
  662. "status",
  663. ),
  664. http.protected
  665. .get("/api/fs/read", "v2.fs.read")
  666. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  667. .at((ctx) => ({ path: "/api/fs/read?path=hello.txt", headers: ctx.headers() }))
  668. .json(200, locationData(object)),
  669. http.protected.get("/api/fs/list", "v2.fs.list").json(200, locationData(array)),
  670. http.protected
  671. .get("/api/fs/find", "v2.fs.find")
  672. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  673. .at((ctx) => ({ path: "/api/fs/find?query=hello&type=file", headers: ctx.headers() }))
  674. .json(200, locationData(array)),
  675. http.protected.get("/api/reference", "v2.reference.list").json(200, object),
  676. http.protected
  677. .get("/api/provider/{providerID}", "v2.provider.get")
  678. .at((ctx) => ({ path: route("/api/provider/{providerID}", { providerID: "missing" }), headers: ctx.headers() }))
  679. .json(404, object, "status"),
  680. http.protected.get("/api/permission/request", "v2.permission.request.list").json(200, (body) => {
  681. object(body)
  682. object(body.location)
  683. array(body.data)
  684. }),
  685. http.protected.get("/api/question/request", "v2.question.request.list").json(200, (body) => {
  686. object(body)
  687. object(body.location)
  688. array(body.data)
  689. }),
  690. http.protected
  691. .get("/api/session/{sessionID}/permission", "v2.session.permission.list")
  692. .seeded((ctx) => ctx.session({ title: "Permission list owner" }))
  693. .at((ctx) => ({
  694. path: route("/api/session/{sessionID}/permission", { sessionID: ctx.state.id }),
  695. headers: ctx.headers(),
  696. }))
  697. .json(200, data(array)),
  698. http.protected
  699. .get("/api/session/{sessionID}/question", "v2.session.question.list")
  700. .seeded((ctx) => ctx.session({ title: "Question list owner" }))
  701. .at((ctx) => ({
  702. path: route("/api/session/{sessionID}/question", { sessionID: ctx.state.id }),
  703. headers: ctx.headers(),
  704. }))
  705. .json(200, data(array)),
  706. http.protected
  707. .post("/api/session/{sessionID}/permission/{requestID}/reply", "v2.session.permission.reply")
  708. .seeded((ctx) => ctx.session({ title: "Permission owner" }))
  709. .at((ctx) => ({
  710. path: route("/api/session/{sessionID}/permission/{requestID}/reply", {
  711. sessionID: ctx.state.id,
  712. requestID: "per_httpapi_missing",
  713. }),
  714. headers: ctx.headers(),
  715. body: { reply: "once" },
  716. }))
  717. .json(404, object, "status"),
  718. http.protected
  719. .post("/api/session/{sessionID}/question/{requestID}/reply", "v2.session.question.reply")
  720. .seeded((ctx) => ctx.session({ title: "Question reply owner" }))
  721. .at((ctx) => ({
  722. path: route("/api/session/{sessionID}/question/{requestID}/reply", {
  723. sessionID: ctx.state.id,
  724. requestID: "que_httpapi_missing",
  725. }),
  726. headers: ctx.headers(),
  727. body: { answers: [] },
  728. }))
  729. .json(404, object, "status"),
  730. http.protected
  731. .post("/api/session/{sessionID}/question/{requestID}/reject", "v2.session.question.reject")
  732. .seeded((ctx) => ctx.session({ title: "Question reject owner" }))
  733. .at((ctx) => ({
  734. path: route("/api/session/{sessionID}/question/{requestID}/reject", {
  735. sessionID: ctx.state.id,
  736. requestID: "que_httpapi_missing",
  737. }),
  738. headers: ctx.headers(),
  739. }))
  740. .json(404, object, "status"),
  741. http.protected.get("/api/permission/saved", "v2.permission.saved.list").json(200, (body) => {
  742. object(body)
  743. array(body.data)
  744. }),
  745. http.protected
  746. .delete("/api/permission/saved/{id}", "v2.permission.saved.remove")
  747. .at((ctx) => ({ path: route("/api/permission/saved/{id}", { id: "psv_httpapi_missing" }), headers: ctx.headers() }))
  748. .status(204, undefined, "status"),
  749. http.protected
  750. .get("/api/session", "v2.session.list")
  751. .at((ctx) => ({ path: "/api/session?roots=true", headers: ctx.headers() }))
  752. .json(
  753. 200,
  754. (body) => {
  755. object(body)
  756. array(body.data)
  757. object(body.cursor)
  758. },
  759. "none",
  760. ),
  761. http.protected
  762. .get("/api/session", "v2.session.list.filters")
  763. .at((ctx) => ({
  764. path: `/api/session?${new URLSearchParams({
  765. limit: "2",
  766. order: "asc",
  767. path: ".",
  768. roots: "false",
  769. start: "0",
  770. search: "missing",
  771. directory: ctx.directory ?? "",
  772. })}`,
  773. headers: ctx.headers(),
  774. }))
  775. .json(
  776. 200,
  777. (body) => {
  778. object(body)
  779. array(body.data)
  780. object(body.cursor)
  781. },
  782. "none",
  783. ),
  784. http.protected
  785. .get("/api/session", "v2.session.list.cursor")
  786. .at((ctx) => ({
  787. path: `/api/session?${new URLSearchParams({
  788. limit: "2",
  789. cursor: cursor({
  790. order: "desc",
  791. directory: ctx.directory,
  792. anchor: { id: "ses_httpapi_missing", time: 0, direction: "next" },
  793. }),
  794. })}`,
  795. headers: ctx.headers(),
  796. }))
  797. .json(
  798. 200,
  799. (body) => {
  800. object(body)
  801. array(body.data)
  802. object(body.cursor)
  803. },
  804. "none",
  805. ),
  806. http.protected
  807. .get("/api/session", "v2.session.list.cursor.invalid")
  808. .at((ctx) => ({
  809. path: `/api/session?${new URLSearchParams({
  810. cursor: "invalid",
  811. })}`,
  812. headers: ctx.headers(),
  813. }))
  814. .status(400, undefined, "none"),
  815. http.protected
  816. .post("/api/session", "v2.session.create")
  817. .at((ctx) => ({
  818. path: "/api/session",
  819. headers: { ...ctx.headers(), "content-type": "application/json" },
  820. body: {},
  821. }))
  822. .json(200, data(object)),
  823. http.protected
  824. .get("/api/session/{sessionID}", "v2.session.get")
  825. .seeded((ctx) => ctx.session({ title: "Session get" }))
  826. .at((ctx) => ({
  827. path: route("/api/session/{sessionID}", { sessionID: ctx.state.id }),
  828. headers: ctx.headers(),
  829. }))
  830. .json(200, data(object)),
  831. http.protected
  832. .get("/api/session/{sessionID}/context", "v2.session.context")
  833. .at((ctx) => ({
  834. path: route("/api/session/{sessionID}/context", { sessionID: "ses_httpapi_missing" }),
  835. headers: ctx.headers(),
  836. }))
  837. .json(404, object, "status"),
  838. http.protected
  839. .get("/api/session/{sessionID}/message", "v2.session.messages")
  840. .at((ctx) => ({
  841. path: route("/api/session/{sessionID}/message", { sessionID: "ses_httpapi_missing" }),
  842. headers: ctx.headers(),
  843. }))
  844. .json(404, object, "status"),
  845. http.protected
  846. .get("/api/session/{sessionID}/message", "v2.session.messages.params")
  847. .at((ctx) => ({
  848. path: `${route("/api/session/{sessionID}/message", { sessionID: "ses_httpapi_missing" })}?${new URLSearchParams({
  849. limit: "2",
  850. order: "asc",
  851. })}`,
  852. headers: ctx.headers(),
  853. }))
  854. .json(404, object, "status"),
  855. http.protected
  856. .get("/api/session/{sessionID}/message", "v2.session.messages.cursor")
  857. .at((ctx) => ({
  858. path: `${route("/api/session/{sessionID}/message", { sessionID: "ses_httpapi_missing" })}?${new URLSearchParams({
  859. limit: "2",
  860. directory: ctx.directory ?? "",
  861. cursor: cursor({ id: "msg_httpapi_missing", time: 0, order: "desc", direction: "next" }),
  862. })}`,
  863. headers: ctx.headers(),
  864. }))
  865. .json(404, object, "status"),
  866. http.protected
  867. .get("/api/session/{sessionID}/message", "v2.session.messages.cursor.invalid")
  868. .seeded((ctx) => ctx.session({ title: "Invalid message cursor owner" }))
  869. .at((ctx) => ({
  870. path: `${route("/api/session/{sessionID}/message", { sessionID: ctx.state.id })}?${new URLSearchParams({
  871. cursor: cursor({ id: "msg_httpapi_missing", time: 0, order: "desc", direction: "next" }),
  872. order: "asc",
  873. })}`,
  874. headers: ctx.headers(),
  875. }))
  876. .status(400, undefined, "none"),
  877. http.protected
  878. .post("/api/session/{sessionID}/prompt", "v2.session.prompt.invalid")
  879. .seeded((ctx) => ctx.session({ title: "Invalid prompt owner" }))
  880. .at((ctx) => ({
  881. path: route("/api/session/{sessionID}/prompt", { sessionID: ctx.state.id }),
  882. headers: ctx.headers(),
  883. body: {},
  884. }))
  885. .status(400, undefined, "none"),
  886. http.protected
  887. .post("/api/session/{sessionID}/compact", "v2.session.compact")
  888. .at((ctx) => ({
  889. path: route("/api/session/{sessionID}/compact", { sessionID: "ses_httpapi_missing" }),
  890. headers: ctx.headers(),
  891. }))
  892. .status(404, undefined, "status"),
  893. http.protected
  894. .post("/api/session/{sessionID}/wait", "v2.session.wait")
  895. .at((ctx) => ({
  896. path: route("/api/session/{sessionID}/wait", { sessionID: "ses_httpapi_missing" }),
  897. headers: ctx.headers(),
  898. }))
  899. .status(404, undefined, "status"),
  900. http.protected
  901. .get("/session", "session.list")
  902. .seeded((ctx) => ctx.session({ title: "List me" }))
  903. .at((ctx) => ({ path: "/session?roots=true", headers: ctx.headers() }))
  904. .json(200, (body, ctx) => {
  905. array(body)
  906. check(
  907. body.some((item) => isRecord(item) && item.id === ctx.state.id && item.title === "List me"),
  908. "seeded session should be listed",
  909. )
  910. }),
  911. http.protected
  912. .get("/session/status", "session.status")
  913. .seeded((ctx) => ctx.session({ title: "Status session" }))
  914. .json(200, object),
  915. http.protected
  916. .post("/session", "session.create")
  917. .mutating()
  918. .at((ctx) => ({ path: "/session", headers: ctx.headers(), body: { title: "Created session" } }))
  919. .json(
  920. 200,
  921. (body, ctx) => {
  922. object(body)
  923. check(body.title === "Created session", "created session should use requested title")
  924. check(body.directory === ctx.directory, "created session should use scenario directory")
  925. },
  926. "status",
  927. ),
  928. http.protected
  929. .get("/session/{sessionID}", "session.get")
  930. .seeded((ctx) => ctx.session({ title: "Get me" }))
  931. .at((ctx) => ({ path: route("/session/{sessionID}", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  932. .json(200, (body, ctx) => {
  933. object(body)
  934. check(body.id === ctx.state.id, "should return requested session")
  935. check(body.title === "Get me", "should preserve seeded title")
  936. }),
  937. http.protected
  938. .get("/session/{sessionID}", "session.get.missing")
  939. .at((ctx) => ({
  940. path: route("/session/{sessionID}", { sessionID: "ses_httpapi_missing" }),
  941. headers: ctx.headers(),
  942. }))
  943. .status(404),
  944. http.protected
  945. .patch("/session/{sessionID}", "session.update")
  946. .mutating()
  947. .seeded((ctx) => ctx.session({ title: "Before rename" }))
  948. .at((ctx) => ({
  949. path: route("/session/{sessionID}", { sessionID: ctx.state.id }),
  950. headers: ctx.headers(),
  951. body: { title: "After rename" },
  952. }))
  953. .json(
  954. 200,
  955. (body) => {
  956. object(body)
  957. check(body.title === "After rename", "updated session should use new title")
  958. },
  959. "status",
  960. ),
  961. http.protected
  962. .patch("/session/{sessionID}", "session.update.invalid")
  963. .mutating()
  964. .at((ctx) => ({
  965. path: route("/session/{sessionID}", { sessionID: "ses_httpapi_missing" }),
  966. headers: ctx.headers(),
  967. body: { title: 1 },
  968. }))
  969. .status(400),
  970. http.protected
  971. .delete("/session/{sessionID}", "session.delete")
  972. .mutating()
  973. .seeded((ctx) => ctx.session({ title: "Delete me" }))
  974. .at((ctx) => ({ path: route("/session/{sessionID}", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  975. .jsonEffect(200, (body, ctx) =>
  976. Effect.gen(function* () {
  977. check(body === true, "delete should return true")
  978. check((yield* ctx.sessionGet(ctx.state.id)) === undefined, "deleted session should not remain in storage")
  979. }),
  980. ),
  981. http.protected
  982. .get("/session/{sessionID}/children", "session.children")
  983. .seeded((ctx) =>
  984. Effect.gen(function* () {
  985. const parent = yield* ctx.session({ title: "Parent" })
  986. const child = yield* ctx.session({ title: "Child", parentID: parent.id })
  987. return { parent, child }
  988. }),
  989. )
  990. .at((ctx) => ({
  991. path: route("/session/{sessionID}/children", { sessionID: ctx.state.parent.id }),
  992. headers: ctx.headers(),
  993. }))
  994. .json(200, (body, ctx) => {
  995. array(body)
  996. check(
  997. body.some((item) => isRecord(item) && item.id === ctx.state.child.id && item.parentID === ctx.state.parent.id),
  998. "children should include seeded child",
  999. )
  1000. }),
  1001. http.protected
  1002. .get("/session/{sessionID}/todo", "session.todo")
  1003. .seeded((ctx) =>
  1004. Effect.gen(function* () {
  1005. const session = yield* ctx.session({ title: "Todo session" })
  1006. const todos = [{ content: "cover session todo", status: "pending", priority: "high" }]
  1007. yield* ctx.todos(session.id, todos)
  1008. return { session, todos }
  1009. }),
  1010. )
  1011. .at((ctx) => ({
  1012. path: route("/session/{sessionID}/todo", { sessionID: ctx.state.session.id }),
  1013. headers: ctx.headers(),
  1014. }))
  1015. .json(200, (body, ctx) => {
  1016. check(stable(body) === stable(ctx.state.todos), "todos should match seeded state")
  1017. }),
  1018. http.protected
  1019. .get("/session/{sessionID}/diff", "session.diff")
  1020. .seeded((ctx) => ctx.session({ title: "Diff session" }))
  1021. .at((ctx) => ({ path: route("/session/{sessionID}/diff", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1022. .json(200, array),
  1023. http.protected
  1024. .get("/session/{sessionID}/message", "session.messages")
  1025. .seeded((ctx) => ctx.session({ title: "Messages session" }))
  1026. .at((ctx) => ({ path: route("/session/{sessionID}/message", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1027. .json(200, (body) => {
  1028. array(body)
  1029. check(body.length === 0, "new session should have no messages")
  1030. }),
  1031. http.protected
  1032. .get("/session/{sessionID}/message/{messageID}", "session.message")
  1033. .seeded((ctx) =>
  1034. Effect.gen(function* () {
  1035. const session = yield* ctx.session({ title: "Message get session" })
  1036. const message = yield* ctx.message(session.id, { text: "read me" })
  1037. return { session, message }
  1038. }),
  1039. )
  1040. .at((ctx) => ({
  1041. path: route("/session/{sessionID}/message/{messageID}", {
  1042. sessionID: ctx.state.session.id,
  1043. messageID: ctx.state.message.info.id,
  1044. }),
  1045. headers: ctx.headers(),
  1046. }))
  1047. .json(200, (body, ctx) => {
  1048. object(body)
  1049. check(isRecord(body.info) && body.info.id === ctx.state.message.info.id, "should return requested message")
  1050. check(
  1051. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.id === ctx.state.message.part.id),
  1052. "message should include seeded part",
  1053. )
  1054. }),
  1055. http.protected
  1056. .patch("/session/{sessionID}/message/{messageID}/part/{partID}", "part.update")
  1057. .mutating()
  1058. .seeded((ctx) =>
  1059. Effect.gen(function* () {
  1060. const session = yield* ctx.session({ title: "Part update session" })
  1061. const message = yield* ctx.message(session.id, { text: "before" })
  1062. return { session, message }
  1063. }),
  1064. )
  1065. .at((ctx) => ({
  1066. path: route("/session/{sessionID}/message/{messageID}/part/{partID}", {
  1067. sessionID: ctx.state.session.id,
  1068. messageID: ctx.state.message.info.id,
  1069. partID: ctx.state.message.part.id,
  1070. }),
  1071. headers: ctx.headers(),
  1072. body: { ...ctx.state.message.part, text: "after" },
  1073. }))
  1074. .json(
  1075. 200,
  1076. (body) => {
  1077. object(body)
  1078. check(body.type === "text" && body.text === "after", "updated part should be returned")
  1079. },
  1080. "status",
  1081. ),
  1082. http.protected
  1083. .delete("/session/{sessionID}/message/{messageID}/part/{partID}", "part.delete")
  1084. .mutating()
  1085. .seeded((ctx) =>
  1086. Effect.gen(function* () {
  1087. const session = yield* ctx.session({ title: "Part delete session" })
  1088. const message = yield* ctx.message(session.id, { text: "delete part" })
  1089. return { session, message }
  1090. }),
  1091. )
  1092. .at((ctx) => ({
  1093. path: route("/session/{sessionID}/message/{messageID}/part/{partID}", {
  1094. sessionID: ctx.state.session.id,
  1095. messageID: ctx.state.message.info.id,
  1096. partID: ctx.state.message.part.id,
  1097. }),
  1098. headers: ctx.headers(),
  1099. }))
  1100. .jsonEffect(200, (body, ctx) =>
  1101. Effect.gen(function* () {
  1102. check(body === true, "delete part should return true")
  1103. const messages = yield* ctx.messages(ctx.state.session.id)
  1104. check(messages[0]?.parts.length === 0, "deleted part should not remain on message")
  1105. }),
  1106. ),
  1107. http.protected
  1108. .delete("/session/{sessionID}/message/{messageID}", "session.deleteMessage")
  1109. .mutating()
  1110. .seeded((ctx) =>
  1111. Effect.gen(function* () {
  1112. const session = yield* ctx.session({ title: "Message delete session" })
  1113. const message = yield* ctx.message(session.id, { text: "delete message" })
  1114. return { session, message }
  1115. }),
  1116. )
  1117. .at((ctx) => ({
  1118. path: route("/session/{sessionID}/message/{messageID}", {
  1119. sessionID: ctx.state.session.id,
  1120. messageID: ctx.state.message.info.id,
  1121. }),
  1122. headers: ctx.headers(),
  1123. }))
  1124. .jsonEffect(200, (body, ctx) =>
  1125. Effect.gen(function* () {
  1126. check(body === true, "delete message should return true")
  1127. check((yield* ctx.messages(ctx.state.session.id)).length === 0, "deleted message should not remain")
  1128. }),
  1129. ),
  1130. http.protected
  1131. .post("/session/{sessionID}/fork", "session.fork")
  1132. .mutating()
  1133. .seeded((ctx) => ctx.session({ title: "Fork source" }))
  1134. .at((ctx) => ({
  1135. path: route("/session/{sessionID}/fork", { sessionID: ctx.state.id }),
  1136. headers: ctx.headers(),
  1137. body: {},
  1138. }))
  1139. .json(
  1140. 200,
  1141. (body) => {
  1142. object(body)
  1143. check(typeof body.id === "string", "fork should return a session")
  1144. },
  1145. "status",
  1146. ),
  1147. http.protected
  1148. .post("/session/{sessionID}/abort", "session.abort")
  1149. .mutating()
  1150. .seeded((ctx) => ctx.session({ title: "Abort session" }))
  1151. .at((ctx) => ({ path: route("/session/{sessionID}/abort", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1152. .json(200, (body) => {
  1153. check(body === true, "abort should return true")
  1154. }),
  1155. http.protected
  1156. .post("/session/{sessionID}/abort", "session.abort.missing")
  1157. .at((ctx) => ({
  1158. path: route("/session/{sessionID}/abort", { sessionID: "ses_httpapi_missing" }),
  1159. headers: ctx.headers(),
  1160. }))
  1161. .json(200, (body) => {
  1162. check(body === true, "missing session abort should remain a no-op success")
  1163. }),
  1164. http.protected
  1165. .post("/session/{sessionID}/init", "session.init")
  1166. .preserveDatabase()
  1167. .withLlm()
  1168. .seeded((ctx) =>
  1169. Effect.gen(function* () {
  1170. const session = yield* ctx.session({ title: "Init session" })
  1171. const message = yield* ctx.message(session.id, { text: "initialize" })
  1172. yield* ctx.llmText("initialized")
  1173. yield* ctx.llmText("initialized")
  1174. return { session, message }
  1175. }),
  1176. )
  1177. .at((ctx) => ({
  1178. path: route("/session/{sessionID}/init", { sessionID: ctx.state.session.id }),
  1179. headers: ctx.headers(),
  1180. body: { providerID: "test", modelID: "test-model", messageID: ctx.state.message.info.id },
  1181. }))
  1182. .jsonEffect(200, (body, ctx) =>
  1183. Effect.gen(function* () {
  1184. check(body === true, "init should return true")
  1185. yield* ctx.llmWait(1)
  1186. }),
  1187. ),
  1188. http.protected
  1189. .post("/session/{sessionID}/message", "session.prompt")
  1190. .preserveDatabase()
  1191. .withLlm()
  1192. .seeded((ctx) =>
  1193. Effect.gen(function* () {
  1194. const session = yield* ctx.session({ title: "LLM prompt session" })
  1195. yield* ctx.llmText("fake assistant")
  1196. yield* ctx.llmText("fake assistant")
  1197. return session
  1198. }),
  1199. )
  1200. .at((ctx) => ({
  1201. path: route("/session/{sessionID}/message", { sessionID: ctx.state.id }),
  1202. headers: ctx.headers(),
  1203. body: {
  1204. agent: "build",
  1205. model: { providerID: "test", modelID: "test-model" },
  1206. parts: [{ type: "text", text: "hello llm" }],
  1207. },
  1208. }))
  1209. .jsonEffect(
  1210. 200,
  1211. (body, ctx) =>
  1212. Effect.gen(function* () {
  1213. object(body)
  1214. check(isRecord(body.info) && body.info.role === "assistant", "prompt should return assistant message")
  1215. check(
  1216. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.text === "fake assistant"),
  1217. "assistant message should use fake LLM text",
  1218. )
  1219. yield* ctx.llmWait(1)
  1220. }),
  1221. "status",
  1222. ),
  1223. http.protected
  1224. .post("/session/{sessionID}/prompt_async", "session.prompt_async")
  1225. .preserveDatabase()
  1226. .withLlm()
  1227. .seeded((ctx) =>
  1228. Effect.gen(function* () {
  1229. const session = yield* ctx.session({ title: "Async prompt session" })
  1230. yield* ctx.llmText("fake async assistant")
  1231. yield* ctx.llmText("fake async assistant")
  1232. return session
  1233. }),
  1234. )
  1235. .at((ctx) => ({
  1236. path: route("/session/{sessionID}/prompt_async", { sessionID: ctx.state.id }),
  1237. headers: ctx.headers(),
  1238. body: {
  1239. agent: "build",
  1240. model: { providerID: "test", modelID: "test-model" },
  1241. parts: [{ type: "text", text: "hello async" }],
  1242. },
  1243. }))
  1244. .status(204, (ctx) =>
  1245. Effect.gen(function* () {
  1246. yield* ctx.llmWait(1)
  1247. }),
  1248. ),
  1249. http.protected
  1250. .post("/session/{sessionID}/command", "session.command")
  1251. .preserveDatabase()
  1252. .withLlm()
  1253. .seeded((ctx) =>
  1254. Effect.gen(function* () {
  1255. const session = yield* ctx.session({ title: "Command session" })
  1256. yield* ctx.llmText("command done")
  1257. yield* ctx.llmText("command done")
  1258. return session
  1259. }),
  1260. )
  1261. .at((ctx) => ({
  1262. path: route("/session/{sessionID}/command", { sessionID: ctx.state.id }),
  1263. headers: ctx.headers(),
  1264. body: { command: "init", arguments: "", model: "test/test-model" },
  1265. }))
  1266. .jsonEffect(
  1267. 200,
  1268. (body, ctx) =>
  1269. Effect.gen(function* () {
  1270. object(body)
  1271. check(isRecord(body.info) && body.info.role === "assistant", "command should return assistant message")
  1272. yield* ctx.llmWait(1)
  1273. }),
  1274. "status",
  1275. ),
  1276. http.protected
  1277. .post("/session/{sessionID}/shell", "session.shell")
  1278. .preserveDatabase()
  1279. .mutating()
  1280. .seeded((ctx) => ctx.session({ title: "Shell session" }))
  1281. .at((ctx) => ({
  1282. path: route("/session/{sessionID}/shell", { sessionID: ctx.state.id }),
  1283. headers: ctx.headers(),
  1284. body: { agent: "build", model: { providerID: "test", modelID: "test-model" }, command: "printf shell-ok" },
  1285. }))
  1286. .json(
  1287. 200,
  1288. (body) => {
  1289. object(body)
  1290. check(isRecord(body.info) && body.info.role === "assistant", "shell should return assistant message")
  1291. check(
  1292. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.type === "tool"),
  1293. "shell should return a tool part",
  1294. )
  1295. },
  1296. "status",
  1297. ),
  1298. http.protected
  1299. .post("/session/{sessionID}/summarize", "session.summarize")
  1300. .preserveDatabase()
  1301. .withLlm()
  1302. .seeded((ctx) =>
  1303. Effect.gen(function* () {
  1304. const session = yield* ctx.session({ title: "Summarize session" })
  1305. yield* ctx.message(session.id, { text: "summarize this work" })
  1306. const summary = [
  1307. "## Goal",
  1308. "- Exercise session summarize.",
  1309. "",
  1310. "## Constraints & Preferences",
  1311. "- Use fake LLM.",
  1312. "",
  1313. "## Progress",
  1314. "### Done",
  1315. "- Summary generated.",
  1316. "",
  1317. "### In Progress",
  1318. "- (none)",
  1319. "",
  1320. "### Blocked",
  1321. "- (none)",
  1322. "",
  1323. "## Key Decisions",
  1324. "- Keep route local.",
  1325. "",
  1326. "## Next Steps",
  1327. "- (none)",
  1328. "",
  1329. "## Critical Context",
  1330. "- Test fixture.",
  1331. "",
  1332. "## Relevant Files",
  1333. "- test/server/httpapi-exercise/index.ts: scenario",
  1334. ].join("\n")
  1335. yield* ctx.llmText(summary)
  1336. yield* ctx.llmText(summary)
  1337. return session
  1338. }),
  1339. )
  1340. .at((ctx) => ({
  1341. path: route("/session/{sessionID}/summarize", { sessionID: ctx.state.id }),
  1342. headers: ctx.headers(),
  1343. body: { providerID: "test", modelID: "test-model", auto: false },
  1344. }))
  1345. .jsonEffect(
  1346. 200,
  1347. (body, ctx) =>
  1348. Effect.gen(function* () {
  1349. check(body === true, "summarize should return true")
  1350. const messages = yield* ctx.messages(ctx.state.id)
  1351. check(
  1352. messages.some((message) => message.info.role === "assistant" && message.info.summary === true),
  1353. "summarize should create a summary assistant message",
  1354. )
  1355. yield* ctx.llmWait(1)
  1356. }),
  1357. "status",
  1358. ),
  1359. http.protected
  1360. .post("/session/{sessionID}/revert", "session.revert")
  1361. .mutating()
  1362. .seeded((ctx) =>
  1363. Effect.gen(function* () {
  1364. const session = yield* ctx.session({ title: "Revert session" })
  1365. const message = yield* ctx.message(session.id, { text: "revert me" })
  1366. return { session, message }
  1367. }),
  1368. )
  1369. .at((ctx) => ({
  1370. path: route("/session/{sessionID}/revert", { sessionID: ctx.state.session.id }),
  1371. headers: ctx.headers(),
  1372. body: { messageID: ctx.state.message.info.id },
  1373. }))
  1374. .json(
  1375. 200,
  1376. (body, ctx) => {
  1377. object(body)
  1378. check(body.id === ctx.state.session.id, "revert should return the session")
  1379. check(
  1380. isRecord(body.revert) && body.revert.messageID === ctx.state.message.info.id,
  1381. "revert should record reverted message",
  1382. )
  1383. },
  1384. "status",
  1385. ),
  1386. http.protected
  1387. .post("/session/{sessionID}/unrevert", "session.unrevert")
  1388. .mutating()
  1389. .seeded((ctx) => ctx.session({ title: "Unrevert session" }))
  1390. .at((ctx) => ({
  1391. path: route("/session/{sessionID}/unrevert", { sessionID: ctx.state.id }),
  1392. headers: ctx.headers(),
  1393. }))
  1394. .json(
  1395. 200,
  1396. (body, ctx) => {
  1397. object(body)
  1398. check(body.id === ctx.state.id, "unrevert should return the session")
  1399. },
  1400. "status",
  1401. ),
  1402. http.protected
  1403. .post("/session/{sessionID}/permissions/{permissionID}", "permission.respond")
  1404. .seeded((ctx) => ctx.session({ title: "Deprecated permission session" }))
  1405. .at((ctx) => ({
  1406. path: route("/session/{sessionID}/permissions/{permissionID}", {
  1407. sessionID: ctx.state.id,
  1408. permissionID: "per_httpapi_deprecated",
  1409. }),
  1410. headers: ctx.headers(),
  1411. body: { response: "once" },
  1412. }))
  1413. .json(404, object, "status"),
  1414. http.protected
  1415. .post("/session/{sessionID}/share", "session.share")
  1416. .mutating()
  1417. .seeded((ctx) => ctx.session({ title: "Share session" }))
  1418. .at((ctx) => ({ path: route("/session/{sessionID}/share", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1419. .json(
  1420. 200,
  1421. (body, ctx) => {
  1422. object(body)
  1423. check(body.id === ctx.state.id, "share should return the session")
  1424. },
  1425. "status",
  1426. ),
  1427. http.protected
  1428. .delete("/session/{sessionID}/share", "session.unshare")
  1429. .mutating()
  1430. .seeded((ctx) => ctx.session({ title: "Unshare session" }))
  1431. .at((ctx) => ({ path: route("/session/{sessionID}/share", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1432. .json(
  1433. 200,
  1434. (body, ctx) => {
  1435. object(body)
  1436. check(body.id === ctx.state.id, "unshare should return the session")
  1437. },
  1438. "status",
  1439. ),
  1440. http.protected
  1441. .post("/tui/append-prompt", "tui.appendPrompt")
  1442. .at((ctx) => ({ path: "/tui/append-prompt", headers: ctx.headers(), body: { text: "hello" } }))
  1443. .json(200, boolean, "status"),
  1444. http.protected
  1445. .post("/tui/select-session", "tui.selectSession.invalid")
  1446. .at((ctx) => ({ path: "/tui/select-session", headers: ctx.headers(), body: { sessionID: "invalid" } }))
  1447. .status(400),
  1448. http.protected.post("/tui/open-help", "tui.openHelp").json(200, boolean, "status"),
  1449. http.protected.post("/tui/open-sessions", "tui.openSessions").json(200, boolean, "status"),
  1450. http.protected.post("/tui/open-themes", "tui.openThemes").json(200, boolean, "status"),
  1451. http.protected.post("/tui/open-models", "tui.openModels").json(200, boolean, "status"),
  1452. http.protected.post("/tui/submit-prompt", "tui.submitPrompt").json(200, boolean, "status"),
  1453. http.protected.post("/tui/clear-prompt", "tui.clearPrompt").json(200, boolean, "status"),
  1454. http.protected
  1455. .post("/tui/execute-command", "tui.executeCommand")
  1456. .at((ctx) => ({ path: "/tui/execute-command", headers: ctx.headers(), body: { command: "agent_cycle" } }))
  1457. .json(200, boolean, "status"),
  1458. http.protected
  1459. .post("/tui/show-toast", "tui.showToast")
  1460. .at((ctx) => ({
  1461. path: "/tui/show-toast",
  1462. headers: ctx.headers(),
  1463. body: { title: "Exercise", message: "covered", variant: "info", duration: 1000 },
  1464. }))
  1465. .json(200, boolean, "status"),
  1466. http.protected
  1467. .post("/tui/publish", "tui.publish")
  1468. .at((ctx) => ({
  1469. path: "/tui/publish",
  1470. headers: ctx.headers(),
  1471. body: { type: "tui.prompt.append", properties: { text: "published" } },
  1472. }))
  1473. .json(200, boolean, "status"),
  1474. http.protected
  1475. .post("/tui/select-session", "tui.selectSession")
  1476. .seeded((ctx) => ctx.session({ title: "TUI select" }))
  1477. .at((ctx) => ({ path: "/tui/select-session", headers: ctx.headers(), body: { sessionID: ctx.state.id } }))
  1478. .json(200, boolean, "status"),
  1479. http.protected
  1480. .post("/tui/control/response", "tui.control.response")
  1481. .at((ctx) => ({ path: "/tui/control/response", headers: ctx.headers(), body: { ok: true } }))
  1482. .json(200, boolean, "status"),
  1483. http.protected
  1484. .get("/tui/control/next", "tui.control.next")
  1485. .mutating()
  1486. .seeded((ctx) => ctx.tuiRequest({ path: "/tui/exercise", body: { text: "queued" } }))
  1487. .json(
  1488. 200,
  1489. (body) => {
  1490. object(body)
  1491. check(body.path === "/tui/exercise", "control next should return queued path")
  1492. object(body.body)
  1493. check(body.body.text === "queued", "control next should return queued body")
  1494. },
  1495. "status",
  1496. ),
  1497. http.protected
  1498. .post("/global/upgrade", "global.upgrade")
  1499. .global()
  1500. .probe({ path: "/global/upgrade", body: { target: 1 } })
  1501. .at(() => ({ path: "/global/upgrade", body: { target: 1 } }))
  1502. .status(400),
  1503. ]
  1504. const llmScenarios = new Set([
  1505. "session.init",
  1506. "session.prompt",
  1507. "session.prompt_async",
  1508. "session.command",
  1509. "session.summarize",
  1510. ])
  1511. const main = Effect.gen(function* () {
  1512. yield* Effect.addFinalizer(() => Effect.promise(() => disposeApps()).pipe(Effect.andThen(cleanupExercisePaths)))
  1513. const options = parseOptions(Bun.argv.slice(2))
  1514. const modules = yield* Effect.promise(() => runtime())
  1515. const effectRoutes = routeKeys(OpenApi.fromApi(modules.PublicApi))
  1516. const selected = selectedScenarios(options, scenarios)
  1517. const missing = effectRoutes.filter((route) => !scenarios.some((scenario) => route === routeKey(scenario)))
  1518. const extra = scenarios.filter((scenario) => !effectRoutes.includes(routeKey(scenario)))
  1519. for (const scenario of scenarios) {
  1520. if (scenario.kind === "active" && llmScenarios.has(scenario.name) && !scenario.project?.llm) {
  1521. return yield* Effect.fail(new Error(`${scenario.name} must use TestLLMServer via .withLlm()`))
  1522. }
  1523. }
  1524. printHeader(options, effectRoutes, selected, missing, extra, {
  1525. database: exerciseDatabasePath,
  1526. global: exerciseGlobalRoot,
  1527. })
  1528. const results =
  1529. options.mode === "coverage"
  1530. ? selected.map(coverageResult)
  1531. : yield* Effect.forEach(
  1532. selected,
  1533. (scenario) =>
  1534. Effect.gen(function* () {
  1535. if (options.progress) console.log(`${color.dim}RUN ${routeKey(scenario)} ${scenario.name}${color.reset}`)
  1536. return yield* runScenario(options)(scenario)
  1537. }),
  1538. { concurrency: 1 },
  1539. )
  1540. printResults(results, missing, extra)
  1541. if (results.some((result) => result.status === "fail"))
  1542. return yield* Effect.fail(new Error("one or more scenarios failed"))
  1543. if (options.failOnSkip && results.some((result) => result.status === "skip"))
  1544. return yield* Effect.fail(new Error("one or more scenarios are skipped"))
  1545. if (options.failOnMissing && missing.length > 0)
  1546. return yield* Effect.fail(new Error("one or more routes have no scenario"))
  1547. return undefined
  1548. })
  1549. Effect.runPromise(main.pipe(Effect.provide(TestLLMServer.layer), Effect.scoped)).then(
  1550. () => process.exit(0),
  1551. (error: unknown) => {
  1552. console.error(`${color.red}${message(error)}${color.reset}`)
  1553. process.exit(1)
  1554. },
  1555. )