index.ts 70 KB

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