index.ts 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  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 `KIRINCODE_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, "kirincode.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, "kirincode.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: "kirincode", 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: "kirincode", 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}/history", "v2.session.history")
  1068. .seeded((ctx) => ctx.session({ title: "Session history" }))
  1069. .at((ctx) => ({
  1070. path: `${route("/api/session/{sessionID}/history", { sessionID: ctx.state.id })}?${new URLSearchParams({
  1071. after: "0",
  1072. limit: "2",
  1073. })}`,
  1074. headers: ctx.headers(),
  1075. }))
  1076. .json(
  1077. 200,
  1078. (body) => {
  1079. object(body)
  1080. array(body.data)
  1081. check(typeof body.hasMore === "boolean", "Expected a history exhaustion signal")
  1082. },
  1083. "none",
  1084. ),
  1085. http.protected
  1086. .get("/api/session/{sessionID}/history", "v2.session.history.missing")
  1087. .at((ctx) => ({
  1088. path: route("/api/session/{sessionID}/history", { sessionID: "ses_httpapi_missing" }),
  1089. headers: ctx.headers(),
  1090. }))
  1091. .json(404, object, "status"),
  1092. http.protected
  1093. .get("/api/session/{sessionID}/history", "v2.session.history.invalid")
  1094. .seeded((ctx) => ctx.session({ title: "Invalid history sequence" }))
  1095. .at((ctx) => ({
  1096. path: `${route("/api/session/{sessionID}/history", { sessionID: ctx.state.id })}?after=-1`,
  1097. headers: ctx.headers(),
  1098. }))
  1099. .json(400, object, "status"),
  1100. http.protected
  1101. .get("/api/session/{sessionID}/event", "v2.session.events.missing")
  1102. .at((ctx) => ({
  1103. path: `${route("/api/session/{sessionID}/event", { sessionID: "ses_httpapi_missing" })}?after=0`,
  1104. headers: ctx.headers(),
  1105. }))
  1106. .status(404, undefined, "status"),
  1107. http.protected
  1108. .post("/api/session/{sessionID}/interrupt", "v2.session.interrupt")
  1109. .seeded((ctx) => ctx.session({ title: "Interrupt session" }))
  1110. .at((ctx) => ({
  1111. path: route("/api/session/{sessionID}/interrupt", { sessionID: ctx.state.id }),
  1112. headers: ctx.headers(),
  1113. }))
  1114. .status(204, undefined, "none"),
  1115. http.protected
  1116. .get("/api/session/{sessionID}/message/{messageID}", "v2.session.message.missing")
  1117. .at((ctx) => ({
  1118. path: route("/api/session/{sessionID}/message/{messageID}", {
  1119. sessionID: "ses_httpapi_missing",
  1120. messageID: "msg_httpapi_missing",
  1121. }),
  1122. headers: ctx.headers(),
  1123. }))
  1124. .json(404, object, "status"),
  1125. http.protected
  1126. .post("/api/session/{sessionID}/prompt", "v2.session.prompt.invalid")
  1127. .seeded((ctx) => ctx.session({ title: "Invalid prompt owner" }))
  1128. .at((ctx) => ({
  1129. path: route("/api/session/{sessionID}/prompt", { sessionID: ctx.state.id }),
  1130. headers: ctx.headers(),
  1131. body: {},
  1132. }))
  1133. .status(400, undefined, "none"),
  1134. http.protected
  1135. .post("/api/session/{sessionID}/compact", "v2.session.compact")
  1136. .at((ctx) => ({
  1137. path: route("/api/session/{sessionID}/compact", { sessionID: "ses_httpapi_missing" }),
  1138. headers: ctx.headers(),
  1139. }))
  1140. .status(404, undefined, "status"),
  1141. http.protected
  1142. .post("/api/session/{sessionID}/wait", "v2.session.wait")
  1143. .at((ctx) => ({
  1144. path: route("/api/session/{sessionID}/wait", { sessionID: "ses_httpapi_missing" }),
  1145. headers: ctx.headers(),
  1146. }))
  1147. .status(404, undefined, "status"),
  1148. http.protected
  1149. .get("/session", "session.list")
  1150. .seeded((ctx) => ctx.session({ title: "List me" }))
  1151. .at((ctx) => ({ path: "/session?roots=true", headers: ctx.headers() }))
  1152. .json(200, (body, ctx) => {
  1153. array(body)
  1154. check(
  1155. body.some((item) => isRecord(item) && item.id === ctx.state.id && item.title === "List me"),
  1156. "seeded session should be listed",
  1157. )
  1158. }),
  1159. http.protected
  1160. .get("/session/status", "session.status")
  1161. .seeded((ctx) => ctx.session({ title: "Status session" }))
  1162. .json(200, object),
  1163. http.protected
  1164. .post("/session", "session.create")
  1165. .mutating()
  1166. .at((ctx) => ({ path: "/session", headers: ctx.headers(), body: { title: "Created session" } }))
  1167. .json(
  1168. 200,
  1169. (body, ctx) => {
  1170. object(body)
  1171. check(body.title === "Created session", "created session should use requested title")
  1172. check(body.directory === ctx.directory, "created session should use scenario directory")
  1173. },
  1174. "status",
  1175. ),
  1176. http.protected
  1177. .get("/session/{sessionID}", "session.get")
  1178. .seeded((ctx) => ctx.session({ title: "Get me" }))
  1179. .at((ctx) => ({ path: route("/session/{sessionID}", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1180. .json(200, (body, ctx) => {
  1181. object(body)
  1182. check(body.id === ctx.state.id, "should return requested session")
  1183. check(body.title === "Get me", "should preserve seeded title")
  1184. }),
  1185. http.protected
  1186. .get("/session/{sessionID}", "session.get.missing")
  1187. .at((ctx) => ({
  1188. path: route("/session/{sessionID}", { sessionID: "ses_httpapi_missing" }),
  1189. headers: ctx.headers(),
  1190. }))
  1191. .status(404),
  1192. http.protected
  1193. .patch("/session/{sessionID}", "session.update")
  1194. .mutating()
  1195. .seeded((ctx) => ctx.session({ title: "Before rename" }))
  1196. .at((ctx) => ({
  1197. path: route("/session/{sessionID}", { sessionID: ctx.state.id }),
  1198. headers: ctx.headers(),
  1199. body: { title: "After rename" },
  1200. }))
  1201. .json(
  1202. 200,
  1203. (body) => {
  1204. object(body)
  1205. check(body.title === "After rename", "updated session should use new title")
  1206. },
  1207. "status",
  1208. ),
  1209. http.protected
  1210. .patch("/session/{sessionID}", "session.update.invalid")
  1211. .mutating()
  1212. .at((ctx) => ({
  1213. path: route("/session/{sessionID}", { sessionID: "ses_httpapi_missing" }),
  1214. headers: ctx.headers(),
  1215. body: { title: 1 },
  1216. }))
  1217. .status(400),
  1218. http.protected
  1219. .delete("/session/{sessionID}", "session.delete")
  1220. .mutating()
  1221. .seeded((ctx) => ctx.session({ title: "Delete me" }))
  1222. .at((ctx) => ({ path: route("/session/{sessionID}", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1223. .jsonEffect(200, (body, ctx) =>
  1224. Effect.gen(function* () {
  1225. check(body === true, "delete should return true")
  1226. check((yield* ctx.sessionGet(ctx.state.id)) === undefined, "deleted session should not remain in storage")
  1227. }),
  1228. ),
  1229. http.protected
  1230. .get("/session/{sessionID}/children", "session.children")
  1231. .seeded((ctx) =>
  1232. Effect.gen(function* () {
  1233. const parent = yield* ctx.session({ title: "Parent" })
  1234. const child = yield* ctx.session({ title: "Child", parentID: parent.id })
  1235. return { parent, child }
  1236. }),
  1237. )
  1238. .at((ctx) => ({
  1239. path: route("/session/{sessionID}/children", { sessionID: ctx.state.parent.id }),
  1240. headers: ctx.headers(),
  1241. }))
  1242. .json(200, (body, ctx) => {
  1243. array(body)
  1244. check(
  1245. body.some((item) => isRecord(item) && item.id === ctx.state.child.id && item.parentID === ctx.state.parent.id),
  1246. "children should include seeded child",
  1247. )
  1248. }),
  1249. http.protected
  1250. .get("/session/{sessionID}/todo", "session.todo")
  1251. .seeded((ctx) =>
  1252. Effect.gen(function* () {
  1253. const session = yield* ctx.session({ title: "Todo session" })
  1254. const todos = [{ content: "cover session todo", status: "pending" as const, priority: "high" as const }]
  1255. yield* ctx.todos(session.id, todos)
  1256. return { session, todos }
  1257. }),
  1258. )
  1259. .at((ctx) => ({
  1260. path: route("/session/{sessionID}/todo", { sessionID: ctx.state.session.id }),
  1261. headers: ctx.headers(),
  1262. }))
  1263. .json(200, (body, ctx) => {
  1264. check(stable(body) === stable(ctx.state.todos), "todos should match seeded state")
  1265. }),
  1266. http.protected
  1267. .get("/session/{sessionID}/diff", "session.diff")
  1268. .seeded((ctx) => ctx.session({ title: "Diff session" }))
  1269. .at((ctx) => ({ path: route("/session/{sessionID}/diff", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1270. .json(200, array),
  1271. http.protected
  1272. .get("/session/{sessionID}/message", "session.messages")
  1273. .seeded((ctx) => ctx.session({ title: "Messages session" }))
  1274. .at((ctx) => ({ path: route("/session/{sessionID}/message", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1275. .json(200, (body) => {
  1276. array(body)
  1277. check(body.length === 0, "new session should have no messages")
  1278. }),
  1279. http.protected
  1280. .get("/session/{sessionID}/message/{messageID}", "session.message")
  1281. .seeded((ctx) =>
  1282. Effect.gen(function* () {
  1283. const session = yield* ctx.session({ title: "Message get session" })
  1284. const message = yield* ctx.message(session.id, { text: "read me" })
  1285. return { session, message }
  1286. }),
  1287. )
  1288. .at((ctx) => ({
  1289. path: route("/session/{sessionID}/message/{messageID}", {
  1290. sessionID: ctx.state.session.id,
  1291. messageID: ctx.state.message.info.id,
  1292. }),
  1293. headers: ctx.headers(),
  1294. }))
  1295. .json(200, (body, ctx) => {
  1296. object(body)
  1297. check(isRecord(body.info) && body.info.id === ctx.state.message.info.id, "should return requested message")
  1298. check(
  1299. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.id === ctx.state.message.part.id),
  1300. "message should include seeded part",
  1301. )
  1302. }),
  1303. http.protected
  1304. .patch("/session/{sessionID}/message/{messageID}/part/{partID}", "part.update")
  1305. .mutating()
  1306. .seeded((ctx) =>
  1307. Effect.gen(function* () {
  1308. const session = yield* ctx.session({ title: "Part update session" })
  1309. const message = yield* ctx.message(session.id, { text: "before" })
  1310. return { session, message }
  1311. }),
  1312. )
  1313. .at((ctx) => ({
  1314. path: route("/session/{sessionID}/message/{messageID}/part/{partID}", {
  1315. sessionID: ctx.state.session.id,
  1316. messageID: ctx.state.message.info.id,
  1317. partID: ctx.state.message.part.id,
  1318. }),
  1319. headers: ctx.headers(),
  1320. body: { ...ctx.state.message.part, text: "after" },
  1321. }))
  1322. .json(
  1323. 200,
  1324. (body) => {
  1325. object(body)
  1326. check(body.type === "text" && body.text === "after", "updated part should be returned")
  1327. },
  1328. "status",
  1329. ),
  1330. http.protected
  1331. .delete("/session/{sessionID}/message/{messageID}/part/{partID}", "part.delete")
  1332. .mutating()
  1333. .seeded((ctx) =>
  1334. Effect.gen(function* () {
  1335. const session = yield* ctx.session({ title: "Part delete session" })
  1336. const message = yield* ctx.message(session.id, { text: "delete part" })
  1337. return { session, message }
  1338. }),
  1339. )
  1340. .at((ctx) => ({
  1341. path: route("/session/{sessionID}/message/{messageID}/part/{partID}", {
  1342. sessionID: ctx.state.session.id,
  1343. messageID: ctx.state.message.info.id,
  1344. partID: ctx.state.message.part.id,
  1345. }),
  1346. headers: ctx.headers(),
  1347. }))
  1348. .jsonEffect(200, (body, ctx) =>
  1349. Effect.gen(function* () {
  1350. check(body === true, "delete part should return true")
  1351. const messages = yield* ctx.messages(ctx.state.session.id)
  1352. check(messages[0]?.parts.length === 0, "deleted part should not remain on message")
  1353. }),
  1354. ),
  1355. http.protected
  1356. .delete("/session/{sessionID}/message/{messageID}", "session.deleteMessage")
  1357. .mutating()
  1358. .seeded((ctx) =>
  1359. Effect.gen(function* () {
  1360. const session = yield* ctx.session({ title: "Message delete session" })
  1361. const message = yield* ctx.message(session.id, { text: "delete message" })
  1362. return { session, message }
  1363. }),
  1364. )
  1365. .at((ctx) => ({
  1366. path: route("/session/{sessionID}/message/{messageID}", {
  1367. sessionID: ctx.state.session.id,
  1368. messageID: ctx.state.message.info.id,
  1369. }),
  1370. headers: ctx.headers(),
  1371. }))
  1372. .jsonEffect(200, (body, ctx) =>
  1373. Effect.gen(function* () {
  1374. check(body === true, "delete message should return true")
  1375. check((yield* ctx.messages(ctx.state.session.id)).length === 0, "deleted message should not remain")
  1376. }),
  1377. ),
  1378. http.protected
  1379. .post("/session/{sessionID}/fork", "session.fork")
  1380. .mutating()
  1381. .seeded((ctx) => ctx.session({ title: "Fork source" }))
  1382. .at((ctx) => ({
  1383. path: route("/session/{sessionID}/fork", { sessionID: ctx.state.id }),
  1384. headers: ctx.headers(),
  1385. body: {},
  1386. }))
  1387. .json(
  1388. 200,
  1389. (body) => {
  1390. object(body)
  1391. check(typeof body.id === "string", "fork should return a session")
  1392. },
  1393. "status",
  1394. ),
  1395. http.protected
  1396. .post("/session/{sessionID}/abort", "session.abort")
  1397. .mutating()
  1398. .seeded((ctx) => ctx.session({ title: "Abort session" }))
  1399. .at((ctx) => ({ path: route("/session/{sessionID}/abort", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1400. .json(200, (body) => {
  1401. check(body === true, "abort should return true")
  1402. }),
  1403. http.protected
  1404. .post("/session/{sessionID}/abort", "session.abort.missing")
  1405. .at((ctx) => ({
  1406. path: route("/session/{sessionID}/abort", { sessionID: "ses_httpapi_missing" }),
  1407. headers: ctx.headers(),
  1408. }))
  1409. .json(200, (body) => {
  1410. check(body === true, "missing session abort should remain a no-op success")
  1411. }),
  1412. http.protected
  1413. .post("/session/{sessionID}/init", "session.init")
  1414. .preserveDatabase()
  1415. .withLlm()
  1416. .seeded((ctx) =>
  1417. Effect.gen(function* () {
  1418. const session = yield* ctx.session({ title: "Init session" })
  1419. const message = yield* ctx.message(session.id, { text: "initialize" })
  1420. yield* ctx.llmText("initialized")
  1421. yield* ctx.llmText("initialized")
  1422. return { session, message }
  1423. }),
  1424. )
  1425. .at((ctx) => ({
  1426. path: route("/session/{sessionID}/init", { sessionID: ctx.state.session.id }),
  1427. headers: ctx.headers(),
  1428. body: { providerID: "test", modelID: "test-model", messageID: ctx.state.message.info.id },
  1429. }))
  1430. .jsonEffect(200, (body, ctx) =>
  1431. Effect.gen(function* () {
  1432. check(body === true, "init should return true")
  1433. yield* ctx.llmWait(1)
  1434. }),
  1435. ),
  1436. http.protected
  1437. .post("/session/{sessionID}/message", "session.prompt")
  1438. .preserveDatabase()
  1439. .withLlm()
  1440. .seeded((ctx) =>
  1441. Effect.gen(function* () {
  1442. const session = yield* ctx.session({ title: "LLM prompt session" })
  1443. yield* ctx.llmText("fake assistant")
  1444. yield* ctx.llmText("fake assistant")
  1445. return session
  1446. }),
  1447. )
  1448. .at((ctx) => ({
  1449. path: route("/session/{sessionID}/message", { sessionID: ctx.state.id }),
  1450. headers: ctx.headers(),
  1451. body: {
  1452. agent: "build",
  1453. model: { providerID: "test", modelID: "test-model" },
  1454. parts: [{ type: "text", text: "hello llm" }],
  1455. },
  1456. }))
  1457. .jsonEffect(
  1458. 200,
  1459. (body, ctx) =>
  1460. Effect.gen(function* () {
  1461. object(body)
  1462. check(isRecord(body.info) && body.info.role === "assistant", "prompt should return assistant message")
  1463. check(
  1464. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.text === "fake assistant"),
  1465. "assistant message should use fake LLM text",
  1466. )
  1467. yield* ctx.llmWait(1)
  1468. }),
  1469. "status",
  1470. ),
  1471. http.protected
  1472. .post("/session/{sessionID}/prompt_async", "session.prompt_async")
  1473. .preserveDatabase()
  1474. .withLlm()
  1475. .seeded((ctx) =>
  1476. Effect.gen(function* () {
  1477. const session = yield* ctx.session({ title: "Async prompt session" })
  1478. yield* ctx.llmText("fake async assistant")
  1479. yield* ctx.llmText("fake async assistant")
  1480. return session
  1481. }),
  1482. )
  1483. .at((ctx) => ({
  1484. path: route("/session/{sessionID}/prompt_async", { sessionID: ctx.state.id }),
  1485. headers: ctx.headers(),
  1486. body: {
  1487. agent: "build",
  1488. model: { providerID: "test", modelID: "test-model" },
  1489. parts: [{ type: "text", text: "hello async" }],
  1490. },
  1491. }))
  1492. .status(204, (ctx) =>
  1493. Effect.gen(function* () {
  1494. yield* ctx.llmWait(1)
  1495. }),
  1496. ),
  1497. http.protected
  1498. .post("/session/{sessionID}/command", "session.command")
  1499. .preserveDatabase()
  1500. .withLlm()
  1501. .seeded((ctx) =>
  1502. Effect.gen(function* () {
  1503. const session = yield* ctx.session({ title: "Command session" })
  1504. yield* ctx.llmText("command done")
  1505. yield* ctx.llmText("command done")
  1506. return session
  1507. }),
  1508. )
  1509. .at((ctx) => ({
  1510. path: route("/session/{sessionID}/command", { sessionID: ctx.state.id }),
  1511. headers: ctx.headers(),
  1512. body: { command: "init", arguments: "", model: "test/test-model" },
  1513. }))
  1514. .jsonEffect(
  1515. 200,
  1516. (body, ctx) =>
  1517. Effect.gen(function* () {
  1518. object(body)
  1519. check(isRecord(body.info) && body.info.role === "assistant", "command should return assistant message")
  1520. yield* ctx.llmWait(1)
  1521. }),
  1522. "status",
  1523. ),
  1524. http.protected
  1525. .post("/session/{sessionID}/shell", "session.shell")
  1526. .preserveDatabase()
  1527. .mutating()
  1528. .seeded((ctx) => ctx.session({ title: "Shell session" }))
  1529. .at((ctx) => ({
  1530. path: route("/session/{sessionID}/shell", { sessionID: ctx.state.id }),
  1531. headers: ctx.headers(),
  1532. body: { agent: "build", model: { providerID: "test", modelID: "test-model" }, command: "printf shell-ok" },
  1533. }))
  1534. .json(
  1535. 200,
  1536. (body) => {
  1537. object(body)
  1538. check(isRecord(body.info) && body.info.role === "assistant", "shell should return assistant message")
  1539. check(
  1540. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.type === "tool"),
  1541. "shell should return a tool part",
  1542. )
  1543. },
  1544. "status",
  1545. ),
  1546. http.protected
  1547. .post("/session/{sessionID}/summarize", "session.summarize")
  1548. .preserveDatabase()
  1549. .withLlm()
  1550. .seeded((ctx) =>
  1551. Effect.gen(function* () {
  1552. const session = yield* ctx.session({ title: "Summarize session" })
  1553. yield* ctx.message(session.id, { text: "summarize this work" })
  1554. const summary = [
  1555. "## Objective",
  1556. "- Exercise session summarize.",
  1557. "",
  1558. "## Important Details",
  1559. "- Use fake LLM.",
  1560. "- Keep route local.",
  1561. "- Test fixture: test/server/httpapi-exercise/index.ts.",
  1562. "",
  1563. "## Work State",
  1564. "- Completed: Summary generated.",
  1565. "- Active: (none)",
  1566. "- Blocked: (none)",
  1567. "",
  1568. "## Next Move",
  1569. "1. (none)",
  1570. ].join("\n")
  1571. yield* ctx.llmText(summary)
  1572. yield* ctx.llmText(summary)
  1573. return session
  1574. }),
  1575. )
  1576. .at((ctx) => ({
  1577. path: route("/session/{sessionID}/summarize", { sessionID: ctx.state.id }),
  1578. headers: ctx.headers(),
  1579. body: { providerID: "test", modelID: "test-model", auto: false },
  1580. }))
  1581. .jsonEffect(
  1582. 200,
  1583. (body, ctx) =>
  1584. Effect.gen(function* () {
  1585. check(body === true, "summarize should return true")
  1586. const messages = yield* ctx.messages(ctx.state.id)
  1587. check(
  1588. messages.some((message) => message.info.role === "assistant" && message.info.summary === true),
  1589. "summarize should create a summary assistant message",
  1590. )
  1591. yield* ctx.llmWait(1)
  1592. }),
  1593. "status",
  1594. ),
  1595. http.protected
  1596. .post("/session/{sessionID}/revert", "session.revert")
  1597. .mutating()
  1598. .seeded((ctx) =>
  1599. Effect.gen(function* () {
  1600. const session = yield* ctx.session({ title: "Revert session" })
  1601. const message = yield* ctx.message(session.id, { text: "revert me" })
  1602. return { session, message }
  1603. }),
  1604. )
  1605. .at((ctx) => ({
  1606. path: route("/session/{sessionID}/revert", { sessionID: ctx.state.session.id }),
  1607. headers: ctx.headers(),
  1608. body: { messageID: ctx.state.message.info.id },
  1609. }))
  1610. .json(
  1611. 200,
  1612. (body, ctx) => {
  1613. object(body)
  1614. check(body.id === ctx.state.session.id, "revert should return the session")
  1615. check(
  1616. isRecord(body.revert) && body.revert.messageID === ctx.state.message.info.id,
  1617. "revert should record reverted message",
  1618. )
  1619. },
  1620. "status",
  1621. ),
  1622. http.protected
  1623. .post("/session/{sessionID}/unrevert", "session.unrevert")
  1624. .mutating()
  1625. .seeded((ctx) => ctx.session({ title: "Unrevert session" }))
  1626. .at((ctx) => ({
  1627. path: route("/session/{sessionID}/unrevert", { sessionID: ctx.state.id }),
  1628. headers: ctx.headers(),
  1629. }))
  1630. .json(
  1631. 200,
  1632. (body, ctx) => {
  1633. object(body)
  1634. check(body.id === ctx.state.id, "unrevert should return the session")
  1635. },
  1636. "status",
  1637. ),
  1638. http.protected
  1639. .post("/session/{sessionID}/permissions/{permissionID}", "permission.respond")
  1640. .seeded((ctx) => ctx.session({ title: "Deprecated permission session" }))
  1641. .at((ctx) => ({
  1642. path: route("/session/{sessionID}/permissions/{permissionID}", {
  1643. sessionID: ctx.state.id,
  1644. permissionID: "per_httpapi_deprecated",
  1645. }),
  1646. headers: ctx.headers(),
  1647. body: { response: "once" },
  1648. }))
  1649. .json(404, object, "status"),
  1650. http.protected
  1651. .post("/session/{sessionID}/share", "session.share")
  1652. .mutating()
  1653. .seeded((ctx) => ctx.session({ title: "Share session" }))
  1654. .at((ctx) => ({ path: route("/session/{sessionID}/share", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1655. .json(
  1656. 200,
  1657. (body, ctx) => {
  1658. object(body)
  1659. check(body.id === ctx.state.id, "share should return the session")
  1660. },
  1661. "status",
  1662. ),
  1663. http.protected
  1664. .delete("/session/{sessionID}/share", "session.unshare")
  1665. .mutating()
  1666. .seeded((ctx) => ctx.session({ title: "Unshare session" }))
  1667. .at((ctx) => ({ path: route("/session/{sessionID}/share", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1668. .json(
  1669. 200,
  1670. (body, ctx) => {
  1671. object(body)
  1672. check(body.id === ctx.state.id, "unshare should return the session")
  1673. },
  1674. "status",
  1675. ),
  1676. http.protected
  1677. .post("/tui/append-prompt", "tui.appendPrompt")
  1678. .at((ctx) => ({ path: "/tui/append-prompt", headers: ctx.headers(), body: { text: "hello" } }))
  1679. .json(200, boolean, "status"),
  1680. http.protected
  1681. .post("/tui/select-session", "tui.selectSession.invalid")
  1682. .at((ctx) => ({ path: "/tui/select-session", headers: ctx.headers(), body: { sessionID: "invalid" } }))
  1683. .status(400),
  1684. http.protected.post("/tui/open-help", "tui.openHelp").json(200, boolean, "status"),
  1685. http.protected.post("/tui/open-sessions", "tui.openSessions").json(200, boolean, "status"),
  1686. http.protected.post("/tui/open-themes", "tui.openThemes").json(200, boolean, "status"),
  1687. http.protected.post("/tui/open-models", "tui.openModels").json(200, boolean, "status"),
  1688. http.protected.post("/tui/submit-prompt", "tui.submitPrompt").json(200, boolean, "status"),
  1689. http.protected.post("/tui/clear-prompt", "tui.clearPrompt").json(200, boolean, "status"),
  1690. http.protected
  1691. .post("/tui/execute-command", "tui.executeCommand")
  1692. .at((ctx) => ({ path: "/tui/execute-command", headers: ctx.headers(), body: { command: "agent_cycle" } }))
  1693. .json(200, boolean, "status"),
  1694. http.protected
  1695. .post("/tui/show-toast", "tui.showToast")
  1696. .at((ctx) => ({
  1697. path: "/tui/show-toast",
  1698. headers: ctx.headers(),
  1699. body: { title: "Exercise", message: "covered", variant: "info", duration: 1000 },
  1700. }))
  1701. .json(200, boolean, "status"),
  1702. http.protected
  1703. .post("/tui/publish", "tui.publish")
  1704. .at((ctx) => ({
  1705. path: "/tui/publish",
  1706. headers: ctx.headers(),
  1707. body: { type: "tui.prompt.append", properties: { text: "published" } },
  1708. }))
  1709. .json(200, boolean, "status"),
  1710. http.protected
  1711. .post("/tui/select-session", "tui.selectSession")
  1712. .seeded((ctx) => ctx.session({ title: "TUI select" }))
  1713. .at((ctx) => ({ path: "/tui/select-session", headers: ctx.headers(), body: { sessionID: ctx.state.id } }))
  1714. .json(200, boolean, "status"),
  1715. http.protected
  1716. .post("/tui/control/response", "tui.control.response")
  1717. .at((ctx) => ({ path: "/tui/control/response", headers: ctx.headers(), body: { ok: true } }))
  1718. .json(200, boolean, "status"),
  1719. http.protected
  1720. .get("/tui/control/next", "tui.control.next")
  1721. .mutating()
  1722. .seeded((ctx) => ctx.tuiRequest({ path: "/tui/exercise", body: { text: "queued" } }))
  1723. .json(
  1724. 200,
  1725. (body) => {
  1726. object(body)
  1727. check(body.path === "/tui/exercise", "control next should return queued path")
  1728. object(body.body)
  1729. check(body.body.text === "queued", "control next should return queued body")
  1730. },
  1731. "status",
  1732. ),
  1733. http.protected
  1734. .post("/global/upgrade", "global.upgrade")
  1735. .global()
  1736. .probe({ path: "/global/upgrade", body: { target: 1 } })
  1737. .at(() => ({ path: "/global/upgrade", body: { target: 1 } }))
  1738. .status(400),
  1739. ]
  1740. const llmScenarios = new Set([
  1741. "session.init",
  1742. "session.prompt",
  1743. "session.prompt_async",
  1744. "session.command",
  1745. "session.summarize",
  1746. ])
  1747. const main = Effect.gen(function* () {
  1748. yield* Effect.addFinalizer(() => Effect.promise(() => disposeApps()).pipe(Effect.andThen(cleanupExercisePaths)))
  1749. const options = parseOptions(Bun.argv.slice(2))
  1750. const modules = yield* Effect.promise(() => runtime())
  1751. const effectRoutes = routeKeys(OpenApi.fromApi(modules.PublicApi))
  1752. const selected = selectedScenarios(options, scenarios)
  1753. const missing = effectRoutes.filter((route) => !scenarios.some((scenario) => route === routeKey(scenario)))
  1754. const extra = scenarios.filter((scenario) => !effectRoutes.includes(routeKey(scenario)))
  1755. for (const scenario of scenarios) {
  1756. if (scenario.kind === "active" && llmScenarios.has(scenario.name) && !scenario.project?.llm) {
  1757. return yield* Effect.fail(new Error(`${scenario.name} must use TestLLMServer via .withLlm()`))
  1758. }
  1759. }
  1760. printHeader(options, effectRoutes, selected, missing, extra, {
  1761. database: exerciseDatabasePath,
  1762. global: exerciseGlobalRoot,
  1763. })
  1764. const results =
  1765. options.mode === "coverage"
  1766. ? selected.map(coverageResult)
  1767. : yield* Effect.forEach(
  1768. selected,
  1769. (scenario) =>
  1770. Effect.gen(function* () {
  1771. if (options.progress) console.log(`${color.dim}RUN ${routeKey(scenario)} ${scenario.name}${color.reset}`)
  1772. return yield* runScenario(options)(scenario)
  1773. }),
  1774. { concurrency: 1 },
  1775. )
  1776. printResults(results, missing, extra)
  1777. if (results.some((result) => result.status === "fail"))
  1778. return yield* Effect.fail(new Error("one or more scenarios failed"))
  1779. if (options.failOnSkip && results.some((result) => result.status === "skip"))
  1780. return yield* Effect.fail(new Error("one or more scenarios are skipped"))
  1781. if (options.failOnMissing && missing.length > 0)
  1782. return yield* Effect.fail(new Error("one or more routes have no scenario"))
  1783. return undefined
  1784. })
  1785. Effect.runPromise(main.pipe(Effect.provide(TestLLMServer.layer), Effect.scoped)).then(
  1786. () => process.exit(0),
  1787. (error: unknown) => {
  1788. console.error(`${color.red}${message(error)}${color.reset}`)
  1789. process.exit(1)
  1790. },
  1791. )