index.ts 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888
  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/generate-name", "experimental.projectCopy.generateName")
  223. .seeded((ctx) => ctx.project())
  224. .at((ctx) => ({
  225. path: route("/experimental/project/{projectID}/copy/generate-name", { projectID: ctx.state.id }),
  226. headers: ctx.headers(),
  227. body: {},
  228. }))
  229. .json(200, (body) => {
  230. object(body)
  231. check(typeof body.name === "string" && body.name.length > 0, "generated copy name should be non-empty")
  232. }),
  233. http.protected
  234. .post("/experimental/project/{projectID}/copy", "experimental.projectCopy.create")
  235. .seeded((ctx) => ctx.project())
  236. .at((ctx) => ({
  237. path: route("/experimental/project/{projectID}/copy", { projectID: ctx.state.id }),
  238. headers: ctx.headers(),
  239. body: {},
  240. }))
  241. .status(400),
  242. http.protected
  243. .delete("/experimental/project/{projectID}/copy", "experimental.projectCopy.remove")
  244. .seeded((ctx) => ctx.project())
  245. .at((ctx) => ({
  246. path: route("/experimental/project/{projectID}/copy", { projectID: ctx.state.id }),
  247. headers: ctx.headers(),
  248. body: {},
  249. }))
  250. .status(400),
  251. http.protected
  252. .post("/experimental/project/{projectID}/copy/refresh", "experimental.projectCopy.refresh")
  253. .mutating()
  254. .seeded((ctx) => ctx.project())
  255. .at((ctx) => ({
  256. path: route("/experimental/project/{projectID}/copy/refresh", { projectID: ctx.state.id }),
  257. headers: ctx.headers(),
  258. }))
  259. .status(204, undefined, "status"),
  260. http.protected.get("/provider", "provider.list").json(),
  261. http.protected.get("/provider/auth", "provider.auth").json(),
  262. http.protected
  263. .post("/provider/{providerID}/oauth/authorize", "provider.oauth.authorize")
  264. .at((ctx) => ({
  265. path: route("/provider/{providerID}/oauth/authorize", { providerID: "httpapi" }),
  266. headers: ctx.headers(),
  267. body: { method: "bad" },
  268. }))
  269. .status(400),
  270. http.protected
  271. .post("/provider/{providerID}/oauth/callback", "provider.oauth.callback")
  272. .at((ctx) => ({
  273. path: route("/provider/{providerID}/oauth/callback", { providerID: "httpapi" }),
  274. headers: ctx.headers(),
  275. body: { method: "bad" },
  276. }))
  277. .status(400),
  278. http.protected.get("/permission", "permission.list").json(200, array),
  279. http.protected
  280. .post("/permission/{requestID}/reply", "permission.reply.invalid")
  281. .at((ctx) => ({
  282. path: route("/permission/{requestID}/reply", { requestID: "per_httpapi" }),
  283. headers: ctx.headers(),
  284. body: { reply: "bad" },
  285. }))
  286. .status(400),
  287. http.protected
  288. .post("/permission/{requestID}/reply", "permission.reply")
  289. .at((ctx) => ({
  290. path: route("/permission/{requestID}/reply", { requestID: "per_httpapi" }),
  291. headers: ctx.headers(),
  292. body: { reply: "once" },
  293. }))
  294. .json(404, object, "status"),
  295. http.protected.get("/question", "question.list").json(200, array),
  296. http.protected
  297. .post("/question/{requestID}/reply", "question.reply.invalid")
  298. .at((ctx) => ({
  299. path: route("/question/{requestID}/reply", { requestID: "que_httpapi_reply" }),
  300. headers: ctx.headers(),
  301. body: { answers: "Yes" },
  302. }))
  303. .status(400),
  304. http.protected
  305. .post("/question/{requestID}/reply", "question.reply")
  306. .at((ctx) => ({
  307. path: route("/question/{requestID}/reply", { requestID: "que_httpapi_reply" }),
  308. headers: ctx.headers(),
  309. body: { answers: [["Yes"]] },
  310. }))
  311. .json(404, object, "status"),
  312. http.protected
  313. .post("/question/{requestID}/reject", "question.reject")
  314. .at((ctx) => ({
  315. path: route("/question/{requestID}/reject", { requestID: "que_httpapi_reject" }),
  316. headers: ctx.headers(),
  317. }))
  318. .json(404, object, "status"),
  319. http.protected
  320. .get("/file", "file.list")
  321. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  322. .at((ctx) => ({ path: `/file?${new URLSearchParams({ path: "." })}`, headers: ctx.headers() }))
  323. .json(200, array),
  324. http.protected
  325. .get("/file/content", "file.read")
  326. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  327. .at((ctx) => ({ path: `/file/content?${new URLSearchParams({ path: "hello.txt" })}`, headers: ctx.headers() }))
  328. .json(200, (body) => {
  329. object(body)
  330. check(body.content === "hello", `content should match seeded file: ${JSON.stringify(body)}`)
  331. }),
  332. http.protected
  333. .get("/file/content", "file.read.missing")
  334. .at((ctx) => ({ path: `/file/content?${new URLSearchParams({ path: "missing.txt" })}`, headers: ctx.headers() }))
  335. .json(200, (body) => {
  336. object(body)
  337. check(body.type === "text" && body.content === "", "missing file content should return an empty text result")
  338. }),
  339. http.protected.get("/file/status", "file.status").json(200, array),
  340. http.protected
  341. .get("/find", "find.text")
  342. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  343. .at((ctx) => ({ path: `/find?${new URLSearchParams({ pattern: "hello" })}`, headers: ctx.headers() }))
  344. .json(200, array),
  345. http.protected
  346. .get("/find/file", "find.files")
  347. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  348. .at((ctx) => ({
  349. path: `/find/file?${new URLSearchParams({ query: "hello", dirs: "false" })}`,
  350. headers: ctx.headers(),
  351. }))
  352. .json(200, array),
  353. http.protected
  354. .get("/find/symbol", "find.symbols")
  355. .seeded((ctx) => ctx.file("hello.ts", "export const hello = 1\n"))
  356. .at((ctx) => ({ path: `/find/symbol?${new URLSearchParams({ query: "hello" })}`, headers: ctx.headers() }))
  357. .json(200, array),
  358. http.protected
  359. .get("/event", "event.stream")
  360. .stream()
  361. .status(
  362. 200,
  363. (_ctx, result) =>
  364. Effect.sync(() => {
  365. check(result.contentType.includes("text/event-stream"), "event should be an SSE stream")
  366. check(result.text.includes("server.connected"), "event should emit initial connection event")
  367. }),
  368. "status",
  369. ),
  370. http.protected.get("/mcp", "mcp.status").json(),
  371. http.protected
  372. .post("/mcp", "mcp.add")
  373. .mutating()
  374. .at((ctx) => ({
  375. path: "/mcp",
  376. headers: ctx.headers(),
  377. body: { name: "httpapi-disabled", config: { type: "local", command: ["bun", "--version"], enabled: false } },
  378. }))
  379. .json(
  380. 200,
  381. (body) => {
  382. object(body)
  383. object(body["httpapi-disabled"])
  384. check(body["httpapi-disabled"].status === "disabled", "disabled MCP server should be added without spawning")
  385. },
  386. "status",
  387. ),
  388. http.protected
  389. .post("/mcp", "mcp.add.invalid")
  390. .at((ctx) => ({
  391. path: "/mcp",
  392. headers: ctx.headers(),
  393. body: { name: "httpapi-invalid", config: { type: "invalid" } },
  394. }))
  395. .status(400),
  396. http.protected
  397. .post("/mcp/{name}/auth", "mcp.auth.start")
  398. .at((ctx) => ({ path: route("/mcp/{name}/auth", { name: "httpapi-missing" }), headers: ctx.headers() }))
  399. .json(404, object, "status"),
  400. http.protected
  401. .delete("/mcp/{name}/auth", "mcp.auth.remove")
  402. .mutating()
  403. .at((ctx) => ({ path: route("/mcp/{name}/auth", { name: "httpapi-missing" }), headers: ctx.headers() }))
  404. .json(404, object, "status"),
  405. http.protected
  406. .post("/mcp/{name}/auth/authenticate", "mcp.auth.authenticate")
  407. .at((ctx) => ({
  408. path: route("/mcp/{name}/auth/authenticate", { name: "httpapi-missing" }),
  409. headers: ctx.headers(),
  410. }))
  411. .json(404, object, "status"),
  412. http.protected
  413. .post("/mcp/{name}/auth/callback", "mcp.auth.callback")
  414. .at((ctx) => ({
  415. path: route("/mcp/{name}/auth/callback", { name: "httpapi-missing" }),
  416. headers: ctx.headers(),
  417. body: { code: "code" },
  418. }))
  419. .json(404, object, "status"),
  420. http.protected
  421. .post("/mcp/{name}/connect", "mcp.connect")
  422. .mutating()
  423. .at((ctx) => ({ path: route("/mcp/{name}/connect", { name: "httpapi-missing" }), headers: ctx.headers() }))
  424. .json(404, object, "status"),
  425. http.protected
  426. .post("/mcp/{name}/disconnect", "mcp.disconnect")
  427. .mutating()
  428. .at((ctx) => ({ path: route("/mcp/{name}/disconnect", { name: "httpapi-missing" }), headers: ctx.headers() }))
  429. .json(404, object, "status"),
  430. http.protected.get("/pty/shells", "pty.shells").json(200, array),
  431. http.protected.get("/pty", "pty.list").json(200, array),
  432. http.protected
  433. .post("/pty", "pty.create")
  434. .mutating()
  435. .at((ctx) => ({ path: "/pty", headers: ctx.headers(), body: controlledPtyInput("HTTP API PTY") }))
  436. .json(
  437. 200,
  438. (body, ctx) => {
  439. object(body)
  440. check(body.title === "HTTP API PTY", "PTY create should return requested title")
  441. check(body.command === "/bin/sh", "PTY create should use controlled shell command")
  442. check(body.cwd === ctx.directory, "PTY create should default cwd to scenario directory")
  443. },
  444. "status",
  445. ),
  446. http.protected
  447. .post("/pty", "pty.create.invalid")
  448. .at((ctx) => ({ path: "/pty", headers: ctx.headers(), body: { command: 1 } }))
  449. .status(400),
  450. http.protected
  451. .post("/pty/{ptyID}/connect-token", "pty.connectToken.invalid")
  452. .at((ctx) => ({
  453. path: route("/pty/{ptyID}/connect-token", { ptyID: "pty_httpapi_missing" }),
  454. headers: ctx.headers(),
  455. }))
  456. .status(403, undefined, "status"),
  457. http.protected
  458. .get("/pty/{ptyID}", "pty.get")
  459. .at((ctx) => ({ path: route("/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  460. .status(404),
  461. http.protected
  462. .put("/pty/{ptyID}", "pty.update")
  463. .mutating()
  464. .at((ctx) => ({
  465. path: route("/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }),
  466. headers: ctx.headers(),
  467. body: { size: { rows: 0, cols: 0 } },
  468. }))
  469. .status(400),
  470. http.protected
  471. .delete("/pty/{ptyID}", "pty.remove")
  472. .mutating()
  473. .at((ctx) => ({ path: route("/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  474. .json(404, object, "status"),
  475. http.protected
  476. .get("/pty/{ptyID}/connect", "pty.connect")
  477. .at((ctx) => ({ path: route("/pty/{ptyID}/connect", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  478. .status(404, undefined, "none"),
  479. http.protected.get("/experimental/console", "experimental.console.get").json(),
  480. http.protected.get("/experimental/console/orgs", "experimental.console.listOrgs").json(),
  481. http.protected
  482. .post("/experimental/console/switch", "experimental.console.switchOrg")
  483. .at((ctx) => ({
  484. path: "/experimental/console/switch",
  485. headers: ctx.headers(),
  486. body: { accountID: "httpapi-account", orgID: "httpapi-org" },
  487. }))
  488. .status(400, undefined, "none"),
  489. http.protected.get("/experimental/workspace/adapter", "experimental.workspace.adapter.list").json(200, array),
  490. http.protected.get("/experimental/workspace", "experimental.workspace.list").json(200, array),
  491. http.protected.get("/experimental/workspace/status", "experimental.workspace.status").json(200, array),
  492. http.protected
  493. .post("/experimental/workspace", "experimental.workspace.create")
  494. .at((ctx) => ({ path: "/experimental/workspace", headers: ctx.headers(), body: {} }))
  495. .status(400),
  496. http.protected
  497. .post("/experimental/workspace/sync-list", "experimental.workspace.syncList")
  498. .status(204, undefined, "status"),
  499. http.protected
  500. .delete("/experimental/workspace/{id}", "experimental.workspace.remove")
  501. .mutating()
  502. .at((ctx) => ({
  503. path: route("/experimental/workspace/{id}", { id: "wrk_httpapi_missing" }),
  504. headers: ctx.headers(),
  505. }))
  506. .status(200),
  507. http.protected
  508. .post("/experimental/workspace/warp", "experimental.workspace.warp")
  509. .at((ctx) => ({
  510. path: "/experimental/workspace/warp",
  511. headers: ctx.headers(),
  512. body: {},
  513. }))
  514. .status(400),
  515. http.protected
  516. .post("/experimental/control-plane/move-session", "experimental.controlPlane.moveSession")
  517. .global()
  518. .at(() => ({
  519. path: "/experimental/control-plane/move-session",
  520. body: {},
  521. }))
  522. .status(400),
  523. http.protected
  524. .get("/experimental/tool", "tool.list")
  525. .at((ctx) => ({
  526. path: `/experimental/tool?${new URLSearchParams({ provider: "opencode", model: "test" })}`,
  527. headers: ctx.headers(),
  528. }))
  529. .json(200, array, "status"),
  530. http.protected.get("/experimental/tool/ids", "tool.ids").json(200, array),
  531. http.protected.get("/experimental/worktree", "worktree.list").json(200, array),
  532. http.protected
  533. .post("/experimental/worktree", "worktree.create")
  534. .mutating()
  535. .at((ctx) => ({ path: "/experimental/worktree", headers: ctx.headers(), body: { name: "api-dsl" } }))
  536. .jsonEffect(
  537. 200,
  538. (body, ctx) =>
  539. Effect.gen(function* () {
  540. object(body)
  541. check(typeof body.directory === "string", "created worktree should include directory")
  542. yield* ctx.worktreeRemove(body.directory)
  543. }),
  544. "status",
  545. ),
  546. http.protected
  547. .post("/experimental/worktree", "worktree.create.invalid")
  548. .at((ctx) => ({ path: "/experimental/worktree", headers: ctx.headers(), body: { name: 1 } }))
  549. .status(400),
  550. http.protected
  551. .delete("/experimental/worktree", "worktree.remove")
  552. .mutating()
  553. .seeded((ctx) => ctx.worktree({ name: "api-remove" }))
  554. .at((ctx) => ({ path: "/experimental/worktree", headers: ctx.headers(), body: { directory: ctx.state.directory } }))
  555. .json(200, (body) => {
  556. check(body === true, "worktree remove should return true")
  557. }),
  558. http.protected
  559. .post("/experimental/worktree/reset", "worktree.reset")
  560. .mutating()
  561. .seeded((ctx) => ctx.worktree({ name: "api-reset" }))
  562. .at((ctx) => ({
  563. path: "/experimental/worktree/reset",
  564. headers: ctx.headers(),
  565. body: { directory: ctx.state.directory },
  566. }))
  567. .jsonEffect(200, (body, ctx) =>
  568. Effect.gen(function* () {
  569. check(body === true, "worktree reset should return true")
  570. yield* ctx.worktreeRemove(ctx.state.directory)
  571. }),
  572. ),
  573. http.protected
  574. .get("/experimental/session", "experimental.session.list")
  575. .at((ctx) => ({ path: "/experimental/session?roots=false&archived=false", headers: ctx.headers() }))
  576. .json(200, array),
  577. http.protected.get("/experimental/capabilities", "experimental.capabilities.get").json(200, (body) => {
  578. check(typeof body === "object" && body !== null, "capabilities should be an object")
  579. check("backgroundSubagents" in body, "capabilities should report background subagents")
  580. }),
  581. http.protected
  582. .post("/experimental/session/{sessionID}/background", "experimental.session.background")
  583. .mutating()
  584. .seeded((ctx) => ctx.session({ title: "Background route owner" }))
  585. .at((ctx) => ({
  586. path: route("/experimental/session/{sessionID}/background", { sessionID: ctx.state.id }),
  587. headers: ctx.headers(),
  588. }))
  589. .json(200, (body) => {
  590. check(body === false, "background route should be a no-op without running subagents")
  591. }),
  592. http.protected.get("/experimental/resource", "experimental.resource.list").json(),
  593. http.protected
  594. .post("/sync/history", "sync.history.list")
  595. .at((ctx) => ({ path: "/sync/history", headers: ctx.headers(), body: {} }))
  596. .json(200, array),
  597. http.protected
  598. .post("/sync/replay", "sync.replay")
  599. .at((ctx) => ({ path: "/sync/replay", headers: ctx.headers(), body: { directory: ctx.directory, events: [] } }))
  600. .status(400),
  601. http.protected
  602. .post("/sync/steal", "sync.steal.invalid")
  603. .at((ctx) => ({ path: "/sync/steal", headers: ctx.headers(), body: {} }))
  604. .status(400, undefined, "status"),
  605. http.protected
  606. .post("/sync/start", "sync.start")
  607. .mutating()
  608. .preserveDatabase()
  609. .json(200, (body) => {
  610. check(body === true, "sync start should return true when no workspace sessions exist")
  611. }),
  612. http.protected
  613. .post("/instance/dispose", "instance.dispose")
  614. .mutating()
  615. .json(200, (body) => {
  616. check(body === true, "instance dispose should return true")
  617. }),
  618. http.protected
  619. .post("/log", "app.log")
  620. .global()
  621. .at(() => ({ path: "/log", body: { service: "httpapi-exercise", level: "info", message: "route coverage" } }))
  622. .json(200, (body) => {
  623. check(body === true, "log route should return true")
  624. }),
  625. http.protected
  626. .put("/auth/{providerID}", "auth.set")
  627. .global()
  628. .at(() => ({ path: route("/auth/{providerID}", { providerID: "test" }), body: { type: "api", key: "test-key" } }))
  629. .jsonEffect(200, (body) =>
  630. Effect.gen(function* () {
  631. check(body === true, "auth set should return true")
  632. const auth = yield* Effect.promise(() => Bun.file(path.join(exerciseDataDirectory, "auth.json")).json())
  633. object(auth)
  634. check(isRecord(auth.test) && auth.test.key === "test-key", "auth set should write isolated auth file")
  635. }),
  636. ),
  637. http.protected
  638. .delete("/auth/{providerID}", "auth.remove")
  639. .global()
  640. .seeded(() =>
  641. Effect.promise(() =>
  642. Bun.write(
  643. path.join(exerciseDataDirectory, "auth.json"),
  644. JSON.stringify({ test: { type: "api", key: "remove-me" } }),
  645. ),
  646. ),
  647. )
  648. .at(() => ({ path: route("/auth/{providerID}", { providerID: "test" }) }))
  649. .jsonEffect(200, (body) =>
  650. Effect.gen(function* () {
  651. check(body === true, "auth remove should return true")
  652. const auth = yield* Effect.promise(() => Bun.file(path.join(exerciseDataDirectory, "auth.json")).json())
  653. object(auth)
  654. check(auth.test === undefined, "auth remove should delete provider from isolated auth file")
  655. }),
  656. ),
  657. http.protected.get("/api/health", "v2.health.get").json(200, (body) => {
  658. object(body)
  659. check(body.healthy === true, "v2 server should report healthy")
  660. }),
  661. http.protected.get("/api/location", "v2.location.get").json(200, object),
  662. http.protected.get("/api/agent", "v2.agent.list").json(200, locationData(array)),
  663. http.protected.get("/api/model", "v2.model.list").json(200, locationData(array)),
  664. // The default model may be undefined in the exercise environment, so only the location envelope is asserted.
  665. http.protected.get("/api/model/default", "v2.model.default").json(200, object),
  666. http.protected.get("/api/provider", "v2.provider.list").json(200, locationData(array)),
  667. http.protected.get("/api/integration", "v2.integration.list").json(200, locationData(array)),
  668. http.protected
  669. .get("/api/integration/{integrationID}", "v2.integration.get")
  670. .at((ctx) => ({
  671. path: route("/api/integration/{integrationID}", { integrationID: "missing" }),
  672. headers: ctx.headers(),
  673. }))
  674. .json(200, object),
  675. http.protected
  676. .post("/api/integration/{integrationID}/connect/key", "v2.integration.connect.key")
  677. .at((ctx) => ({
  678. path: route("/api/integration/{integrationID}/connect/key", { integrationID: "missing" }),
  679. headers: ctx.headers(),
  680. body: { key: "test" },
  681. }))
  682. .status(500, undefined, "status"),
  683. http.protected
  684. .post("/api/integration/{integrationID}/connect/oauth", "v2.integration.connect.oauth")
  685. .at((ctx) => ({
  686. path: route("/api/integration/{integrationID}/connect/oauth", { integrationID: "missing" }),
  687. headers: ctx.headers(),
  688. body: { methodID: "missing", inputs: {} },
  689. }))
  690. .status(500, undefined, "status"),
  691. http.protected
  692. .get("/api/integration/attempt/{attemptID}", "v2.integration.attempt.status")
  693. .at((ctx) => ({
  694. path: route("/api/integration/attempt/{attemptID}", { attemptID: "con_missing" }),
  695. headers: ctx.headers(),
  696. }))
  697. .status(500, undefined, "status"),
  698. http.protected
  699. .post("/api/integration/attempt/{attemptID}/complete", "v2.integration.attempt.complete")
  700. .at((ctx) => ({
  701. path: route("/api/integration/attempt/{attemptID}/complete", { attemptID: "con_missing" }),
  702. headers: ctx.headers(),
  703. body: {},
  704. }))
  705. .status(500, undefined, "status"),
  706. http.protected
  707. .delete("/api/integration/attempt/{attemptID}", "v2.integration.attempt.cancel")
  708. .at((ctx) => ({
  709. path: route("/api/integration/attempt/{attemptID}", { attemptID: "con_missing" }),
  710. headers: ctx.headers(),
  711. }))
  712. .status(204, undefined, "status"),
  713. http.protected
  714. .delete("/api/credential/{credentialID}", "v2.credential.remove")
  715. .at((ctx) => ({
  716. path: route("/api/credential/{credentialID}", { credentialID: "cred_missing" }),
  717. headers: ctx.headers(),
  718. }))
  719. .status(204, undefined, "status"),
  720. http.protected
  721. .patch("/api/credential/{credentialID}", "v2.credential.update")
  722. .at((ctx) => ({
  723. path: route("/api/credential/{credentialID}", { credentialID: "cred_missing" }),
  724. headers: ctx.headers(),
  725. body: { label: "Work" },
  726. }))
  727. .status(204, undefined, "status"),
  728. http.protected.get("/api/command", "v2.command.list").json(200, locationData(array)),
  729. http.protected.get("/api/skill", "v2.skill.list").json(200, locationData(array)),
  730. http.protected
  731. .get("/api/event", "v2.event.subscribe")
  732. .stream()
  733. .status(
  734. 200,
  735. (_ctx, result) =>
  736. Effect.sync(() => {
  737. check(result.contentType.includes("text/event-stream"), "v2 event should be an SSE stream")
  738. check(result.text.includes("server.connected"), "v2 event should emit initial connection event")
  739. check(!result.text.includes('"location"'), "v2 connection event should not be scoped to a location")
  740. }),
  741. "status",
  742. ),
  743. http.protected
  744. .get("/api/fs/read/*", "v2.fs.read")
  745. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  746. .at((ctx) => ({ path: "/api/fs/read/hello.txt", headers: ctx.headers() }))
  747. .status(
  748. 200,
  749. (_ctx, result) =>
  750. Effect.sync(() => {
  751. check(result.text === "hello\n", "v2 fs read should return the file body")
  752. check(result.contentType.includes("text/plain"), "v2 fs read should return the file content type")
  753. }),
  754. "status",
  755. ),
  756. http.protected.get("/api/fs/list", "v2.fs.list").json(200, locationData(array)),
  757. http.protected
  758. .get("/api/fs/find", "v2.fs.find")
  759. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  760. .at((ctx) => ({ path: "/api/fs/find?query=hello&type=file", headers: ctx.headers() }))
  761. .json(200, locationData(array)),
  762. http.protected.get("/api/pty", "v2.pty.list").json(200, locationData(array)),
  763. http.protected
  764. .post("/api/pty", "v2.pty.create")
  765. .mutating()
  766. .at((ctx) => ({ path: "/api/pty", headers: ctx.headers(), body: controlledPtyInput("HTTP API V2 PTY") }))
  767. .json(200, locationData(object)),
  768. http.protected
  769. .get("/api/pty/{ptyID}", "v2.pty.get")
  770. .at((ctx) => ({ path: route("/api/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  771. .json(404, object, "status"),
  772. http.protected
  773. .put("/api/pty/{ptyID}", "v2.pty.update")
  774. .mutating()
  775. .at((ctx) => ({
  776. path: route("/api/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }),
  777. headers: ctx.headers(),
  778. body: { title: "missing" },
  779. }))
  780. .json(404, object, "status"),
  781. http.protected
  782. .delete("/api/pty/{ptyID}", "v2.pty.remove")
  783. .mutating()
  784. .at((ctx) => ({ path: route("/api/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  785. .json(404, object, "status"),
  786. http.protected
  787. .post("/api/pty/{ptyID}/connect-token", "v2.pty.connectToken")
  788. .at((ctx) => ({
  789. path: route("/api/pty/{ptyID}/connect-token", { ptyID: "pty_httpapi_missing" }),
  790. headers: { ...ctx.headers(), "x-opencode-ticket": "1" },
  791. }))
  792. .json(404, object, "status"),
  793. http.protected
  794. .get("/api/pty/{ptyID}/connect", "v2.pty.connect")
  795. .at((ctx) => ({
  796. path: route("/api/pty/{ptyID}/connect", { ptyID: "pty_httpapi_missing" }),
  797. headers: ctx.headers(),
  798. }))
  799. .status(404, undefined, "none"),
  800. http.protected.get("/api/reference", "v2.reference.list").json(200, object),
  801. http.protected
  802. .get("/api/provider/{providerID}", "v2.provider.get")
  803. .at((ctx) => ({ path: route("/api/provider/{providerID}", { providerID: "missing" }), headers: ctx.headers() }))
  804. .json(404, object, "status"),
  805. http.protected.get("/api/permission/request", "v2.permission.request.list").json(200, (body) => {
  806. object(body)
  807. object(body.location)
  808. array(body.data)
  809. }),
  810. http.protected.get("/api/question/request", "v2.question.request.list").json(200, (body) => {
  811. object(body)
  812. object(body.location)
  813. array(body.data)
  814. }),
  815. http.protected.get("/api/form/request", "v2.form.request.list").json(200, (body) => {
  816. object(body)
  817. object(body.location)
  818. array(body.data)
  819. }),
  820. http.protected
  821. .get("/api/session/{sessionID}/form", "v2.session.form.list")
  822. .seeded((ctx) => ctx.session({ title: "Form list owner" }))
  823. .at((ctx) => ({
  824. path: route("/api/session/{sessionID}/form", { sessionID: ctx.state.id }),
  825. headers: ctx.headers(),
  826. }))
  827. .json(200, data(array)),
  828. http.protected
  829. .post("/api/session/{sessionID}/form", "v2.session.form.create")
  830. .mutating()
  831. .seeded((ctx) => ctx.session({ title: "Form create owner" }))
  832. .at((ctx) => ({
  833. path: route("/api/session/{sessionID}/form", { sessionID: ctx.state.id }),
  834. headers: ctx.headers(),
  835. body: { mode: "url", url: "https://example.com/form" },
  836. }))
  837. .json(200, (body) => {
  838. object(body)
  839. object(body.data)
  840. check(typeof body.data.id === "string", "form create should return an ID")
  841. check(body.data.mode === "url", "form create should preserve URL mode")
  842. }),
  843. http.protected
  844. .get("/api/session/{sessionID}/form/{formID}", "v2.session.form.get")
  845. .seeded((ctx) => ctx.session({ title: "Form get owner" }))
  846. .at((ctx) => ({
  847. path: route("/api/session/{sessionID}/form/{formID}", { sessionID: ctx.state.id, formID: "frm_httpapi_missing" }),
  848. headers: ctx.headers(),
  849. }))
  850. .json(404, object, "status"),
  851. http.protected
  852. .get("/api/session/{sessionID}/form/{formID}/state", "v2.session.form.state")
  853. .seeded((ctx) => ctx.session({ title: "Form state owner" }))
  854. .at((ctx) => ({
  855. path: route("/api/session/{sessionID}/form/{formID}/state", {
  856. sessionID: ctx.state.id,
  857. formID: "frm_httpapi_missing",
  858. }),
  859. headers: ctx.headers(),
  860. }))
  861. .json(404, object, "status"),
  862. http.protected
  863. .post("/api/session/{sessionID}/form/{formID}/reply", "v2.session.form.reply")
  864. .mutating()
  865. .seeded((ctx) => ctx.session({ title: "Form reply owner" }))
  866. .at((ctx) => ({
  867. path: route("/api/session/{sessionID}/form/{formID}/reply", {
  868. sessionID: ctx.state.id,
  869. formID: "frm_httpapi_missing",
  870. }),
  871. headers: ctx.headers(),
  872. body: { answer: {} },
  873. }))
  874. .json(404, object, "status"),
  875. http.protected
  876. .post("/api/session/{sessionID}/form/{formID}/cancel", "v2.session.form.cancel")
  877. .mutating()
  878. .seeded((ctx) => ctx.session({ title: "Form cancel owner" }))
  879. .at((ctx) => ({
  880. path: route("/api/session/{sessionID}/form/{formID}/cancel", {
  881. sessionID: ctx.state.id,
  882. formID: "frm_httpapi_missing",
  883. }),
  884. headers: ctx.headers(),
  885. }))
  886. .json(404, object, "status"),
  887. http.protected
  888. .post("/api/session/{sessionID}/permission", "v2.session.permission.create")
  889. .seeded((ctx) => ctx.session({ title: "Permission create owner" }))
  890. .at((ctx) => ({
  891. path: route("/api/session/{sessionID}/permission", { sessionID: ctx.state.id }),
  892. headers: ctx.headers(),
  893. body: { action: "read", resources: [".env"] },
  894. }))
  895. .json(200, (body) => {
  896. object(body)
  897. object(body.data)
  898. check(typeof body.data.id === "string", "permission create should return an ID")
  899. check(body.data.effect === "ask", "permission create should create a pending request")
  900. }),
  901. http.protected
  902. .get("/api/session/{sessionID}/permission", "v2.session.permission.list")
  903. .seeded((ctx) => ctx.session({ title: "Permission list owner" }))
  904. .at((ctx) => ({
  905. path: route("/api/session/{sessionID}/permission", { sessionID: ctx.state.id }),
  906. headers: ctx.headers(),
  907. }))
  908. .json(200, data(array)),
  909. http.protected
  910. .get("/api/session/{sessionID}/permission/{requestID}", "v2.session.permission.get")
  911. .seeded((ctx) => ctx.session({ title: "Permission get owner" }))
  912. .at((ctx) => ({
  913. path: route("/api/session/{sessionID}/permission/{requestID}", {
  914. sessionID: ctx.state.id,
  915. requestID: "per_httpapi_missing",
  916. }),
  917. headers: ctx.headers(),
  918. }))
  919. .json(404, object, "status"),
  920. http.protected
  921. .get("/api/session/{sessionID}/question", "v2.session.question.list")
  922. .seeded((ctx) => ctx.session({ title: "Question list owner" }))
  923. .at((ctx) => ({
  924. path: route("/api/session/{sessionID}/question", { sessionID: ctx.state.id }),
  925. headers: ctx.headers(),
  926. }))
  927. .json(200, data(array)),
  928. http.protected
  929. .post("/api/session/{sessionID}/permission/{requestID}/reply", "v2.session.permission.reply")
  930. .seeded((ctx) => ctx.session({ title: "Permission owner" }))
  931. .at((ctx) => ({
  932. path: route("/api/session/{sessionID}/permission/{requestID}/reply", {
  933. sessionID: ctx.state.id,
  934. requestID: "per_httpapi_missing",
  935. }),
  936. headers: ctx.headers(),
  937. body: { reply: "once" },
  938. }))
  939. .json(404, object, "status"),
  940. http.protected
  941. .post("/api/session/{sessionID}/question/{requestID}/reply", "v2.session.question.reply")
  942. .seeded((ctx) => ctx.session({ title: "Question reply owner" }))
  943. .at((ctx) => ({
  944. path: route("/api/session/{sessionID}/question/{requestID}/reply", {
  945. sessionID: ctx.state.id,
  946. requestID: "que_httpapi_missing",
  947. }),
  948. headers: ctx.headers(),
  949. body: { answers: [] },
  950. }))
  951. .json(404, object, "status"),
  952. http.protected
  953. .post("/api/session/{sessionID}/question/{requestID}/reject", "v2.session.question.reject")
  954. .seeded((ctx) => ctx.session({ title: "Question reject owner" }))
  955. .at((ctx) => ({
  956. path: route("/api/session/{sessionID}/question/{requestID}/reject", {
  957. sessionID: ctx.state.id,
  958. requestID: "que_httpapi_missing",
  959. }),
  960. headers: ctx.headers(),
  961. }))
  962. .json(404, object, "status"),
  963. http.protected.get("/api/permission/saved", "v2.permission.saved.list").json(200, (body) => {
  964. object(body)
  965. array(body.data)
  966. }),
  967. http.protected
  968. .delete("/api/permission/saved/{id}", "v2.permission.saved.remove")
  969. .at((ctx) => ({ path: route("/api/permission/saved/{id}", { id: "psv_httpapi_missing" }), headers: ctx.headers() }))
  970. .status(204, undefined, "status"),
  971. http.protected
  972. .get("/api/session", "v2.session.list")
  973. .at((ctx) => ({ path: "/api/session?roots=true", headers: ctx.headers() }))
  974. .json(
  975. 200,
  976. (body) => {
  977. object(body)
  978. array(body.data)
  979. object(body.cursor)
  980. },
  981. "none",
  982. ),
  983. http.protected
  984. .get("/api/session", "v2.session.list.filters")
  985. .at((ctx) => ({
  986. path: `/api/session?${new URLSearchParams({
  987. limit: "2",
  988. order: "asc",
  989. path: ".",
  990. roots: "false",
  991. start: "0",
  992. search: "missing",
  993. directory: ctx.directory ?? "",
  994. })}`,
  995. headers: ctx.headers(),
  996. }))
  997. .json(
  998. 200,
  999. (body) => {
  1000. object(body)
  1001. array(body.data)
  1002. object(body.cursor)
  1003. },
  1004. "none",
  1005. ),
  1006. http.protected
  1007. .get("/api/session", "v2.session.list.cursor")
  1008. .at((ctx) => ({
  1009. path: `/api/session?${new URLSearchParams({
  1010. limit: "2",
  1011. cursor: cursor({
  1012. order: "desc",
  1013. directory: ctx.directory,
  1014. anchor: { id: "ses_httpapi_missing", time: 0, direction: "next" },
  1015. }),
  1016. })}`,
  1017. headers: ctx.headers(),
  1018. }))
  1019. .json(
  1020. 200,
  1021. (body) => {
  1022. object(body)
  1023. array(body.data)
  1024. object(body.cursor)
  1025. },
  1026. "none",
  1027. ),
  1028. http.protected
  1029. .get("/api/session", "v2.session.list.cursor.invalid")
  1030. .at((ctx) => ({
  1031. path: `/api/session?${new URLSearchParams({
  1032. cursor: "invalid",
  1033. })}`,
  1034. headers: ctx.headers(),
  1035. }))
  1036. .status(400, undefined, "none"),
  1037. http.protected.get("/api/session/active", "v2.session.active").json(200, data(object), "none"),
  1038. http.protected
  1039. .post("/api/session", "v2.session.create")
  1040. .at((ctx) => ({
  1041. path: "/api/session",
  1042. headers: { ...ctx.headers(), "content-type": "application/json" },
  1043. body: {},
  1044. }))
  1045. .json(200, data(object)),
  1046. http.protected
  1047. .get("/api/session/{sessionID}", "v2.session.get")
  1048. .seeded((ctx) => ctx.session({ title: "Session get" }))
  1049. .at((ctx) => ({
  1050. path: route("/api/session/{sessionID}", { sessionID: ctx.state.id }),
  1051. headers: ctx.headers(),
  1052. }))
  1053. .json(200, data(object)),
  1054. http.protected
  1055. .post("/api/session/{sessionID}/agent", "v2.session.switchAgent")
  1056. .seeded((ctx) => ctx.session({ title: "Switch agent" }))
  1057. .at((ctx) => ({
  1058. path: route("/api/session/{sessionID}/agent", { sessionID: ctx.state.id }),
  1059. headers: { ...ctx.headers(), "content-type": "application/json" },
  1060. body: { agent: "plan" },
  1061. }))
  1062. .status(204, undefined, "none"),
  1063. http.protected
  1064. .post("/api/session/{sessionID}/model", "v2.session.switchModel")
  1065. .seeded((ctx) => ctx.session({ title: "Switch model" }))
  1066. .at((ctx) => ({
  1067. path: route("/api/session/{sessionID}/model", { sessionID: ctx.state.id }),
  1068. headers: { ...ctx.headers(), "content-type": "application/json" },
  1069. body: { model: { providerID: "opencode", id: "big-pickle" } },
  1070. }))
  1071. .status(204, undefined, "none"),
  1072. http.protected
  1073. .get("/api/session/{sessionID}/context", "v2.session.context")
  1074. .at((ctx) => ({
  1075. path: route("/api/session/{sessionID}/context", { sessionID: "ses_httpapi_missing" }),
  1076. headers: ctx.headers(),
  1077. }))
  1078. .json(404, object, "status"),
  1079. http.protected
  1080. .post("/api/session/{sessionID}/revert/stage", "v2.session.revert.stage")
  1081. .at((ctx) => ({
  1082. path: route("/api/session/{sessionID}/revert/stage", { sessionID: "ses_httpapi_missing" }),
  1083. headers: { ...ctx.headers(), "content-type": "application/json" },
  1084. body: { messageID: "msg_httpapi_missing" },
  1085. }))
  1086. .json(404, object, "status"),
  1087. http.protected
  1088. .post("/api/session/{sessionID}/revert/clear", "v2.session.revert.clear")
  1089. .at((ctx) => ({
  1090. path: route("/api/session/{sessionID}/revert/clear", { sessionID: "ses_httpapi_missing" }),
  1091. headers: ctx.headers(),
  1092. }))
  1093. .json(404, object, "status"),
  1094. http.protected
  1095. .post("/api/session/{sessionID}/revert/commit", "v2.session.revert.commit")
  1096. .at((ctx) => ({
  1097. path: route("/api/session/{sessionID}/revert/commit", { sessionID: "ses_httpapi_missing" }),
  1098. headers: ctx.headers(),
  1099. }))
  1100. .json(404, object, "status"),
  1101. http.protected
  1102. .get("/api/session/{sessionID}/message", "v2.session.messages")
  1103. .at((ctx) => ({
  1104. path: route("/api/session/{sessionID}/message", { sessionID: "ses_httpapi_missing" }),
  1105. headers: ctx.headers(),
  1106. }))
  1107. .json(404, object, "status"),
  1108. http.protected
  1109. .get("/api/session/{sessionID}/message", "v2.session.messages.params")
  1110. .at((ctx) => ({
  1111. path: `${route("/api/session/{sessionID}/message", { sessionID: "ses_httpapi_missing" })}?${new URLSearchParams({
  1112. limit: "2",
  1113. order: "asc",
  1114. })}`,
  1115. headers: ctx.headers(),
  1116. }))
  1117. .json(404, object, "status"),
  1118. http.protected
  1119. .get("/api/session/{sessionID}/message", "v2.session.messages.cursor")
  1120. .at((ctx) => ({
  1121. path: `${route("/api/session/{sessionID}/message", { sessionID: "ses_httpapi_missing" })}?${new URLSearchParams({
  1122. limit: "2",
  1123. directory: ctx.directory ?? "",
  1124. cursor: cursor({ id: "msg_httpapi_missing", time: 0, order: "desc", direction: "next" }),
  1125. })}`,
  1126. headers: ctx.headers(),
  1127. }))
  1128. .json(404, object, "status"),
  1129. http.protected
  1130. .get("/api/session/{sessionID}/message", "v2.session.messages.cursor.invalid")
  1131. .seeded((ctx) => ctx.session({ title: "Invalid message cursor owner" }))
  1132. .at((ctx) => ({
  1133. path: `${route("/api/session/{sessionID}/message", { sessionID: ctx.state.id })}?${new URLSearchParams({
  1134. cursor: cursor({ id: "msg_httpapi_missing", time: 0, order: "desc", direction: "next" }),
  1135. order: "asc",
  1136. })}`,
  1137. headers: ctx.headers(),
  1138. }))
  1139. .status(400, undefined, "none"),
  1140. http.protected
  1141. .get("/api/session/{sessionID}/history", "v2.session.history")
  1142. .seeded((ctx) => ctx.session({ title: "Session history" }))
  1143. .at((ctx) => ({
  1144. path: `${route("/api/session/{sessionID}/history", { sessionID: ctx.state.id })}?${new URLSearchParams({
  1145. after: "0",
  1146. limit: "2",
  1147. })}`,
  1148. headers: ctx.headers(),
  1149. }))
  1150. .json(
  1151. 200,
  1152. (body) => {
  1153. object(body)
  1154. array(body.data)
  1155. check(typeof body.hasMore === "boolean", "Expected a history exhaustion signal")
  1156. },
  1157. "none",
  1158. ),
  1159. http.protected
  1160. .get("/api/session/{sessionID}/history", "v2.session.history.missing")
  1161. .at((ctx) => ({
  1162. path: route("/api/session/{sessionID}/history", { sessionID: "ses_httpapi_missing" }),
  1163. headers: ctx.headers(),
  1164. }))
  1165. .json(404, object, "status"),
  1166. http.protected
  1167. .get("/api/session/{sessionID}/history", "v2.session.history.invalid")
  1168. .seeded((ctx) => ctx.session({ title: "Invalid history sequence" }))
  1169. .at((ctx) => ({
  1170. path: `${route("/api/session/{sessionID}/history", { sessionID: ctx.state.id })}?after=-1`,
  1171. headers: ctx.headers(),
  1172. }))
  1173. .json(400, object, "status"),
  1174. http.protected
  1175. .get("/api/session/{sessionID}/event", "v2.session.events.missing")
  1176. .at((ctx) => ({
  1177. path: `${route("/api/session/{sessionID}/event", { sessionID: "ses_httpapi_missing" })}?after=0`,
  1178. headers: ctx.headers(),
  1179. }))
  1180. .status(404, undefined, "status"),
  1181. http.protected
  1182. .post("/api/session/{sessionID}/interrupt", "v2.session.interrupt")
  1183. .seeded((ctx) => ctx.session({ title: "Interrupt session" }))
  1184. .at((ctx) => ({
  1185. path: route("/api/session/{sessionID}/interrupt", { sessionID: ctx.state.id }),
  1186. headers: ctx.headers(),
  1187. }))
  1188. .status(204, undefined, "none"),
  1189. http.protected
  1190. .get("/api/session/{sessionID}/message/{messageID}", "v2.session.message.missing")
  1191. .at((ctx) => ({
  1192. path: route("/api/session/{sessionID}/message/{messageID}", {
  1193. sessionID: "ses_httpapi_missing",
  1194. messageID: "msg_httpapi_missing",
  1195. }),
  1196. headers: ctx.headers(),
  1197. }))
  1198. .json(404, object, "status"),
  1199. http.protected
  1200. .post("/api/session/{sessionID}/prompt", "v2.session.prompt.invalid")
  1201. .seeded((ctx) => ctx.session({ title: "Invalid prompt owner" }))
  1202. .at((ctx) => ({
  1203. path: route("/api/session/{sessionID}/prompt", { sessionID: ctx.state.id }),
  1204. headers: ctx.headers(),
  1205. body: {},
  1206. }))
  1207. .status(400, undefined, "none"),
  1208. http.protected
  1209. .post("/api/session/{sessionID}/compact", "v2.session.compact")
  1210. .at((ctx) => ({
  1211. path: route("/api/session/{sessionID}/compact", { sessionID: "ses_httpapi_missing" }),
  1212. headers: ctx.headers(),
  1213. }))
  1214. .status(404, undefined, "status"),
  1215. http.protected
  1216. .post("/api/session/{sessionID}/wait", "v2.session.wait")
  1217. .at((ctx) => ({
  1218. path: route("/api/session/{sessionID}/wait", { sessionID: "ses_httpapi_missing" }),
  1219. headers: ctx.headers(),
  1220. }))
  1221. .status(404, undefined, "status"),
  1222. http.protected
  1223. .get("/session", "session.list")
  1224. .seeded((ctx) => ctx.session({ title: "List me" }))
  1225. .at((ctx) => ({ path: "/session?roots=true", headers: ctx.headers() }))
  1226. .json(200, (body, ctx) => {
  1227. array(body)
  1228. check(
  1229. body.some((item) => isRecord(item) && item.id === ctx.state.id && item.title === "List me"),
  1230. "seeded session should be listed",
  1231. )
  1232. }),
  1233. http.protected
  1234. .get("/session/status", "session.status")
  1235. .seeded((ctx) => ctx.session({ title: "Status session" }))
  1236. .json(200, object),
  1237. http.protected
  1238. .post("/session", "session.create")
  1239. .mutating()
  1240. .at((ctx) => ({ path: "/session", headers: ctx.headers(), body: { title: "Created session" } }))
  1241. .json(
  1242. 200,
  1243. (body, ctx) => {
  1244. object(body)
  1245. check(body.title === "Created session", "created session should use requested title")
  1246. check(body.directory === ctx.directory, "created session should use scenario directory")
  1247. },
  1248. "status",
  1249. ),
  1250. http.protected
  1251. .get("/session/{sessionID}", "session.get")
  1252. .seeded((ctx) => ctx.session({ title: "Get me" }))
  1253. .at((ctx) => ({ path: route("/session/{sessionID}", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1254. .json(200, (body, ctx) => {
  1255. object(body)
  1256. check(body.id === ctx.state.id, "should return requested session")
  1257. check(body.title === "Get me", "should preserve seeded title")
  1258. }),
  1259. http.protected
  1260. .get("/session/{sessionID}", "session.get.missing")
  1261. .at((ctx) => ({
  1262. path: route("/session/{sessionID}", { sessionID: "ses_httpapi_missing" }),
  1263. headers: ctx.headers(),
  1264. }))
  1265. .status(404),
  1266. http.protected
  1267. .patch("/session/{sessionID}", "session.update")
  1268. .mutating()
  1269. .seeded((ctx) => ctx.session({ title: "Before rename" }))
  1270. .at((ctx) => ({
  1271. path: route("/session/{sessionID}", { sessionID: ctx.state.id }),
  1272. headers: ctx.headers(),
  1273. body: { title: "After rename" },
  1274. }))
  1275. .json(
  1276. 200,
  1277. (body) => {
  1278. object(body)
  1279. check(body.title === "After rename", "updated session should use new title")
  1280. },
  1281. "status",
  1282. ),
  1283. http.protected
  1284. .patch("/session/{sessionID}", "session.update.invalid")
  1285. .mutating()
  1286. .at((ctx) => ({
  1287. path: route("/session/{sessionID}", { sessionID: "ses_httpapi_missing" }),
  1288. headers: ctx.headers(),
  1289. body: { title: 1 },
  1290. }))
  1291. .status(400),
  1292. http.protected
  1293. .delete("/session/{sessionID}", "session.delete")
  1294. .mutating()
  1295. .seeded((ctx) => ctx.session({ title: "Delete me" }))
  1296. .at((ctx) => ({ path: route("/session/{sessionID}", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1297. .jsonEffect(200, (body, ctx) =>
  1298. Effect.gen(function* () {
  1299. check(body === true, "delete should return true")
  1300. check((yield* ctx.sessionGet(ctx.state.id)) === undefined, "deleted session should not remain in storage")
  1301. }),
  1302. ),
  1303. http.protected
  1304. .get("/session/{sessionID}/children", "session.children")
  1305. .seeded((ctx) =>
  1306. Effect.gen(function* () {
  1307. const parent = yield* ctx.session({ title: "Parent" })
  1308. const child = yield* ctx.session({ title: "Child", parentID: parent.id })
  1309. return { parent, child }
  1310. }),
  1311. )
  1312. .at((ctx) => ({
  1313. path: route("/session/{sessionID}/children", { sessionID: ctx.state.parent.id }),
  1314. headers: ctx.headers(),
  1315. }))
  1316. .json(200, (body, ctx) => {
  1317. array(body)
  1318. check(
  1319. body.some((item) => isRecord(item) && item.id === ctx.state.child.id && item.parentID === ctx.state.parent.id),
  1320. "children should include seeded child",
  1321. )
  1322. }),
  1323. http.protected
  1324. .get("/session/{sessionID}/todo", "session.todo")
  1325. .seeded((ctx) =>
  1326. Effect.gen(function* () {
  1327. const session = yield* ctx.session({ title: "Todo session" })
  1328. const todos = [{ content: "cover session todo", status: "pending" as const, priority: "high" as const }]
  1329. yield* ctx.todos(session.id, todos)
  1330. return { session, todos }
  1331. }),
  1332. )
  1333. .at((ctx) => ({
  1334. path: route("/session/{sessionID}/todo", { sessionID: ctx.state.session.id }),
  1335. headers: ctx.headers(),
  1336. }))
  1337. .json(200, (body, ctx) => {
  1338. check(stable(body) === stable(ctx.state.todos), "todos should match seeded state")
  1339. }),
  1340. http.protected
  1341. .get("/session/{sessionID}/diff", "session.diff")
  1342. .seeded((ctx) => ctx.session({ title: "Diff session" }))
  1343. .at((ctx) => ({ path: route("/session/{sessionID}/diff", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1344. .json(200, array),
  1345. http.protected
  1346. .get("/session/{sessionID}/message", "session.messages")
  1347. .seeded((ctx) => ctx.session({ title: "Messages session" }))
  1348. .at((ctx) => ({ path: route("/session/{sessionID}/message", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1349. .json(200, (body) => {
  1350. array(body)
  1351. check(body.length === 0, "new session should have no messages")
  1352. }),
  1353. http.protected
  1354. .get("/session/{sessionID}/message/{messageID}", "session.message")
  1355. .seeded((ctx) =>
  1356. Effect.gen(function* () {
  1357. const session = yield* ctx.session({ title: "Message get session" })
  1358. const message = yield* ctx.message(session.id, { text: "read me" })
  1359. return { session, message }
  1360. }),
  1361. )
  1362. .at((ctx) => ({
  1363. path: route("/session/{sessionID}/message/{messageID}", {
  1364. sessionID: ctx.state.session.id,
  1365. messageID: ctx.state.message.info.id,
  1366. }),
  1367. headers: ctx.headers(),
  1368. }))
  1369. .json(200, (body, ctx) => {
  1370. object(body)
  1371. check(isRecord(body.info) && body.info.id === ctx.state.message.info.id, "should return requested message")
  1372. check(
  1373. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.id === ctx.state.message.part.id),
  1374. "message should include seeded part",
  1375. )
  1376. }),
  1377. http.protected
  1378. .patch("/session/{sessionID}/message/{messageID}/part/{partID}", "part.update")
  1379. .mutating()
  1380. .seeded((ctx) =>
  1381. Effect.gen(function* () {
  1382. const session = yield* ctx.session({ title: "Part update session" })
  1383. const message = yield* ctx.message(session.id, { text: "before" })
  1384. return { session, message }
  1385. }),
  1386. )
  1387. .at((ctx) => ({
  1388. path: route("/session/{sessionID}/message/{messageID}/part/{partID}", {
  1389. sessionID: ctx.state.session.id,
  1390. messageID: ctx.state.message.info.id,
  1391. partID: ctx.state.message.part.id,
  1392. }),
  1393. headers: ctx.headers(),
  1394. body: { ...ctx.state.message.part, text: "after" },
  1395. }))
  1396. .json(
  1397. 200,
  1398. (body) => {
  1399. object(body)
  1400. check(body.type === "text" && body.text === "after", "updated part should be returned")
  1401. },
  1402. "status",
  1403. ),
  1404. http.protected
  1405. .delete("/session/{sessionID}/message/{messageID}/part/{partID}", "part.delete")
  1406. .mutating()
  1407. .seeded((ctx) =>
  1408. Effect.gen(function* () {
  1409. const session = yield* ctx.session({ title: "Part delete session" })
  1410. const message = yield* ctx.message(session.id, { text: "delete part" })
  1411. return { session, message }
  1412. }),
  1413. )
  1414. .at((ctx) => ({
  1415. path: route("/session/{sessionID}/message/{messageID}/part/{partID}", {
  1416. sessionID: ctx.state.session.id,
  1417. messageID: ctx.state.message.info.id,
  1418. partID: ctx.state.message.part.id,
  1419. }),
  1420. headers: ctx.headers(),
  1421. }))
  1422. .jsonEffect(200, (body, ctx) =>
  1423. Effect.gen(function* () {
  1424. check(body === true, "delete part should return true")
  1425. const messages = yield* ctx.messages(ctx.state.session.id)
  1426. check(messages[0]?.parts.length === 0, "deleted part should not remain on message")
  1427. }),
  1428. ),
  1429. http.protected
  1430. .delete("/session/{sessionID}/message/{messageID}", "session.deleteMessage")
  1431. .mutating()
  1432. .seeded((ctx) =>
  1433. Effect.gen(function* () {
  1434. const session = yield* ctx.session({ title: "Message delete session" })
  1435. const message = yield* ctx.message(session.id, { text: "delete message" })
  1436. return { session, message }
  1437. }),
  1438. )
  1439. .at((ctx) => ({
  1440. path: route("/session/{sessionID}/message/{messageID}", {
  1441. sessionID: ctx.state.session.id,
  1442. messageID: ctx.state.message.info.id,
  1443. }),
  1444. headers: ctx.headers(),
  1445. }))
  1446. .jsonEffect(200, (body, ctx) =>
  1447. Effect.gen(function* () {
  1448. check(body === true, "delete message should return true")
  1449. check((yield* ctx.messages(ctx.state.session.id)).length === 0, "deleted message should not remain")
  1450. }),
  1451. ),
  1452. http.protected
  1453. .post("/session/{sessionID}/fork", "session.fork")
  1454. .mutating()
  1455. .seeded((ctx) => ctx.session({ title: "Fork source" }))
  1456. .at((ctx) => ({
  1457. path: route("/session/{sessionID}/fork", { sessionID: ctx.state.id }),
  1458. headers: ctx.headers(),
  1459. body: {},
  1460. }))
  1461. .json(
  1462. 200,
  1463. (body) => {
  1464. object(body)
  1465. check(typeof body.id === "string", "fork should return a session")
  1466. },
  1467. "status",
  1468. ),
  1469. http.protected
  1470. .post("/session/{sessionID}/abort", "session.abort")
  1471. .mutating()
  1472. .seeded((ctx) => ctx.session({ title: "Abort session" }))
  1473. .at((ctx) => ({ path: route("/session/{sessionID}/abort", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1474. .json(200, (body) => {
  1475. check(body === true, "abort should return true")
  1476. }),
  1477. http.protected
  1478. .post("/session/{sessionID}/abort", "session.abort.missing")
  1479. .at((ctx) => ({
  1480. path: route("/session/{sessionID}/abort", { sessionID: "ses_httpapi_missing" }),
  1481. headers: ctx.headers(),
  1482. }))
  1483. .json(200, (body) => {
  1484. check(body === true, "missing session abort should remain a no-op success")
  1485. }),
  1486. http.protected
  1487. .post("/session/{sessionID}/init", "session.init")
  1488. .preserveDatabase()
  1489. .withLlm()
  1490. .seeded((ctx) =>
  1491. Effect.gen(function* () {
  1492. const session = yield* ctx.session({ title: "Init session" })
  1493. const message = yield* ctx.message(session.id, { text: "initialize" })
  1494. yield* ctx.llmText("initialized")
  1495. yield* ctx.llmText("initialized")
  1496. return { session, message }
  1497. }),
  1498. )
  1499. .at((ctx) => ({
  1500. path: route("/session/{sessionID}/init", { sessionID: ctx.state.session.id }),
  1501. headers: ctx.headers(),
  1502. body: { providerID: "test", modelID: "test-model", messageID: ctx.state.message.info.id },
  1503. }))
  1504. .jsonEffect(200, (body, ctx) =>
  1505. Effect.gen(function* () {
  1506. check(body === true, "init should return true")
  1507. yield* ctx.llmWait(1)
  1508. }),
  1509. ),
  1510. http.protected
  1511. .post("/session/{sessionID}/message", "session.prompt")
  1512. .preserveDatabase()
  1513. .withLlm()
  1514. .seeded((ctx) =>
  1515. Effect.gen(function* () {
  1516. const session = yield* ctx.session({ title: "LLM prompt session" })
  1517. yield* ctx.llmText("fake assistant")
  1518. yield* ctx.llmText("fake assistant")
  1519. return session
  1520. }),
  1521. )
  1522. .at((ctx) => ({
  1523. path: route("/session/{sessionID}/message", { sessionID: ctx.state.id }),
  1524. headers: ctx.headers(),
  1525. body: {
  1526. agent: "build",
  1527. model: { providerID: "test", modelID: "test-model" },
  1528. parts: [{ type: "text", text: "hello llm" }],
  1529. },
  1530. }))
  1531. .jsonEffect(
  1532. 200,
  1533. (body, ctx) =>
  1534. Effect.gen(function* () {
  1535. object(body)
  1536. check(isRecord(body.info) && body.info.role === "assistant", "prompt should return assistant message")
  1537. check(
  1538. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.text === "fake assistant"),
  1539. "assistant message should use fake LLM text",
  1540. )
  1541. yield* ctx.llmWait(1)
  1542. }),
  1543. "status",
  1544. ),
  1545. http.protected
  1546. .post("/session/{sessionID}/prompt_async", "session.prompt_async")
  1547. .preserveDatabase()
  1548. .withLlm()
  1549. .seeded((ctx) =>
  1550. Effect.gen(function* () {
  1551. const session = yield* ctx.session({ title: "Async prompt session" })
  1552. yield* ctx.llmText("fake async assistant")
  1553. yield* ctx.llmText("fake async assistant")
  1554. return session
  1555. }),
  1556. )
  1557. .at((ctx) => ({
  1558. path: route("/session/{sessionID}/prompt_async", { sessionID: ctx.state.id }),
  1559. headers: ctx.headers(),
  1560. body: {
  1561. agent: "build",
  1562. model: { providerID: "test", modelID: "test-model" },
  1563. parts: [{ type: "text", text: "hello async" }],
  1564. },
  1565. }))
  1566. .status(204, (ctx) =>
  1567. Effect.gen(function* () {
  1568. yield* ctx.llmWait(1)
  1569. }),
  1570. ),
  1571. http.protected
  1572. .post("/session/{sessionID}/command", "session.command")
  1573. .preserveDatabase()
  1574. .withLlm()
  1575. .seeded((ctx) =>
  1576. Effect.gen(function* () {
  1577. const session = yield* ctx.session({ title: "Command session" })
  1578. yield* ctx.llmText("command done")
  1579. yield* ctx.llmText("command done")
  1580. return session
  1581. }),
  1582. )
  1583. .at((ctx) => ({
  1584. path: route("/session/{sessionID}/command", { sessionID: ctx.state.id }),
  1585. headers: ctx.headers(),
  1586. body: { command: "init", arguments: "", model: "test/test-model" },
  1587. }))
  1588. .jsonEffect(
  1589. 200,
  1590. (body, ctx) =>
  1591. Effect.gen(function* () {
  1592. object(body)
  1593. check(isRecord(body.info) && body.info.role === "assistant", "command should return assistant message")
  1594. yield* ctx.llmWait(1)
  1595. }),
  1596. "status",
  1597. ),
  1598. http.protected
  1599. .post("/session/{sessionID}/shell", "session.shell")
  1600. .preserveDatabase()
  1601. .mutating()
  1602. .seeded((ctx) => ctx.session({ title: "Shell session" }))
  1603. .at((ctx) => ({
  1604. path: route("/session/{sessionID}/shell", { sessionID: ctx.state.id }),
  1605. headers: ctx.headers(),
  1606. body: { agent: "build", model: { providerID: "test", modelID: "test-model" }, command: "printf shell-ok" },
  1607. }))
  1608. .json(
  1609. 200,
  1610. (body) => {
  1611. object(body)
  1612. check(isRecord(body.info) && body.info.role === "assistant", "shell should return assistant message")
  1613. check(
  1614. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.type === "tool"),
  1615. "shell should return a tool part",
  1616. )
  1617. },
  1618. "status",
  1619. ),
  1620. http.protected
  1621. .post("/session/{sessionID}/summarize", "session.summarize")
  1622. .preserveDatabase()
  1623. .withLlm()
  1624. .seeded((ctx) =>
  1625. Effect.gen(function* () {
  1626. const session = yield* ctx.session({ title: "Summarize session" })
  1627. yield* ctx.message(session.id, { text: "summarize this work" })
  1628. const summary = [
  1629. "## Goal",
  1630. "- Exercise session summarize.",
  1631. "",
  1632. "## Constraints & Preferences",
  1633. "- Use fake LLM.",
  1634. "",
  1635. "## Progress",
  1636. "### Done",
  1637. "- Summary generated.",
  1638. "",
  1639. "### In Progress",
  1640. "- (none)",
  1641. "",
  1642. "### Blocked",
  1643. "- (none)",
  1644. "",
  1645. "## Key Decisions",
  1646. "- Keep route local.",
  1647. "",
  1648. "## Next Steps",
  1649. "- (none)",
  1650. "",
  1651. "## Critical Context",
  1652. "- Test fixture.",
  1653. "",
  1654. "## Relevant Files",
  1655. "- test/server/httpapi-exercise/index.ts: scenario",
  1656. ].join("\n")
  1657. yield* ctx.llmText(summary)
  1658. yield* ctx.llmText(summary)
  1659. return session
  1660. }),
  1661. )
  1662. .at((ctx) => ({
  1663. path: route("/session/{sessionID}/summarize", { sessionID: ctx.state.id }),
  1664. headers: ctx.headers(),
  1665. body: { providerID: "test", modelID: "test-model", auto: false },
  1666. }))
  1667. .jsonEffect(
  1668. 200,
  1669. (body, ctx) =>
  1670. Effect.gen(function* () {
  1671. check(body === true, "summarize should return true")
  1672. const messages = yield* ctx.messages(ctx.state.id)
  1673. check(
  1674. messages.some((message) => message.info.role === "assistant" && message.info.summary === true),
  1675. "summarize should create a summary assistant message",
  1676. )
  1677. yield* ctx.llmWait(1)
  1678. }),
  1679. "status",
  1680. ),
  1681. http.protected
  1682. .post("/session/{sessionID}/revert", "session.revert")
  1683. .mutating()
  1684. .seeded((ctx) =>
  1685. Effect.gen(function* () {
  1686. const session = yield* ctx.session({ title: "Revert session" })
  1687. const message = yield* ctx.message(session.id, { text: "revert me" })
  1688. return { session, message }
  1689. }),
  1690. )
  1691. .at((ctx) => ({
  1692. path: route("/session/{sessionID}/revert", { sessionID: ctx.state.session.id }),
  1693. headers: ctx.headers(),
  1694. body: { messageID: ctx.state.message.info.id },
  1695. }))
  1696. .json(
  1697. 200,
  1698. (body, ctx) => {
  1699. object(body)
  1700. check(body.id === ctx.state.session.id, "revert should return the session")
  1701. check(
  1702. isRecord(body.revert) && body.revert.messageID === ctx.state.message.info.id,
  1703. "revert should record reverted message",
  1704. )
  1705. },
  1706. "status",
  1707. ),
  1708. http.protected
  1709. .post("/session/{sessionID}/unrevert", "session.unrevert")
  1710. .mutating()
  1711. .seeded((ctx) => ctx.session({ title: "Unrevert session" }))
  1712. .at((ctx) => ({
  1713. path: route("/session/{sessionID}/unrevert", { sessionID: ctx.state.id }),
  1714. headers: ctx.headers(),
  1715. }))
  1716. .json(
  1717. 200,
  1718. (body, ctx) => {
  1719. object(body)
  1720. check(body.id === ctx.state.id, "unrevert should return the session")
  1721. },
  1722. "status",
  1723. ),
  1724. http.protected
  1725. .post("/session/{sessionID}/permissions/{permissionID}", "permission.respond")
  1726. .seeded((ctx) => ctx.session({ title: "Deprecated permission session" }))
  1727. .at((ctx) => ({
  1728. path: route("/session/{sessionID}/permissions/{permissionID}", {
  1729. sessionID: ctx.state.id,
  1730. permissionID: "per_httpapi_deprecated",
  1731. }),
  1732. headers: ctx.headers(),
  1733. body: { response: "once" },
  1734. }))
  1735. .json(404, object, "status"),
  1736. http.protected
  1737. .post("/session/{sessionID}/share", "session.share")
  1738. .mutating()
  1739. .seeded((ctx) => ctx.session({ title: "Share session" }))
  1740. .at((ctx) => ({ path: route("/session/{sessionID}/share", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1741. .json(
  1742. 200,
  1743. (body, ctx) => {
  1744. object(body)
  1745. check(body.id === ctx.state.id, "share should return the session")
  1746. },
  1747. "status",
  1748. ),
  1749. http.protected
  1750. .delete("/session/{sessionID}/share", "session.unshare")
  1751. .mutating()
  1752. .seeded((ctx) => ctx.session({ title: "Unshare session" }))
  1753. .at((ctx) => ({ path: route("/session/{sessionID}/share", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1754. .json(
  1755. 200,
  1756. (body, ctx) => {
  1757. object(body)
  1758. check(body.id === ctx.state.id, "unshare should return the session")
  1759. },
  1760. "status",
  1761. ),
  1762. http.protected
  1763. .post("/tui/append-prompt", "tui.appendPrompt")
  1764. .at((ctx) => ({ path: "/tui/append-prompt", headers: ctx.headers(), body: { text: "hello" } }))
  1765. .json(200, boolean, "status"),
  1766. http.protected
  1767. .post("/tui/select-session", "tui.selectSession.invalid")
  1768. .at((ctx) => ({ path: "/tui/select-session", headers: ctx.headers(), body: { sessionID: "invalid" } }))
  1769. .status(400),
  1770. http.protected.post("/tui/open-help", "tui.openHelp").json(200, boolean, "status"),
  1771. http.protected.post("/tui/open-sessions", "tui.openSessions").json(200, boolean, "status"),
  1772. http.protected.post("/tui/open-themes", "tui.openThemes").json(200, boolean, "status"),
  1773. http.protected.post("/tui/open-models", "tui.openModels").json(200, boolean, "status"),
  1774. http.protected.post("/tui/submit-prompt", "tui.submitPrompt").json(200, boolean, "status"),
  1775. http.protected.post("/tui/clear-prompt", "tui.clearPrompt").json(200, boolean, "status"),
  1776. http.protected
  1777. .post("/tui/execute-command", "tui.executeCommand")
  1778. .at((ctx) => ({ path: "/tui/execute-command", headers: ctx.headers(), body: { command: "agent_cycle" } }))
  1779. .json(200, boolean, "status"),
  1780. http.protected
  1781. .post("/tui/show-toast", "tui.showToast")
  1782. .at((ctx) => ({
  1783. path: "/tui/show-toast",
  1784. headers: ctx.headers(),
  1785. body: { title: "Exercise", message: "covered", variant: "info", duration: 1000 },
  1786. }))
  1787. .json(200, boolean, "status"),
  1788. http.protected
  1789. .post("/tui/publish", "tui.publish")
  1790. .at((ctx) => ({
  1791. path: "/tui/publish",
  1792. headers: ctx.headers(),
  1793. body: { type: "tui.prompt.append", properties: { text: "published" } },
  1794. }))
  1795. .json(200, boolean, "status"),
  1796. http.protected
  1797. .post("/tui/select-session", "tui.selectSession")
  1798. .seeded((ctx) => ctx.session({ title: "TUI select" }))
  1799. .at((ctx) => ({ path: "/tui/select-session", headers: ctx.headers(), body: { sessionID: ctx.state.id } }))
  1800. .json(200, boolean, "status"),
  1801. http.protected
  1802. .post("/tui/control/response", "tui.control.response")
  1803. .at((ctx) => ({ path: "/tui/control/response", headers: ctx.headers(), body: { ok: true } }))
  1804. .json(200, boolean, "status"),
  1805. http.protected
  1806. .get("/tui/control/next", "tui.control.next")
  1807. .mutating()
  1808. .seeded((ctx) => ctx.tuiRequest({ path: "/tui/exercise", body: { text: "queued" } }))
  1809. .json(
  1810. 200,
  1811. (body) => {
  1812. object(body)
  1813. check(body.path === "/tui/exercise", "control next should return queued path")
  1814. object(body.body)
  1815. check(body.body.text === "queued", "control next should return queued body")
  1816. },
  1817. "status",
  1818. ),
  1819. http.protected
  1820. .post("/global/upgrade", "global.upgrade")
  1821. .global()
  1822. .probe({ path: "/global/upgrade", body: { target: 1 } })
  1823. .at(() => ({ path: "/global/upgrade", body: { target: 1 } }))
  1824. .status(400),
  1825. ]
  1826. const llmScenarios = new Set([
  1827. "session.init",
  1828. "session.prompt",
  1829. "session.prompt_async",
  1830. "session.command",
  1831. "session.summarize",
  1832. ])
  1833. const main = Effect.gen(function* () {
  1834. yield* Effect.addFinalizer(() => Effect.promise(() => disposeApps()).pipe(Effect.andThen(cleanupExercisePaths)))
  1835. const options = parseOptions(Bun.argv.slice(2))
  1836. const modules = yield* Effect.promise(() => runtime())
  1837. const effectRoutes = routeKeys(OpenApi.fromApi(modules.PublicApi))
  1838. const selected = selectedScenarios(options, scenarios)
  1839. const missing = effectRoutes.filter((route) => !scenarios.some((scenario) => route === routeKey(scenario)))
  1840. const extra = scenarios.filter((scenario) => !effectRoutes.includes(routeKey(scenario)))
  1841. for (const scenario of scenarios) {
  1842. if (scenario.kind === "active" && llmScenarios.has(scenario.name) && !scenario.project?.llm) {
  1843. return yield* Effect.fail(new Error(`${scenario.name} must use TestLLMServer via .withLlm()`))
  1844. }
  1845. }
  1846. printHeader(options, effectRoutes, selected, missing, extra, {
  1847. database: exerciseDatabasePath,
  1848. global: exerciseGlobalRoot,
  1849. })
  1850. const results =
  1851. options.mode === "coverage"
  1852. ? selected.map(coverageResult)
  1853. : yield* Effect.forEach(
  1854. selected,
  1855. (scenario) =>
  1856. Effect.gen(function* () {
  1857. if (options.progress) console.log(`${color.dim}RUN ${routeKey(scenario)} ${scenario.name}${color.reset}`)
  1858. return yield* runScenario(options)(scenario)
  1859. }),
  1860. { concurrency: 1 },
  1861. )
  1862. printResults(results, missing, extra)
  1863. if (results.some((result) => result.status === "fail"))
  1864. return yield* Effect.fail(new Error("one or more scenarios failed"))
  1865. if (options.failOnSkip && results.some((result) => result.status === "skip"))
  1866. return yield* Effect.fail(new Error("one or more scenarios are skipped"))
  1867. if (options.failOnMissing && missing.length > 0)
  1868. return yield* Effect.fail(new Error("one or more routes have no scenario"))
  1869. return undefined
  1870. })
  1871. Effect.runPromise(main.pipe(Effect.provide(TestLLMServer.layer), Effect.scoped)).then(
  1872. () => process.exit(0),
  1873. (error: unknown) => {
  1874. console.error(`${color.red}${message(error)}${color.reset}`)
  1875. process.exit(1)
  1876. },
  1877. )