index.ts 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  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/connector", "v2.connector.list").json(200, locationData(array)),
  650. http.protected
  651. .get("/api/connector/{connectorID}", "v2.connector.get")
  652. .at((ctx) => ({ path: route("/api/connector/{connectorID}", { connectorID: "missing" }), headers: ctx.headers() }))
  653. .json(200, object),
  654. http.protected
  655. .post("/api/connector/{connectorID}/connect/key", "v2.connector.connect.key")
  656. .at((ctx) => ({
  657. path: route("/api/connector/{connectorID}/connect/key", { connectorID: "missing" }),
  658. headers: ctx.headers(),
  659. body: { methodID: "missing", key: "test", inputs: {} },
  660. }))
  661. .status(500, undefined, "status"),
  662. http.protected
  663. .post("/api/connector/{connectorID}/connect/oauth", "v2.connector.connect.oauth.begin")
  664. .at((ctx) => ({
  665. path: route("/api/connector/{connectorID}/connect/oauth", { connectorID: "missing" }),
  666. headers: ctx.headers(),
  667. body: { methodID: "missing", inputs: {} },
  668. }))
  669. .status(500, undefined, "status"),
  670. http.protected
  671. .get("/api/connector/oauth/{attemptID}", "v2.connector.connect.oauth.status")
  672. .at((ctx) => ({
  673. path: route("/api/connector/oauth/{attemptID}", { attemptID: "con_missing" }),
  674. headers: ctx.headers(),
  675. }))
  676. .status(500, undefined, "status"),
  677. http.protected
  678. .post("/api/connector/oauth/{attemptID}/complete", "v2.connector.connect.oauth.complete")
  679. .at((ctx) => ({
  680. path: route("/api/connector/oauth/{attemptID}/complete", { attemptID: "con_missing" }),
  681. headers: ctx.headers(),
  682. body: {},
  683. }))
  684. .status(500, undefined, "status"),
  685. http.protected
  686. .delete("/api/connector/oauth/{attemptID}", "v2.connector.connect.oauth.cancel")
  687. .at((ctx) => ({
  688. path: route("/api/connector/oauth/{attemptID}", { attemptID: "con_missing" }),
  689. headers: ctx.headers(),
  690. }))
  691. .status(204, undefined, "status"),
  692. http.protected.get("/api/command", "v2.command.list").json(200, locationData(array)),
  693. http.protected.get("/api/skill", "v2.skill.list").json(200, locationData(array)),
  694. http.protected
  695. .get("/api/event", "v2.event.subscribe")
  696. .stream()
  697. .status(
  698. 200,
  699. (ctx, result) =>
  700. Effect.sync(() => {
  701. check(result.contentType.includes("text/event-stream"), "v2 event should be an SSE stream")
  702. check(result.text.includes("server.connected"), "v2 event should emit initial connection event")
  703. check(!!ctx.directory && result.text.includes(ctx.directory), "v2 event should include the resolved location")
  704. }),
  705. "status",
  706. ),
  707. http.protected
  708. .get("/api/fs/read/*", "v2.fs.read")
  709. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  710. .at((ctx) => ({ path: "/api/fs/read/hello.txt", headers: ctx.headers() }))
  711. .status(
  712. 200,
  713. (_ctx, result) =>
  714. Effect.sync(() => {
  715. check(result.text === "hello\n", "v2 fs read should return the file body")
  716. check(result.contentType.includes("text/plain"), "v2 fs read should return the file content type")
  717. }),
  718. "status",
  719. ),
  720. http.protected.get("/api/fs/list", "v2.fs.list").json(200, locationData(array)),
  721. http.protected
  722. .get("/api/fs/find", "v2.fs.find")
  723. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  724. .at((ctx) => ({ path: "/api/fs/find?query=hello&type=file", headers: ctx.headers() }))
  725. .json(200, locationData(array)),
  726. http.protected.get("/api/reference", "v2.reference.list").json(200, object),
  727. http.protected
  728. .get("/api/provider/{providerID}", "v2.provider.get")
  729. .at((ctx) => ({ path: route("/api/provider/{providerID}", { providerID: "missing" }), headers: ctx.headers() }))
  730. .json(404, object, "status"),
  731. http.protected.get("/api/permission/request", "v2.permission.request.list").json(200, (body) => {
  732. object(body)
  733. object(body.location)
  734. array(body.data)
  735. }),
  736. http.protected.get("/api/question/request", "v2.question.request.list").json(200, (body) => {
  737. object(body)
  738. object(body.location)
  739. array(body.data)
  740. }),
  741. http.protected
  742. .get("/api/session/{sessionID}/permission", "v2.session.permission.list")
  743. .seeded((ctx) => ctx.session({ title: "Permission list owner" }))
  744. .at((ctx) => ({
  745. path: route("/api/session/{sessionID}/permission", { sessionID: ctx.state.id }),
  746. headers: ctx.headers(),
  747. }))
  748. .json(200, data(array)),
  749. http.protected
  750. .get("/api/session/{sessionID}/question", "v2.session.question.list")
  751. .seeded((ctx) => ctx.session({ title: "Question list owner" }))
  752. .at((ctx) => ({
  753. path: route("/api/session/{sessionID}/question", { sessionID: ctx.state.id }),
  754. headers: ctx.headers(),
  755. }))
  756. .json(200, data(array)),
  757. http.protected
  758. .post("/api/session/{sessionID}/permission/{requestID}/reply", "v2.session.permission.reply")
  759. .seeded((ctx) => ctx.session({ title: "Permission owner" }))
  760. .at((ctx) => ({
  761. path: route("/api/session/{sessionID}/permission/{requestID}/reply", {
  762. sessionID: ctx.state.id,
  763. requestID: "per_httpapi_missing",
  764. }),
  765. headers: ctx.headers(),
  766. body: { reply: "once" },
  767. }))
  768. .json(404, object, "status"),
  769. http.protected
  770. .post("/api/session/{sessionID}/question/{requestID}/reply", "v2.session.question.reply")
  771. .seeded((ctx) => ctx.session({ title: "Question reply owner" }))
  772. .at((ctx) => ({
  773. path: route("/api/session/{sessionID}/question/{requestID}/reply", {
  774. sessionID: ctx.state.id,
  775. requestID: "que_httpapi_missing",
  776. }),
  777. headers: ctx.headers(),
  778. body: { answers: [] },
  779. }))
  780. .json(404, object, "status"),
  781. http.protected
  782. .post("/api/session/{sessionID}/question/{requestID}/reject", "v2.session.question.reject")
  783. .seeded((ctx) => ctx.session({ title: "Question reject owner" }))
  784. .at((ctx) => ({
  785. path: route("/api/session/{sessionID}/question/{requestID}/reject", {
  786. sessionID: ctx.state.id,
  787. requestID: "que_httpapi_missing",
  788. }),
  789. headers: ctx.headers(),
  790. }))
  791. .json(404, object, "status"),
  792. http.protected.get("/api/permission/saved", "v2.permission.saved.list").json(200, (body) => {
  793. object(body)
  794. array(body.data)
  795. }),
  796. http.protected
  797. .delete("/api/permission/saved/{id}", "v2.permission.saved.remove")
  798. .at((ctx) => ({ path: route("/api/permission/saved/{id}", { id: "psv_httpapi_missing" }), headers: ctx.headers() }))
  799. .status(204, undefined, "status"),
  800. http.protected
  801. .get("/api/session", "v2.session.list")
  802. .at((ctx) => ({ path: "/api/session?roots=true", headers: ctx.headers() }))
  803. .json(
  804. 200,
  805. (body) => {
  806. object(body)
  807. array(body.data)
  808. object(body.cursor)
  809. },
  810. "none",
  811. ),
  812. http.protected
  813. .get("/api/session", "v2.session.list.filters")
  814. .at((ctx) => ({
  815. path: `/api/session?${new URLSearchParams({
  816. limit: "2",
  817. order: "asc",
  818. path: ".",
  819. roots: "false",
  820. start: "0",
  821. search: "missing",
  822. directory: ctx.directory ?? "",
  823. })}`,
  824. headers: ctx.headers(),
  825. }))
  826. .json(
  827. 200,
  828. (body) => {
  829. object(body)
  830. array(body.data)
  831. object(body.cursor)
  832. },
  833. "none",
  834. ),
  835. http.protected
  836. .get("/api/session", "v2.session.list.cursor")
  837. .at((ctx) => ({
  838. path: `/api/session?${new URLSearchParams({
  839. limit: "2",
  840. cursor: cursor({
  841. order: "desc",
  842. directory: ctx.directory,
  843. anchor: { id: "ses_httpapi_missing", time: 0, direction: "next" },
  844. }),
  845. })}`,
  846. headers: ctx.headers(),
  847. }))
  848. .json(
  849. 200,
  850. (body) => {
  851. object(body)
  852. array(body.data)
  853. object(body.cursor)
  854. },
  855. "none",
  856. ),
  857. http.protected
  858. .get("/api/session", "v2.session.list.cursor.invalid")
  859. .at((ctx) => ({
  860. path: `/api/session?${new URLSearchParams({
  861. cursor: "invalid",
  862. })}`,
  863. headers: ctx.headers(),
  864. }))
  865. .status(400, undefined, "none"),
  866. http.protected
  867. .post("/api/session", "v2.session.create")
  868. .at((ctx) => ({
  869. path: "/api/session",
  870. headers: { ...ctx.headers(), "content-type": "application/json" },
  871. body: {},
  872. }))
  873. .json(200, data(object)),
  874. http.protected
  875. .get("/api/session/{sessionID}", "v2.session.get")
  876. .seeded((ctx) => ctx.session({ title: "Session get" }))
  877. .at((ctx) => ({
  878. path: route("/api/session/{sessionID}", { sessionID: ctx.state.id }),
  879. headers: ctx.headers(),
  880. }))
  881. .json(200, data(object)),
  882. http.protected
  883. .get("/api/session/{sessionID}/context", "v2.session.context")
  884. .at((ctx) => ({
  885. path: route("/api/session/{sessionID}/context", { sessionID: "ses_httpapi_missing" }),
  886. headers: ctx.headers(),
  887. }))
  888. .json(404, object, "status"),
  889. http.protected
  890. .get("/api/session/{sessionID}/message", "v2.session.messages")
  891. .at((ctx) => ({
  892. path: route("/api/session/{sessionID}/message", { sessionID: "ses_httpapi_missing" }),
  893. headers: ctx.headers(),
  894. }))
  895. .json(404, object, "status"),
  896. http.protected
  897. .get("/api/session/{sessionID}/message", "v2.session.messages.params")
  898. .at((ctx) => ({
  899. path: `${route("/api/session/{sessionID}/message", { sessionID: "ses_httpapi_missing" })}?${new URLSearchParams({
  900. limit: "2",
  901. order: "asc",
  902. })}`,
  903. headers: ctx.headers(),
  904. }))
  905. .json(404, object, "status"),
  906. http.protected
  907. .get("/api/session/{sessionID}/message", "v2.session.messages.cursor")
  908. .at((ctx) => ({
  909. path: `${route("/api/session/{sessionID}/message", { sessionID: "ses_httpapi_missing" })}?${new URLSearchParams({
  910. limit: "2",
  911. directory: ctx.directory ?? "",
  912. cursor: cursor({ id: "msg_httpapi_missing", time: 0, order: "desc", direction: "next" }),
  913. })}`,
  914. headers: ctx.headers(),
  915. }))
  916. .json(404, object, "status"),
  917. http.protected
  918. .get("/api/session/{sessionID}/message", "v2.session.messages.cursor.invalid")
  919. .seeded((ctx) => ctx.session({ title: "Invalid message cursor owner" }))
  920. .at((ctx) => ({
  921. path: `${route("/api/session/{sessionID}/message", { sessionID: ctx.state.id })}?${new URLSearchParams({
  922. cursor: cursor({ id: "msg_httpapi_missing", time: 0, order: "desc", direction: "next" }),
  923. order: "asc",
  924. })}`,
  925. headers: ctx.headers(),
  926. }))
  927. .status(400, undefined, "none"),
  928. http.protected
  929. .post("/api/session/{sessionID}/prompt", "v2.session.prompt.invalid")
  930. .seeded((ctx) => ctx.session({ title: "Invalid prompt owner" }))
  931. .at((ctx) => ({
  932. path: route("/api/session/{sessionID}/prompt", { sessionID: ctx.state.id }),
  933. headers: ctx.headers(),
  934. body: {},
  935. }))
  936. .status(400, undefined, "none"),
  937. http.protected
  938. .post("/api/session/{sessionID}/compact", "v2.session.compact")
  939. .at((ctx) => ({
  940. path: route("/api/session/{sessionID}/compact", { sessionID: "ses_httpapi_missing" }),
  941. headers: ctx.headers(),
  942. }))
  943. .status(404, undefined, "status"),
  944. http.protected
  945. .post("/api/session/{sessionID}/wait", "v2.session.wait")
  946. .at((ctx) => ({
  947. path: route("/api/session/{sessionID}/wait", { sessionID: "ses_httpapi_missing" }),
  948. headers: ctx.headers(),
  949. }))
  950. .status(404, undefined, "status"),
  951. http.protected
  952. .get("/session", "session.list")
  953. .seeded((ctx) => ctx.session({ title: "List me" }))
  954. .at((ctx) => ({ path: "/session?roots=true", headers: ctx.headers() }))
  955. .json(200, (body, ctx) => {
  956. array(body)
  957. check(
  958. body.some((item) => isRecord(item) && item.id === ctx.state.id && item.title === "List me"),
  959. "seeded session should be listed",
  960. )
  961. }),
  962. http.protected
  963. .get("/session/status", "session.status")
  964. .seeded((ctx) => ctx.session({ title: "Status session" }))
  965. .json(200, object),
  966. http.protected
  967. .post("/session", "session.create")
  968. .mutating()
  969. .at((ctx) => ({ path: "/session", headers: ctx.headers(), body: { title: "Created session" } }))
  970. .json(
  971. 200,
  972. (body, ctx) => {
  973. object(body)
  974. check(body.title === "Created session", "created session should use requested title")
  975. check(body.directory === ctx.directory, "created session should use scenario directory")
  976. },
  977. "status",
  978. ),
  979. http.protected
  980. .get("/session/{sessionID}", "session.get")
  981. .seeded((ctx) => ctx.session({ title: "Get me" }))
  982. .at((ctx) => ({ path: route("/session/{sessionID}", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  983. .json(200, (body, ctx) => {
  984. object(body)
  985. check(body.id === ctx.state.id, "should return requested session")
  986. check(body.title === "Get me", "should preserve seeded title")
  987. }),
  988. http.protected
  989. .get("/session/{sessionID}", "session.get.missing")
  990. .at((ctx) => ({
  991. path: route("/session/{sessionID}", { sessionID: "ses_httpapi_missing" }),
  992. headers: ctx.headers(),
  993. }))
  994. .status(404),
  995. http.protected
  996. .patch("/session/{sessionID}", "session.update")
  997. .mutating()
  998. .seeded((ctx) => ctx.session({ title: "Before rename" }))
  999. .at((ctx) => ({
  1000. path: route("/session/{sessionID}", { sessionID: ctx.state.id }),
  1001. headers: ctx.headers(),
  1002. body: { title: "After rename" },
  1003. }))
  1004. .json(
  1005. 200,
  1006. (body) => {
  1007. object(body)
  1008. check(body.title === "After rename", "updated session should use new title")
  1009. },
  1010. "status",
  1011. ),
  1012. http.protected
  1013. .patch("/session/{sessionID}", "session.update.invalid")
  1014. .mutating()
  1015. .at((ctx) => ({
  1016. path: route("/session/{sessionID}", { sessionID: "ses_httpapi_missing" }),
  1017. headers: ctx.headers(),
  1018. body: { title: 1 },
  1019. }))
  1020. .status(400),
  1021. http.protected
  1022. .delete("/session/{sessionID}", "session.delete")
  1023. .mutating()
  1024. .seeded((ctx) => ctx.session({ title: "Delete me" }))
  1025. .at((ctx) => ({ path: route("/session/{sessionID}", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1026. .jsonEffect(200, (body, ctx) =>
  1027. Effect.gen(function* () {
  1028. check(body === true, "delete should return true")
  1029. check((yield* ctx.sessionGet(ctx.state.id)) === undefined, "deleted session should not remain in storage")
  1030. }),
  1031. ),
  1032. http.protected
  1033. .get("/session/{sessionID}/children", "session.children")
  1034. .seeded((ctx) =>
  1035. Effect.gen(function* () {
  1036. const parent = yield* ctx.session({ title: "Parent" })
  1037. const child = yield* ctx.session({ title: "Child", parentID: parent.id })
  1038. return { parent, child }
  1039. }),
  1040. )
  1041. .at((ctx) => ({
  1042. path: route("/session/{sessionID}/children", { sessionID: ctx.state.parent.id }),
  1043. headers: ctx.headers(),
  1044. }))
  1045. .json(200, (body, ctx) => {
  1046. array(body)
  1047. check(
  1048. body.some((item) => isRecord(item) && item.id === ctx.state.child.id && item.parentID === ctx.state.parent.id),
  1049. "children should include seeded child",
  1050. )
  1051. }),
  1052. http.protected
  1053. .get("/session/{sessionID}/todo", "session.todo")
  1054. .seeded((ctx) =>
  1055. Effect.gen(function* () {
  1056. const session = yield* ctx.session({ title: "Todo session" })
  1057. const todos = [{ content: "cover session todo", status: "pending", priority: "high" }]
  1058. yield* ctx.todos(session.id, todos)
  1059. return { session, todos }
  1060. }),
  1061. )
  1062. .at((ctx) => ({
  1063. path: route("/session/{sessionID}/todo", { sessionID: ctx.state.session.id }),
  1064. headers: ctx.headers(),
  1065. }))
  1066. .json(200, (body, ctx) => {
  1067. check(stable(body) === stable(ctx.state.todos), "todos should match seeded state")
  1068. }),
  1069. http.protected
  1070. .get("/session/{sessionID}/diff", "session.diff")
  1071. .seeded((ctx) => ctx.session({ title: "Diff session" }))
  1072. .at((ctx) => ({ path: route("/session/{sessionID}/diff", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1073. .json(200, array),
  1074. http.protected
  1075. .get("/session/{sessionID}/message", "session.messages")
  1076. .seeded((ctx) => ctx.session({ title: "Messages session" }))
  1077. .at((ctx) => ({ path: route("/session/{sessionID}/message", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1078. .json(200, (body) => {
  1079. array(body)
  1080. check(body.length === 0, "new session should have no messages")
  1081. }),
  1082. http.protected
  1083. .get("/session/{sessionID}/message/{messageID}", "session.message")
  1084. .seeded((ctx) =>
  1085. Effect.gen(function* () {
  1086. const session = yield* ctx.session({ title: "Message get session" })
  1087. const message = yield* ctx.message(session.id, { text: "read me" })
  1088. return { session, message }
  1089. }),
  1090. )
  1091. .at((ctx) => ({
  1092. path: route("/session/{sessionID}/message/{messageID}", {
  1093. sessionID: ctx.state.session.id,
  1094. messageID: ctx.state.message.info.id,
  1095. }),
  1096. headers: ctx.headers(),
  1097. }))
  1098. .json(200, (body, ctx) => {
  1099. object(body)
  1100. check(isRecord(body.info) && body.info.id === ctx.state.message.info.id, "should return requested message")
  1101. check(
  1102. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.id === ctx.state.message.part.id),
  1103. "message should include seeded part",
  1104. )
  1105. }),
  1106. http.protected
  1107. .patch("/session/{sessionID}/message/{messageID}/part/{partID}", "part.update")
  1108. .mutating()
  1109. .seeded((ctx) =>
  1110. Effect.gen(function* () {
  1111. const session = yield* ctx.session({ title: "Part update session" })
  1112. const message = yield* ctx.message(session.id, { text: "before" })
  1113. return { session, message }
  1114. }),
  1115. )
  1116. .at((ctx) => ({
  1117. path: route("/session/{sessionID}/message/{messageID}/part/{partID}", {
  1118. sessionID: ctx.state.session.id,
  1119. messageID: ctx.state.message.info.id,
  1120. partID: ctx.state.message.part.id,
  1121. }),
  1122. headers: ctx.headers(),
  1123. body: { ...ctx.state.message.part, text: "after" },
  1124. }))
  1125. .json(
  1126. 200,
  1127. (body) => {
  1128. object(body)
  1129. check(body.type === "text" && body.text === "after", "updated part should be returned")
  1130. },
  1131. "status",
  1132. ),
  1133. http.protected
  1134. .delete("/session/{sessionID}/message/{messageID}/part/{partID}", "part.delete")
  1135. .mutating()
  1136. .seeded((ctx) =>
  1137. Effect.gen(function* () {
  1138. const session = yield* ctx.session({ title: "Part delete session" })
  1139. const message = yield* ctx.message(session.id, { text: "delete part" })
  1140. return { session, message }
  1141. }),
  1142. )
  1143. .at((ctx) => ({
  1144. path: route("/session/{sessionID}/message/{messageID}/part/{partID}", {
  1145. sessionID: ctx.state.session.id,
  1146. messageID: ctx.state.message.info.id,
  1147. partID: ctx.state.message.part.id,
  1148. }),
  1149. headers: ctx.headers(),
  1150. }))
  1151. .jsonEffect(200, (body, ctx) =>
  1152. Effect.gen(function* () {
  1153. check(body === true, "delete part should return true")
  1154. const messages = yield* ctx.messages(ctx.state.session.id)
  1155. check(messages[0]?.parts.length === 0, "deleted part should not remain on message")
  1156. }),
  1157. ),
  1158. http.protected
  1159. .delete("/session/{sessionID}/message/{messageID}", "session.deleteMessage")
  1160. .mutating()
  1161. .seeded((ctx) =>
  1162. Effect.gen(function* () {
  1163. const session = yield* ctx.session({ title: "Message delete session" })
  1164. const message = yield* ctx.message(session.id, { text: "delete message" })
  1165. return { session, message }
  1166. }),
  1167. )
  1168. .at((ctx) => ({
  1169. path: route("/session/{sessionID}/message/{messageID}", {
  1170. sessionID: ctx.state.session.id,
  1171. messageID: ctx.state.message.info.id,
  1172. }),
  1173. headers: ctx.headers(),
  1174. }))
  1175. .jsonEffect(200, (body, ctx) =>
  1176. Effect.gen(function* () {
  1177. check(body === true, "delete message should return true")
  1178. check((yield* ctx.messages(ctx.state.session.id)).length === 0, "deleted message should not remain")
  1179. }),
  1180. ),
  1181. http.protected
  1182. .post("/session/{sessionID}/fork", "session.fork")
  1183. .mutating()
  1184. .seeded((ctx) => ctx.session({ title: "Fork source" }))
  1185. .at((ctx) => ({
  1186. path: route("/session/{sessionID}/fork", { sessionID: ctx.state.id }),
  1187. headers: ctx.headers(),
  1188. body: {},
  1189. }))
  1190. .json(
  1191. 200,
  1192. (body) => {
  1193. object(body)
  1194. check(typeof body.id === "string", "fork should return a session")
  1195. },
  1196. "status",
  1197. ),
  1198. http.protected
  1199. .post("/session/{sessionID}/abort", "session.abort")
  1200. .mutating()
  1201. .seeded((ctx) => ctx.session({ title: "Abort session" }))
  1202. .at((ctx) => ({ path: route("/session/{sessionID}/abort", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1203. .json(200, (body) => {
  1204. check(body === true, "abort should return true")
  1205. }),
  1206. http.protected
  1207. .post("/session/{sessionID}/abort", "session.abort.missing")
  1208. .at((ctx) => ({
  1209. path: route("/session/{sessionID}/abort", { sessionID: "ses_httpapi_missing" }),
  1210. headers: ctx.headers(),
  1211. }))
  1212. .json(200, (body) => {
  1213. check(body === true, "missing session abort should remain a no-op success")
  1214. }),
  1215. http.protected
  1216. .post("/session/{sessionID}/init", "session.init")
  1217. .preserveDatabase()
  1218. .withLlm()
  1219. .seeded((ctx) =>
  1220. Effect.gen(function* () {
  1221. const session = yield* ctx.session({ title: "Init session" })
  1222. const message = yield* ctx.message(session.id, { text: "initialize" })
  1223. yield* ctx.llmText("initialized")
  1224. yield* ctx.llmText("initialized")
  1225. return { session, message }
  1226. }),
  1227. )
  1228. .at((ctx) => ({
  1229. path: route("/session/{sessionID}/init", { sessionID: ctx.state.session.id }),
  1230. headers: ctx.headers(),
  1231. body: { providerID: "test", modelID: "test-model", messageID: ctx.state.message.info.id },
  1232. }))
  1233. .jsonEffect(200, (body, ctx) =>
  1234. Effect.gen(function* () {
  1235. check(body === true, "init should return true")
  1236. yield* ctx.llmWait(1)
  1237. }),
  1238. ),
  1239. http.protected
  1240. .post("/session/{sessionID}/message", "session.prompt")
  1241. .preserveDatabase()
  1242. .withLlm()
  1243. .seeded((ctx) =>
  1244. Effect.gen(function* () {
  1245. const session = yield* ctx.session({ title: "LLM prompt session" })
  1246. yield* ctx.llmText("fake assistant")
  1247. yield* ctx.llmText("fake assistant")
  1248. return session
  1249. }),
  1250. )
  1251. .at((ctx) => ({
  1252. path: route("/session/{sessionID}/message", { sessionID: ctx.state.id }),
  1253. headers: ctx.headers(),
  1254. body: {
  1255. agent: "build",
  1256. model: { providerID: "test", modelID: "test-model" },
  1257. parts: [{ type: "text", text: "hello llm" }],
  1258. },
  1259. }))
  1260. .jsonEffect(
  1261. 200,
  1262. (body, ctx) =>
  1263. Effect.gen(function* () {
  1264. object(body)
  1265. check(isRecord(body.info) && body.info.role === "assistant", "prompt should return assistant message")
  1266. check(
  1267. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.text === "fake assistant"),
  1268. "assistant message should use fake LLM text",
  1269. )
  1270. yield* ctx.llmWait(1)
  1271. }),
  1272. "status",
  1273. ),
  1274. http.protected
  1275. .post("/session/{sessionID}/prompt_async", "session.prompt_async")
  1276. .preserveDatabase()
  1277. .withLlm()
  1278. .seeded((ctx) =>
  1279. Effect.gen(function* () {
  1280. const session = yield* ctx.session({ title: "Async prompt session" })
  1281. yield* ctx.llmText("fake async assistant")
  1282. yield* ctx.llmText("fake async assistant")
  1283. return session
  1284. }),
  1285. )
  1286. .at((ctx) => ({
  1287. path: route("/session/{sessionID}/prompt_async", { sessionID: ctx.state.id }),
  1288. headers: ctx.headers(),
  1289. body: {
  1290. agent: "build",
  1291. model: { providerID: "test", modelID: "test-model" },
  1292. parts: [{ type: "text", text: "hello async" }],
  1293. },
  1294. }))
  1295. .status(204, (ctx) =>
  1296. Effect.gen(function* () {
  1297. yield* ctx.llmWait(1)
  1298. }),
  1299. ),
  1300. http.protected
  1301. .post("/session/{sessionID}/command", "session.command")
  1302. .preserveDatabase()
  1303. .withLlm()
  1304. .seeded((ctx) =>
  1305. Effect.gen(function* () {
  1306. const session = yield* ctx.session({ title: "Command session" })
  1307. yield* ctx.llmText("command done")
  1308. yield* ctx.llmText("command done")
  1309. return session
  1310. }),
  1311. )
  1312. .at((ctx) => ({
  1313. path: route("/session/{sessionID}/command", { sessionID: ctx.state.id }),
  1314. headers: ctx.headers(),
  1315. body: { command: "init", arguments: "", model: "test/test-model" },
  1316. }))
  1317. .jsonEffect(
  1318. 200,
  1319. (body, ctx) =>
  1320. Effect.gen(function* () {
  1321. object(body)
  1322. check(isRecord(body.info) && body.info.role === "assistant", "command should return assistant message")
  1323. yield* ctx.llmWait(1)
  1324. }),
  1325. "status",
  1326. ),
  1327. http.protected
  1328. .post("/session/{sessionID}/shell", "session.shell")
  1329. .preserveDatabase()
  1330. .mutating()
  1331. .seeded((ctx) => ctx.session({ title: "Shell session" }))
  1332. .at((ctx) => ({
  1333. path: route("/session/{sessionID}/shell", { sessionID: ctx.state.id }),
  1334. headers: ctx.headers(),
  1335. body: { agent: "build", model: { providerID: "test", modelID: "test-model" }, command: "printf shell-ok" },
  1336. }))
  1337. .json(
  1338. 200,
  1339. (body) => {
  1340. object(body)
  1341. check(isRecord(body.info) && body.info.role === "assistant", "shell should return assistant message")
  1342. check(
  1343. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.type === "tool"),
  1344. "shell should return a tool part",
  1345. )
  1346. },
  1347. "status",
  1348. ),
  1349. http.protected
  1350. .post("/session/{sessionID}/summarize", "session.summarize")
  1351. .preserveDatabase()
  1352. .withLlm()
  1353. .seeded((ctx) =>
  1354. Effect.gen(function* () {
  1355. const session = yield* ctx.session({ title: "Summarize session" })
  1356. yield* ctx.message(session.id, { text: "summarize this work" })
  1357. const summary = [
  1358. "## Goal",
  1359. "- Exercise session summarize.",
  1360. "",
  1361. "## Constraints & Preferences",
  1362. "- Use fake LLM.",
  1363. "",
  1364. "## Progress",
  1365. "### Done",
  1366. "- Summary generated.",
  1367. "",
  1368. "### In Progress",
  1369. "- (none)",
  1370. "",
  1371. "### Blocked",
  1372. "- (none)",
  1373. "",
  1374. "## Key Decisions",
  1375. "- Keep route local.",
  1376. "",
  1377. "## Next Steps",
  1378. "- (none)",
  1379. "",
  1380. "## Critical Context",
  1381. "- Test fixture.",
  1382. "",
  1383. "## Relevant Files",
  1384. "- test/server/httpapi-exercise/index.ts: scenario",
  1385. ].join("\n")
  1386. yield* ctx.llmText(summary)
  1387. yield* ctx.llmText(summary)
  1388. return session
  1389. }),
  1390. )
  1391. .at((ctx) => ({
  1392. path: route("/session/{sessionID}/summarize", { sessionID: ctx.state.id }),
  1393. headers: ctx.headers(),
  1394. body: { providerID: "test", modelID: "test-model", auto: false },
  1395. }))
  1396. .jsonEffect(
  1397. 200,
  1398. (body, ctx) =>
  1399. Effect.gen(function* () {
  1400. check(body === true, "summarize should return true")
  1401. const messages = yield* ctx.messages(ctx.state.id)
  1402. check(
  1403. messages.some((message) => message.info.role === "assistant" && message.info.summary === true),
  1404. "summarize should create a summary assistant message",
  1405. )
  1406. yield* ctx.llmWait(1)
  1407. }),
  1408. "status",
  1409. ),
  1410. http.protected
  1411. .post("/session/{sessionID}/revert", "session.revert")
  1412. .mutating()
  1413. .seeded((ctx) =>
  1414. Effect.gen(function* () {
  1415. const session = yield* ctx.session({ title: "Revert session" })
  1416. const message = yield* ctx.message(session.id, { text: "revert me" })
  1417. return { session, message }
  1418. }),
  1419. )
  1420. .at((ctx) => ({
  1421. path: route("/session/{sessionID}/revert", { sessionID: ctx.state.session.id }),
  1422. headers: ctx.headers(),
  1423. body: { messageID: ctx.state.message.info.id },
  1424. }))
  1425. .json(
  1426. 200,
  1427. (body, ctx) => {
  1428. object(body)
  1429. check(body.id === ctx.state.session.id, "revert should return the session")
  1430. check(
  1431. isRecord(body.revert) && body.revert.messageID === ctx.state.message.info.id,
  1432. "revert should record reverted message",
  1433. )
  1434. },
  1435. "status",
  1436. ),
  1437. http.protected
  1438. .post("/session/{sessionID}/unrevert", "session.unrevert")
  1439. .mutating()
  1440. .seeded((ctx) => ctx.session({ title: "Unrevert session" }))
  1441. .at((ctx) => ({
  1442. path: route("/session/{sessionID}/unrevert", { sessionID: ctx.state.id }),
  1443. headers: ctx.headers(),
  1444. }))
  1445. .json(
  1446. 200,
  1447. (body, ctx) => {
  1448. object(body)
  1449. check(body.id === ctx.state.id, "unrevert should return the session")
  1450. },
  1451. "status",
  1452. ),
  1453. http.protected
  1454. .post("/session/{sessionID}/permissions/{permissionID}", "permission.respond")
  1455. .seeded((ctx) => ctx.session({ title: "Deprecated permission session" }))
  1456. .at((ctx) => ({
  1457. path: route("/session/{sessionID}/permissions/{permissionID}", {
  1458. sessionID: ctx.state.id,
  1459. permissionID: "per_httpapi_deprecated",
  1460. }),
  1461. headers: ctx.headers(),
  1462. body: { response: "once" },
  1463. }))
  1464. .json(404, object, "status"),
  1465. http.protected
  1466. .post("/session/{sessionID}/share", "session.share")
  1467. .mutating()
  1468. .seeded((ctx) => ctx.session({ title: "Share session" }))
  1469. .at((ctx) => ({ path: route("/session/{sessionID}/share", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1470. .json(
  1471. 200,
  1472. (body, ctx) => {
  1473. object(body)
  1474. check(body.id === ctx.state.id, "share should return the session")
  1475. },
  1476. "status",
  1477. ),
  1478. http.protected
  1479. .delete("/session/{sessionID}/share", "session.unshare")
  1480. .mutating()
  1481. .seeded((ctx) => ctx.session({ title: "Unshare session" }))
  1482. .at((ctx) => ({ path: route("/session/{sessionID}/share", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1483. .json(
  1484. 200,
  1485. (body, ctx) => {
  1486. object(body)
  1487. check(body.id === ctx.state.id, "unshare should return the session")
  1488. },
  1489. "status",
  1490. ),
  1491. http.protected
  1492. .post("/tui/append-prompt", "tui.appendPrompt")
  1493. .at((ctx) => ({ path: "/tui/append-prompt", headers: ctx.headers(), body: { text: "hello" } }))
  1494. .json(200, boolean, "status"),
  1495. http.protected
  1496. .post("/tui/select-session", "tui.selectSession.invalid")
  1497. .at((ctx) => ({ path: "/tui/select-session", headers: ctx.headers(), body: { sessionID: "invalid" } }))
  1498. .status(400),
  1499. http.protected.post("/tui/open-help", "tui.openHelp").json(200, boolean, "status"),
  1500. http.protected.post("/tui/open-sessions", "tui.openSessions").json(200, boolean, "status"),
  1501. http.protected.post("/tui/open-themes", "tui.openThemes").json(200, boolean, "status"),
  1502. http.protected.post("/tui/open-models", "tui.openModels").json(200, boolean, "status"),
  1503. http.protected.post("/tui/submit-prompt", "tui.submitPrompt").json(200, boolean, "status"),
  1504. http.protected.post("/tui/clear-prompt", "tui.clearPrompt").json(200, boolean, "status"),
  1505. http.protected
  1506. .post("/tui/execute-command", "tui.executeCommand")
  1507. .at((ctx) => ({ path: "/tui/execute-command", headers: ctx.headers(), body: { command: "agent_cycle" } }))
  1508. .json(200, boolean, "status"),
  1509. http.protected
  1510. .post("/tui/show-toast", "tui.showToast")
  1511. .at((ctx) => ({
  1512. path: "/tui/show-toast",
  1513. headers: ctx.headers(),
  1514. body: { title: "Exercise", message: "covered", variant: "info", duration: 1000 },
  1515. }))
  1516. .json(200, boolean, "status"),
  1517. http.protected
  1518. .post("/tui/publish", "tui.publish")
  1519. .at((ctx) => ({
  1520. path: "/tui/publish",
  1521. headers: ctx.headers(),
  1522. body: { type: "tui.prompt.append", properties: { text: "published" } },
  1523. }))
  1524. .json(200, boolean, "status"),
  1525. http.protected
  1526. .post("/tui/select-session", "tui.selectSession")
  1527. .seeded((ctx) => ctx.session({ title: "TUI select" }))
  1528. .at((ctx) => ({ path: "/tui/select-session", headers: ctx.headers(), body: { sessionID: ctx.state.id } }))
  1529. .json(200, boolean, "status"),
  1530. http.protected
  1531. .post("/tui/control/response", "tui.control.response")
  1532. .at((ctx) => ({ path: "/tui/control/response", headers: ctx.headers(), body: { ok: true } }))
  1533. .json(200, boolean, "status"),
  1534. http.protected
  1535. .get("/tui/control/next", "tui.control.next")
  1536. .mutating()
  1537. .seeded((ctx) => ctx.tuiRequest({ path: "/tui/exercise", body: { text: "queued" } }))
  1538. .json(
  1539. 200,
  1540. (body) => {
  1541. object(body)
  1542. check(body.path === "/tui/exercise", "control next should return queued path")
  1543. object(body.body)
  1544. check(body.body.text === "queued", "control next should return queued body")
  1545. },
  1546. "status",
  1547. ),
  1548. http.protected
  1549. .post("/global/upgrade", "global.upgrade")
  1550. .global()
  1551. .probe({ path: "/global/upgrade", body: { target: 1 } })
  1552. .at(() => ({ path: "/global/upgrade", body: { target: 1 } }))
  1553. .status(400),
  1554. ]
  1555. const llmScenarios = new Set([
  1556. "session.init",
  1557. "session.prompt",
  1558. "session.prompt_async",
  1559. "session.command",
  1560. "session.summarize",
  1561. ])
  1562. const main = Effect.gen(function* () {
  1563. yield* Effect.addFinalizer(() => Effect.promise(() => disposeApps()).pipe(Effect.andThen(cleanupExercisePaths)))
  1564. const options = parseOptions(Bun.argv.slice(2))
  1565. const modules = yield* Effect.promise(() => runtime())
  1566. const effectRoutes = routeKeys(OpenApi.fromApi(modules.PublicApi))
  1567. const selected = selectedScenarios(options, scenarios)
  1568. const missing = effectRoutes.filter((route) => !scenarios.some((scenario) => route === routeKey(scenario)))
  1569. const extra = scenarios.filter((scenario) => !effectRoutes.includes(routeKey(scenario)))
  1570. for (const scenario of scenarios) {
  1571. if (scenario.kind === "active" && llmScenarios.has(scenario.name) && !scenario.project?.llm) {
  1572. return yield* Effect.fail(new Error(`${scenario.name} must use TestLLMServer via .withLlm()`))
  1573. }
  1574. }
  1575. printHeader(options, effectRoutes, selected, missing, extra, {
  1576. database: exerciseDatabasePath,
  1577. global: exerciseGlobalRoot,
  1578. })
  1579. const results =
  1580. options.mode === "coverage"
  1581. ? selected.map(coverageResult)
  1582. : yield* Effect.forEach(
  1583. selected,
  1584. (scenario) =>
  1585. Effect.gen(function* () {
  1586. if (options.progress) console.log(`${color.dim}RUN ${routeKey(scenario)} ${scenario.name}${color.reset}`)
  1587. return yield* runScenario(options)(scenario)
  1588. }),
  1589. { concurrency: 1 },
  1590. )
  1591. printResults(results, missing, extra)
  1592. if (results.some((result) => result.status === "fail"))
  1593. return yield* Effect.fail(new Error("one or more scenarios failed"))
  1594. if (options.failOnSkip && results.some((result) => result.status === "skip"))
  1595. return yield* Effect.fail(new Error("one or more scenarios are skipped"))
  1596. if (options.failOnMissing && missing.length > 0)
  1597. return yield* Effect.fail(new Error("one or more routes have no scenario"))
  1598. return undefined
  1599. })
  1600. Effect.runPromise(main.pipe(Effect.provide(TestLLMServer.layer), Effect.scoped)).then(
  1601. () => process.exit(0),
  1602. (error: unknown) => {
  1603. console.error(`${color.red}${message(error)}${color.reset}`)
  1604. process.exit(1)
  1605. },
  1606. )