index.ts 66 KB

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