index.ts 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  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 { runtime } from "./runtime"
  37. import { type Scenario } from "./types"
  38. void (await import("@opencode-ai/core/util/log")).init({ print: false })
  39. function cursor(input: Record<string, unknown>) {
  40. return Buffer.from(JSON.stringify(input)).toString("base64url")
  41. }
  42. const scenarios: Scenario[] = [
  43. http.protected
  44. .get("/global/health", "global.health")
  45. .global()
  46. .json(200, (body) => {
  47. object(body)
  48. check(body.healthy === true, "server should report healthy")
  49. }),
  50. http.protected
  51. .get("/global/event", "global.event")
  52. .global()
  53. .stream()
  54. .status(
  55. 200,
  56. (_ctx, result) =>
  57. Effect.sync(() => {
  58. check(result.contentType.includes("text/event-stream"), "global event should be an SSE stream")
  59. check(result.text.includes("server.connected"), "global event should emit initial connection event")
  60. }),
  61. "status",
  62. ),
  63. http.protected.get("/global/config", "global.config.get").global().json(),
  64. http.protected
  65. .patch("/global/config", "global.config.update")
  66. .global()
  67. .seeded(() =>
  68. Effect.promise(() =>
  69. Bun.write(
  70. path.join(exerciseConfigDirectory, "opencode.jsonc"),
  71. JSON.stringify({ username: "httpapi-global" }, null, 2),
  72. ),
  73. ),
  74. )
  75. .at(() => ({ path: "/global/config", body: { username: "httpapi-global" } }))
  76. .jsonEffect(
  77. 200,
  78. (body) =>
  79. Effect.gen(function* () {
  80. object(body)
  81. check(body.username === "httpapi-global", "global config update should return patched config")
  82. const text = yield* Effect.promise(() =>
  83. Bun.file(path.join(exerciseConfigDirectory, "opencode.jsonc")).text(),
  84. )
  85. check(text.includes('"username": "httpapi-global"'), "global config update should write isolated config file")
  86. }),
  87. "status",
  88. ),
  89. http.protected
  90. .post("/global/dispose", "global.dispose")
  91. .global()
  92. .mutating()
  93. .json(
  94. 200,
  95. (body) => {
  96. check(body === true, "global dispose should return true")
  97. },
  98. "status",
  99. ),
  100. http.protected.get("/path", "path.get").json(200, (body, ctx) => {
  101. object(body)
  102. check(body.directory === ctx.directory, "directory should resolve from x-opencode-directory")
  103. check(body.worktree === ctx.directory, "worktree should resolve from x-opencode-directory")
  104. }),
  105. http.protected.get("/vcs", "vcs.get").json(),
  106. http.protected.get("/vcs/status", "vcs.status").json(200, array),
  107. http.protected
  108. .get("/vcs/diff", "vcs.diff")
  109. .at((ctx) => ({ path: "/vcs/diff?mode=git", headers: ctx.headers() }))
  110. .json(200, array),
  111. http.protected.get("/vcs/diff/raw", "vcs.diff.raw").status(
  112. 200,
  113. (_ctx, result) =>
  114. Effect.sync(() => {
  115. check(typeof result.text === "string", "raw VCS diff should return text")
  116. }),
  117. "status",
  118. ),
  119. http.protected
  120. .post("/vcs/apply", "vcs.apply")
  121. .inProject({ git: false })
  122. .at((ctx) => ({ path: "/vcs/apply", headers: ctx.headers(), body: { patch: "" } }))
  123. .status(400, undefined, "status"),
  124. http.protected.get("/command", "command.list").json(200, array, "status"),
  125. http.protected.get("/agent", "app.agents").json(200, array, "status"),
  126. http.protected.get("/skill", "app.skills").json(200, array, "status"),
  127. http.protected.get("/lsp", "lsp.status").json(200, array),
  128. http.protected.get("/formatter", "formatter.status").json(200, array),
  129. http.protected.get("/config", "config.get").json(200, undefined, "status"),
  130. http.protected
  131. .patch("/config", "config.update")
  132. .mutating()
  133. .at((ctx) => ({ path: "/config", headers: ctx.headers(), body: { username: "httpapi-local" } }))
  134. .json(
  135. 200,
  136. (body) => {
  137. object(body)
  138. check(body.username === "httpapi-local", "local config update should return patched config")
  139. },
  140. "status",
  141. ),
  142. http.protected
  143. .patch("/config", "config.update.invalid")
  144. .at((ctx) => ({ path: "/config", headers: ctx.headers(), body: { username: 1 } }))
  145. .status(400),
  146. http.protected.get("/config/providers", "config.providers").json(),
  147. http.protected.get("/project", "project.list").json(200, array, "status"),
  148. http.protected.get("/project/current", "project.current").json(
  149. 200,
  150. (body, ctx) => {
  151. object(body)
  152. check(body.worktree === ctx.directory, "current project should resolve from scenario directory")
  153. },
  154. "status",
  155. ),
  156. http.protected
  157. .patch("/project/{projectID}", "project.update")
  158. .mutating()
  159. .seeded((ctx) => ctx.project())
  160. .at((ctx) => ({
  161. path: route("/project/{projectID}", { projectID: ctx.state.id }),
  162. headers: ctx.headers(),
  163. body: { name: "HTTP API Project", commands: { start: "bun --version" } },
  164. }))
  165. .json(
  166. 200,
  167. (body) => {
  168. object(body)
  169. check(body.name === "HTTP API Project", "project update should return patched name")
  170. check(
  171. isRecord(body.commands) && body.commands.start === "bun --version",
  172. "project update should return patched command",
  173. )
  174. },
  175. "status",
  176. ),
  177. http.protected
  178. .post("/project/git/init", "project.initGit")
  179. .mutating()
  180. .inProject({ git: false })
  181. .json(
  182. 200,
  183. (body, ctx) => {
  184. object(body)
  185. check(body.worktree === ctx.directory, "git init should return current project")
  186. check(body.vcs === "git", "git init should mark the project as git-backed")
  187. },
  188. "status",
  189. ),
  190. http.protected.get("/provider", "provider.list").json(),
  191. http.protected.get("/provider/auth", "provider.auth").json(),
  192. http.protected
  193. .post("/provider/{providerID}/oauth/authorize", "provider.oauth.authorize")
  194. .at((ctx) => ({
  195. path: route("/provider/{providerID}/oauth/authorize", { providerID: "httpapi" }),
  196. headers: ctx.headers(),
  197. body: { method: "bad" },
  198. }))
  199. .status(400),
  200. http.protected
  201. .post("/provider/{providerID}/oauth/callback", "provider.oauth.callback")
  202. .at((ctx) => ({
  203. path: route("/provider/{providerID}/oauth/callback", { providerID: "httpapi" }),
  204. headers: ctx.headers(),
  205. body: { method: "bad" },
  206. }))
  207. .status(400),
  208. http.protected.get("/permission", "permission.list").json(200, array),
  209. http.protected
  210. .post("/permission/{requestID}/reply", "permission.reply.invalid")
  211. .at((ctx) => ({
  212. path: route("/permission/{requestID}/reply", { requestID: "per_httpapi" }),
  213. headers: ctx.headers(),
  214. body: { reply: "bad" },
  215. }))
  216. .status(400),
  217. http.protected
  218. .post("/permission/{requestID}/reply", "permission.reply")
  219. .at((ctx) => ({
  220. path: route("/permission/{requestID}/reply", { requestID: "per_httpapi" }),
  221. headers: ctx.headers(),
  222. body: { reply: "once" },
  223. }))
  224. .json(404, object, "status"),
  225. http.protected.get("/question", "question.list").json(200, array),
  226. http.protected
  227. .post("/question/{requestID}/reply", "question.reply.invalid")
  228. .at((ctx) => ({
  229. path: route("/question/{requestID}/reply", { requestID: "que_httpapi_reply" }),
  230. headers: ctx.headers(),
  231. body: { answers: "Yes" },
  232. }))
  233. .status(400),
  234. http.protected
  235. .post("/question/{requestID}/reply", "question.reply")
  236. .at((ctx) => ({
  237. path: route("/question/{requestID}/reply", { requestID: "que_httpapi_reply" }),
  238. headers: ctx.headers(),
  239. body: { answers: [["Yes"]] },
  240. }))
  241. .json(404, object, "status"),
  242. http.protected
  243. .post("/question/{requestID}/reject", "question.reject")
  244. .at((ctx) => ({
  245. path: route("/question/{requestID}/reject", { requestID: "que_httpapi_reject" }),
  246. headers: ctx.headers(),
  247. }))
  248. .json(404, object, "status"),
  249. http.protected
  250. .get("/file", "file.list")
  251. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  252. .at((ctx) => ({ path: `/file?${new URLSearchParams({ path: "." })}`, headers: ctx.headers() }))
  253. .json(200, array),
  254. http.protected
  255. .get("/file/content", "file.read")
  256. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  257. .at((ctx) => ({ path: `/file/content?${new URLSearchParams({ path: "hello.txt" })}`, headers: ctx.headers() }))
  258. .json(200, (body) => {
  259. object(body)
  260. check(body.content === "hello", `content should match seeded file: ${JSON.stringify(body)}`)
  261. }),
  262. http.protected
  263. .get("/file/content", "file.read.missing")
  264. .at((ctx) => ({ path: `/file/content?${new URLSearchParams({ path: "missing.txt" })}`, headers: ctx.headers() }))
  265. .json(200, (body) => {
  266. object(body)
  267. check(body.type === "text" && body.content === "", "missing file content should return an empty text result")
  268. }),
  269. http.protected.get("/file/status", "file.status").json(200, array),
  270. http.protected
  271. .get("/find", "find.text")
  272. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  273. .at((ctx) => ({ path: `/find?${new URLSearchParams({ pattern: "hello" })}`, headers: ctx.headers() }))
  274. .json(200, array),
  275. http.protected
  276. .get("/find/file", "find.files")
  277. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  278. .at((ctx) => ({
  279. path: `/find/file?${new URLSearchParams({ query: "hello", dirs: "false" })}`,
  280. headers: ctx.headers(),
  281. }))
  282. .json(200, array),
  283. http.protected
  284. .get("/find/symbol", "find.symbols")
  285. .seeded((ctx) => ctx.file("hello.ts", "export const hello = 1\n"))
  286. .at((ctx) => ({ path: `/find/symbol?${new URLSearchParams({ query: "hello" })}`, headers: ctx.headers() }))
  287. .json(200, array),
  288. http.protected
  289. .get("/event", "event.stream")
  290. .stream()
  291. .status(
  292. 200,
  293. (_ctx, result) =>
  294. Effect.sync(() => {
  295. check(result.contentType.includes("text/event-stream"), "event should be an SSE stream")
  296. check(result.text.includes("server.connected"), "event should emit initial connection event")
  297. }),
  298. "status",
  299. ),
  300. http.protected.get("/mcp", "mcp.status").json(),
  301. http.protected
  302. .post("/mcp", "mcp.add")
  303. .mutating()
  304. .at((ctx) => ({
  305. path: "/mcp",
  306. headers: ctx.headers(),
  307. body: { name: "httpapi-disabled", config: { type: "local", command: ["bun", "--version"], enabled: false } },
  308. }))
  309. .json(
  310. 200,
  311. (body) => {
  312. object(body)
  313. object(body["httpapi-disabled"])
  314. check(body["httpapi-disabled"].status === "disabled", "disabled MCP server should be added without spawning")
  315. },
  316. "status",
  317. ),
  318. http.protected
  319. .post("/mcp", "mcp.add.invalid")
  320. .at((ctx) => ({
  321. path: "/mcp",
  322. headers: ctx.headers(),
  323. body: { name: "httpapi-invalid", config: { type: "invalid" } },
  324. }))
  325. .status(400),
  326. http.protected
  327. .post("/mcp/{name}/auth", "mcp.auth.start")
  328. .at((ctx) => ({ path: route("/mcp/{name}/auth", { name: "httpapi-missing" }), headers: ctx.headers() }))
  329. .json(404, object, "status"),
  330. http.protected
  331. .delete("/mcp/{name}/auth", "mcp.auth.remove")
  332. .mutating()
  333. .at((ctx) => ({ path: route("/mcp/{name}/auth", { name: "httpapi-missing" }), headers: ctx.headers() }))
  334. .json(404, object, "status"),
  335. http.protected
  336. .post("/mcp/{name}/auth/authenticate", "mcp.auth.authenticate")
  337. .at((ctx) => ({
  338. path: route("/mcp/{name}/auth/authenticate", { name: "httpapi-missing" }),
  339. headers: ctx.headers(),
  340. }))
  341. .json(404, object, "status"),
  342. http.protected
  343. .post("/mcp/{name}/auth/callback", "mcp.auth.callback")
  344. .at((ctx) => ({
  345. path: route("/mcp/{name}/auth/callback", { name: "httpapi-missing" }),
  346. headers: ctx.headers(),
  347. body: { code: "code" },
  348. }))
  349. .json(404, object, "status"),
  350. http.protected
  351. .post("/mcp/{name}/connect", "mcp.connect")
  352. .mutating()
  353. .at((ctx) => ({ path: route("/mcp/{name}/connect", { name: "httpapi-missing" }), headers: ctx.headers() }))
  354. .json(404, object, "status"),
  355. http.protected
  356. .post("/mcp/{name}/disconnect", "mcp.disconnect")
  357. .mutating()
  358. .at((ctx) => ({ path: route("/mcp/{name}/disconnect", { name: "httpapi-missing" }), headers: ctx.headers() }))
  359. .json(404, object, "status"),
  360. http.protected.get("/pty/shells", "pty.shells").json(200, array),
  361. http.protected.get("/pty", "pty.list").json(200, array),
  362. http.protected
  363. .post("/pty", "pty.create")
  364. .mutating()
  365. .at((ctx) => ({ path: "/pty", headers: ctx.headers(), body: controlledPtyInput("HTTP API PTY") }))
  366. .json(
  367. 200,
  368. (body, ctx) => {
  369. object(body)
  370. check(body.title === "HTTP API PTY", "PTY create should return requested title")
  371. check(body.command === "/bin/sh", "PTY create should use controlled shell command")
  372. check(body.cwd === ctx.directory, "PTY create should default cwd to scenario directory")
  373. },
  374. "status",
  375. ),
  376. http.protected
  377. .post("/pty", "pty.create.invalid")
  378. .at((ctx) => ({ path: "/pty", headers: ctx.headers(), body: { command: 1 } }))
  379. .status(400),
  380. http.protected
  381. .post("/pty/{ptyID}/connect-token", "pty.connectToken.invalid")
  382. .at((ctx) => ({
  383. path: route("/pty/{ptyID}/connect-token", { ptyID: "pty_httpapi_missing" }),
  384. headers: ctx.headers(),
  385. }))
  386. .status(403, undefined, "status"),
  387. http.protected
  388. .get("/pty/{ptyID}", "pty.get")
  389. .at((ctx) => ({ path: route("/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  390. .status(404),
  391. http.protected
  392. .put("/pty/{ptyID}", "pty.update")
  393. .mutating()
  394. .at((ctx) => ({
  395. path: route("/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }),
  396. headers: ctx.headers(),
  397. body: { size: { rows: 0, cols: 0 } },
  398. }))
  399. .status(400),
  400. http.protected
  401. .delete("/pty/{ptyID}", "pty.remove")
  402. .mutating()
  403. .at((ctx) => ({ path: route("/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  404. .json(200, (body) => {
  405. check(body === true, "PTY remove should return true")
  406. }),
  407. http.protected
  408. .get("/pty/{ptyID}/connect", "pty.connect")
  409. .at((ctx) => ({ path: route("/pty/{ptyID}/connect", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  410. .status(404, undefined, "none"),
  411. http.protected.get("/experimental/console", "experimental.console.get").json(),
  412. http.protected.get("/experimental/console/orgs", "experimental.console.listOrgs").json(),
  413. http.protected
  414. .post("/experimental/console/switch", "experimental.console.switchOrg")
  415. .at((ctx) => ({
  416. path: "/experimental/console/switch",
  417. headers: ctx.headers(),
  418. body: { accountID: "httpapi-account", orgID: "httpapi-org" },
  419. }))
  420. .status(400, undefined, "none"),
  421. http.protected.get("/experimental/workspace/adapter", "experimental.workspace.adapter.list").json(200, array),
  422. http.protected.get("/experimental/workspace", "experimental.workspace.list").json(200, array),
  423. http.protected.get("/experimental/workspace/status", "experimental.workspace.status").json(200, array),
  424. http.protected
  425. .post("/experimental/workspace", "experimental.workspace.create")
  426. .at((ctx) => ({ path: "/experimental/workspace", headers: ctx.headers(), body: {} }))
  427. .status(400),
  428. http.protected
  429. .post("/experimental/workspace/sync-list", "experimental.workspace.syncList")
  430. .status(204, undefined, "status"),
  431. http.protected
  432. .delete("/experimental/workspace/{id}", "experimental.workspace.remove")
  433. .mutating()
  434. .at((ctx) => ({
  435. path: route("/experimental/workspace/{id}", { id: "wrk_httpapi_missing" }),
  436. headers: ctx.headers(),
  437. }))
  438. .status(200),
  439. http.protected
  440. .post("/experimental/workspace/warp", "experimental.workspace.warp")
  441. .at((ctx) => ({
  442. path: "/experimental/workspace/warp",
  443. headers: ctx.headers(),
  444. body: {},
  445. }))
  446. .status(400),
  447. http.protected
  448. .get("/experimental/tool", "tool.list")
  449. .at((ctx) => ({
  450. path: `/experimental/tool?${new URLSearchParams({ provider: "opencode", model: "test" })}`,
  451. headers: ctx.headers(),
  452. }))
  453. .json(200, array, "status"),
  454. http.protected.get("/experimental/tool/ids", "tool.ids").json(200, array),
  455. http.protected.get("/experimental/worktree", "worktree.list").json(200, array),
  456. http.protected
  457. .post("/experimental/worktree", "worktree.create")
  458. .mutating()
  459. .at((ctx) => ({ path: "/experimental/worktree", headers: ctx.headers(), body: { name: "api-dsl" } }))
  460. .jsonEffect(
  461. 200,
  462. (body, ctx) =>
  463. Effect.gen(function* () {
  464. object(body)
  465. check(typeof body.directory === "string", "created worktree should include directory")
  466. yield* ctx.worktreeRemove(body.directory)
  467. }),
  468. "status",
  469. ),
  470. http.protected
  471. .post("/experimental/worktree", "worktree.create.invalid")
  472. .at((ctx) => ({ path: "/experimental/worktree", headers: ctx.headers(), body: { name: 1 } }))
  473. .status(400),
  474. http.protected
  475. .delete("/experimental/worktree", "worktree.remove")
  476. .mutating()
  477. .seeded((ctx) => ctx.worktree({ name: "api-remove" }))
  478. .at((ctx) => ({ path: "/experimental/worktree", headers: ctx.headers(), body: { directory: ctx.state.directory } }))
  479. .json(200, (body) => {
  480. check(body === true, "worktree remove should return true")
  481. }),
  482. http.protected
  483. .post("/experimental/worktree/reset", "worktree.reset")
  484. .mutating()
  485. .seeded((ctx) => ctx.worktree({ name: "api-reset" }))
  486. .at((ctx) => ({
  487. path: "/experimental/worktree/reset",
  488. headers: ctx.headers(),
  489. body: { directory: ctx.state.directory },
  490. }))
  491. .jsonEffect(200, (body, ctx) =>
  492. Effect.gen(function* () {
  493. check(body === true, "worktree reset should return true")
  494. yield* ctx.worktreeRemove(ctx.state.directory)
  495. }),
  496. ),
  497. http.protected
  498. .get("/experimental/session", "experimental.session.list")
  499. .at((ctx) => ({ path: "/experimental/session?roots=false&archived=false", headers: ctx.headers() }))
  500. .json(200, array),
  501. http.protected.get("/experimental/resource", "experimental.resource.list").json(),
  502. http.protected
  503. .post("/sync/history", "sync.history.list")
  504. .at((ctx) => ({ path: "/sync/history", headers: ctx.headers(), body: {} }))
  505. .json(200, array),
  506. http.protected
  507. .post("/sync/replay", "sync.replay")
  508. .at((ctx) => ({ path: "/sync/replay", headers: ctx.headers(), body: { directory: ctx.directory, events: [] } }))
  509. .status(400),
  510. http.protected
  511. .post("/sync/steal", "sync.steal.invalid")
  512. .at((ctx) => ({ path: "/sync/steal", headers: ctx.headers(), body: {} }))
  513. .status(400, undefined, "status"),
  514. http.protected
  515. .post("/sync/start", "sync.start")
  516. .mutating()
  517. .preserveDatabase()
  518. .json(200, (body) => {
  519. check(body === true, "sync start should return true when no workspace sessions exist")
  520. }),
  521. http.protected
  522. .post("/instance/dispose", "instance.dispose")
  523. .mutating()
  524. .json(200, (body) => {
  525. check(body === true, "instance dispose should return true")
  526. }),
  527. http.protected
  528. .post("/log", "app.log")
  529. .global()
  530. .at(() => ({ path: "/log", body: { service: "httpapi-exercise", level: "info", message: "route coverage" } }))
  531. .json(200, (body) => {
  532. check(body === true, "log route should return true")
  533. }),
  534. http.protected
  535. .put("/auth/{providerID}", "auth.set")
  536. .global()
  537. .at(() => ({ path: route("/auth/{providerID}", { providerID: "test" }), body: { type: "api", key: "test-key" } }))
  538. .jsonEffect(200, (body) =>
  539. Effect.gen(function* () {
  540. check(body === true, "auth set should return true")
  541. const auth = yield* Effect.promise(() => Bun.file(path.join(exerciseDataDirectory, "auth.json")).json())
  542. object(auth)
  543. check(isRecord(auth.test) && auth.test.key === "test-key", "auth set should write isolated auth file")
  544. }),
  545. ),
  546. http.protected
  547. .delete("/auth/{providerID}", "auth.remove")
  548. .global()
  549. .seeded(() =>
  550. Effect.promise(() =>
  551. Bun.write(
  552. path.join(exerciseDataDirectory, "auth.json"),
  553. JSON.stringify({ test: { type: "api", key: "remove-me" } }),
  554. ),
  555. ),
  556. )
  557. .at(() => ({ path: route("/auth/{providerID}", { providerID: "test" }) }))
  558. .jsonEffect(200, (body) =>
  559. Effect.gen(function* () {
  560. check(body === true, "auth remove should return true")
  561. const auth = yield* Effect.promise(() => Bun.file(path.join(exerciseDataDirectory, "auth.json")).json())
  562. object(auth)
  563. check(auth.test === undefined, "auth remove should delete provider from isolated auth file")
  564. }),
  565. ),
  566. http.protected.get("/api/model", "v2.model.list").json(200, array),
  567. http.protected.get("/api/provider", "v2.provider.list").json(200, array),
  568. http.protected
  569. .get("/api/provider/{providerID}", "v2.provider.get")
  570. .at((ctx) => ({ path: route("/api/provider/{providerID}", { providerID: "missing" }), headers: ctx.headers() }))
  571. .json(404, object, "status"),
  572. http.protected
  573. .get("/api/session", "v2.session.list")
  574. .at((ctx) => ({ path: "/api/session?roots=true", headers: ctx.headers() }))
  575. .json(
  576. 200,
  577. (body) => {
  578. object(body)
  579. array(body.items)
  580. object(body.cursor)
  581. },
  582. "none",
  583. ),
  584. http.protected
  585. .get("/api/session", "v2.session.list.filters")
  586. .at((ctx) => ({
  587. path: `/api/session?${new URLSearchParams({
  588. limit: "2",
  589. order: "asc",
  590. path: ".",
  591. roots: "false",
  592. start: "0",
  593. search: "missing",
  594. directory: ctx.directory ?? "",
  595. })}`,
  596. headers: ctx.headers(),
  597. }))
  598. .json(
  599. 200,
  600. (body) => {
  601. object(body)
  602. array(body.items)
  603. object(body.cursor)
  604. },
  605. "none",
  606. ),
  607. http.protected
  608. .get("/api/session", "v2.session.list.cursor")
  609. .at((ctx) => ({
  610. path: `/api/session?${new URLSearchParams({
  611. limit: "2",
  612. directory: ctx.directory ?? "",
  613. cursor: cursor({
  614. id: "ses_httpapi_missing",
  615. time: 0,
  616. order: "desc",
  617. direction: "next",
  618. directory: ctx.directory,
  619. }),
  620. })}`,
  621. headers: ctx.headers(),
  622. }))
  623. .json(
  624. 200,
  625. (body) => {
  626. object(body)
  627. array(body.items)
  628. object(body.cursor)
  629. },
  630. "none",
  631. ),
  632. http.protected
  633. .get("/api/session", "v2.session.list.cursor.invalid")
  634. .at((ctx) => ({
  635. path: `/api/session?${new URLSearchParams({
  636. cursor: cursor({ id: "ses_httpapi_missing", time: 0, order: "desc", direction: "next" }),
  637. search: "not-allowed-with-cursor",
  638. })}`,
  639. headers: ctx.headers(),
  640. }))
  641. .status(400, undefined, "none"),
  642. http.protected
  643. .get("/api/session/{sessionID}/context", "v2.session.context")
  644. .at((ctx) => ({
  645. path: route("/api/session/{sessionID}/context", { sessionID: "ses_httpapi_missing" }),
  646. headers: ctx.headers(),
  647. }))
  648. .json(404, object, "status"),
  649. http.protected
  650. .get("/api/session/{sessionID}/message", "v2.session.messages")
  651. .at((ctx) => ({
  652. path: route("/api/session/{sessionID}/message", { sessionID: "ses_httpapi_missing" }),
  653. headers: ctx.headers(),
  654. }))
  655. .json(404, object, "status"),
  656. http.protected
  657. .get("/api/session/{sessionID}/message", "v2.session.messages.params")
  658. .at((ctx) => ({
  659. path: `${route("/api/session/{sessionID}/message", { sessionID: "ses_httpapi_missing" })}?${new URLSearchParams({
  660. limit: "2",
  661. order: "asc",
  662. })}`,
  663. headers: ctx.headers(),
  664. }))
  665. .json(404, object, "status"),
  666. http.protected
  667. .get("/api/session/{sessionID}/message", "v2.session.messages.cursor")
  668. .at((ctx) => ({
  669. path: `${route("/api/session/{sessionID}/message", { sessionID: "ses_httpapi_missing" })}?${new URLSearchParams({
  670. limit: "2",
  671. directory: ctx.directory ?? "",
  672. cursor: cursor({ id: "msg_httpapi_missing", time: 0, order: "desc", direction: "next" }),
  673. })}`,
  674. headers: ctx.headers(),
  675. }))
  676. .json(404, object, "status"),
  677. http.protected
  678. .get("/api/session/{sessionID}/message", "v2.session.messages.cursor.invalid")
  679. .at((ctx) => ({
  680. path: `${route("/api/session/{sessionID}/message", { sessionID: "ses_httpapi_missing" })}?${new URLSearchParams({
  681. cursor: cursor({ id: "msg_httpapi_missing", time: 0, order: "desc", direction: "next" }),
  682. order: "asc",
  683. })}`,
  684. headers: ctx.headers(),
  685. }))
  686. .status(400, undefined, "none"),
  687. http.protected
  688. .post("/api/session/{sessionID}/prompt", "v2.session.prompt.invalid")
  689. .at((ctx) => ({
  690. path: route("/api/session/{sessionID}/prompt", { sessionID: "ses_httpapi_missing" }),
  691. headers: ctx.headers(),
  692. body: {},
  693. }))
  694. .status(400, undefined, "none"),
  695. http.protected
  696. .post("/api/session/{sessionID}/compact", "v2.session.compact")
  697. .at((ctx) => ({
  698. path: route("/api/session/{sessionID}/compact", { sessionID: "ses_httpapi_missing" }),
  699. headers: ctx.headers(),
  700. }))
  701. .status(404, undefined, "status"),
  702. http.protected
  703. .post("/api/session/{sessionID}/wait", "v2.session.wait")
  704. .at((ctx) => ({
  705. path: route("/api/session/{sessionID}/wait", { sessionID: "ses_httpapi_missing" }),
  706. headers: ctx.headers(),
  707. }))
  708. .status(404, undefined, "status"),
  709. http.protected
  710. .get("/session", "session.list")
  711. .seeded((ctx) => ctx.session({ title: "List me" }))
  712. .at((ctx) => ({ path: "/session?roots=true", headers: ctx.headers() }))
  713. .json(200, (body, ctx) => {
  714. array(body)
  715. check(
  716. body.some((item) => isRecord(item) && item.id === ctx.state.id && item.title === "List me"),
  717. "seeded session should be listed",
  718. )
  719. }),
  720. http.protected
  721. .get("/session/status", "session.status")
  722. .seeded((ctx) => ctx.session({ title: "Status session" }))
  723. .json(200, object),
  724. http.protected
  725. .post("/session", "session.create")
  726. .mutating()
  727. .at((ctx) => ({ path: "/session", headers: ctx.headers(), body: { title: "Created session" } }))
  728. .json(
  729. 200,
  730. (body, ctx) => {
  731. object(body)
  732. check(body.title === "Created session", "created session should use requested title")
  733. check(body.directory === ctx.directory, "created session should use scenario directory")
  734. },
  735. "status",
  736. ),
  737. http.protected
  738. .get("/session/{sessionID}", "session.get")
  739. .seeded((ctx) => ctx.session({ title: "Get me" }))
  740. .at((ctx) => ({ path: route("/session/{sessionID}", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  741. .json(200, (body, ctx) => {
  742. object(body)
  743. check(body.id === ctx.state.id, "should return requested session")
  744. check(body.title === "Get me", "should preserve seeded title")
  745. }),
  746. http.protected
  747. .get("/session/{sessionID}", "session.get.missing")
  748. .at((ctx) => ({
  749. path: route("/session/{sessionID}", { sessionID: "ses_httpapi_missing" }),
  750. headers: ctx.headers(),
  751. }))
  752. .status(404),
  753. http.protected
  754. .patch("/session/{sessionID}", "session.update")
  755. .mutating()
  756. .seeded((ctx) => ctx.session({ title: "Before rename" }))
  757. .at((ctx) => ({
  758. path: route("/session/{sessionID}", { sessionID: ctx.state.id }),
  759. headers: ctx.headers(),
  760. body: { title: "After rename" },
  761. }))
  762. .json(
  763. 200,
  764. (body) => {
  765. object(body)
  766. check(body.title === "After rename", "updated session should use new title")
  767. },
  768. "status",
  769. ),
  770. http.protected
  771. .patch("/session/{sessionID}", "session.update.invalid")
  772. .mutating()
  773. .at((ctx) => ({
  774. path: route("/session/{sessionID}", { sessionID: "ses_httpapi_missing" }),
  775. headers: ctx.headers(),
  776. body: { title: 1 },
  777. }))
  778. .status(400),
  779. http.protected
  780. .delete("/session/{sessionID}", "session.delete")
  781. .mutating()
  782. .seeded((ctx) => ctx.session({ title: "Delete me" }))
  783. .at((ctx) => ({ path: route("/session/{sessionID}", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  784. .jsonEffect(200, (body, ctx) =>
  785. Effect.gen(function* () {
  786. check(body === true, "delete should return true")
  787. check((yield* ctx.sessionGet(ctx.state.id)) === undefined, "deleted session should not remain in storage")
  788. }),
  789. ),
  790. http.protected
  791. .get("/session/{sessionID}/children", "session.children")
  792. .seeded((ctx) =>
  793. Effect.gen(function* () {
  794. const parent = yield* ctx.session({ title: "Parent" })
  795. const child = yield* ctx.session({ title: "Child", parentID: parent.id })
  796. return { parent, child }
  797. }),
  798. )
  799. .at((ctx) => ({
  800. path: route("/session/{sessionID}/children", { sessionID: ctx.state.parent.id }),
  801. headers: ctx.headers(),
  802. }))
  803. .json(200, (body, ctx) => {
  804. array(body)
  805. check(
  806. body.some((item) => isRecord(item) && item.id === ctx.state.child.id && item.parentID === ctx.state.parent.id),
  807. "children should include seeded child",
  808. )
  809. }),
  810. http.protected
  811. .get("/session/{sessionID}/todo", "session.todo")
  812. .seeded((ctx) =>
  813. Effect.gen(function* () {
  814. const session = yield* ctx.session({ title: "Todo session" })
  815. const todos = [{ content: "cover session todo", status: "pending", priority: "high" }]
  816. yield* ctx.todos(session.id, todos)
  817. return { session, todos }
  818. }),
  819. )
  820. .at((ctx) => ({
  821. path: route("/session/{sessionID}/todo", { sessionID: ctx.state.session.id }),
  822. headers: ctx.headers(),
  823. }))
  824. .json(200, (body, ctx) => {
  825. check(stable(body) === stable(ctx.state.todos), "todos should match seeded state")
  826. }),
  827. http.protected
  828. .get("/session/{sessionID}/diff", "session.diff")
  829. .seeded((ctx) => ctx.session({ title: "Diff session" }))
  830. .at((ctx) => ({ path: route("/session/{sessionID}/diff", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  831. .json(200, array),
  832. http.protected
  833. .get("/session/{sessionID}/message", "session.messages")
  834. .seeded((ctx) => ctx.session({ title: "Messages session" }))
  835. .at((ctx) => ({ path: route("/session/{sessionID}/message", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  836. .json(200, (body) => {
  837. array(body)
  838. check(body.length === 0, "new session should have no messages")
  839. }),
  840. http.protected
  841. .get("/session/{sessionID}/message/{messageID}", "session.message")
  842. .seeded((ctx) =>
  843. Effect.gen(function* () {
  844. const session = yield* ctx.session({ title: "Message get session" })
  845. const message = yield* ctx.message(session.id, { text: "read me" })
  846. return { session, message }
  847. }),
  848. )
  849. .at((ctx) => ({
  850. path: route("/session/{sessionID}/message/{messageID}", {
  851. sessionID: ctx.state.session.id,
  852. messageID: ctx.state.message.info.id,
  853. }),
  854. headers: ctx.headers(),
  855. }))
  856. .json(200, (body, ctx) => {
  857. object(body)
  858. check(isRecord(body.info) && body.info.id === ctx.state.message.info.id, "should return requested message")
  859. check(
  860. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.id === ctx.state.message.part.id),
  861. "message should include seeded part",
  862. )
  863. }),
  864. http.protected
  865. .patch("/session/{sessionID}/message/{messageID}/part/{partID}", "part.update")
  866. .mutating()
  867. .seeded((ctx) =>
  868. Effect.gen(function* () {
  869. const session = yield* ctx.session({ title: "Part update session" })
  870. const message = yield* ctx.message(session.id, { text: "before" })
  871. return { session, message }
  872. }),
  873. )
  874. .at((ctx) => ({
  875. path: route("/session/{sessionID}/message/{messageID}/part/{partID}", {
  876. sessionID: ctx.state.session.id,
  877. messageID: ctx.state.message.info.id,
  878. partID: ctx.state.message.part.id,
  879. }),
  880. headers: ctx.headers(),
  881. body: { ...ctx.state.message.part, text: "after" },
  882. }))
  883. .json(
  884. 200,
  885. (body) => {
  886. object(body)
  887. check(body.type === "text" && body.text === "after", "updated part should be returned")
  888. },
  889. "status",
  890. ),
  891. http.protected
  892. .delete("/session/{sessionID}/message/{messageID}/part/{partID}", "part.delete")
  893. .mutating()
  894. .seeded((ctx) =>
  895. Effect.gen(function* () {
  896. const session = yield* ctx.session({ title: "Part delete session" })
  897. const message = yield* ctx.message(session.id, { text: "delete part" })
  898. return { session, message }
  899. }),
  900. )
  901. .at((ctx) => ({
  902. path: route("/session/{sessionID}/message/{messageID}/part/{partID}", {
  903. sessionID: ctx.state.session.id,
  904. messageID: ctx.state.message.info.id,
  905. partID: ctx.state.message.part.id,
  906. }),
  907. headers: ctx.headers(),
  908. }))
  909. .jsonEffect(200, (body, ctx) =>
  910. Effect.gen(function* () {
  911. check(body === true, "delete part should return true")
  912. const messages = yield* ctx.messages(ctx.state.session.id)
  913. check(messages[0]?.parts.length === 0, "deleted part should not remain on message")
  914. }),
  915. ),
  916. http.protected
  917. .delete("/session/{sessionID}/message/{messageID}", "session.deleteMessage")
  918. .mutating()
  919. .seeded((ctx) =>
  920. Effect.gen(function* () {
  921. const session = yield* ctx.session({ title: "Message delete session" })
  922. const message = yield* ctx.message(session.id, { text: "delete message" })
  923. return { session, message }
  924. }),
  925. )
  926. .at((ctx) => ({
  927. path: route("/session/{sessionID}/message/{messageID}", {
  928. sessionID: ctx.state.session.id,
  929. messageID: ctx.state.message.info.id,
  930. }),
  931. headers: ctx.headers(),
  932. }))
  933. .jsonEffect(200, (body, ctx) =>
  934. Effect.gen(function* () {
  935. check(body === true, "delete message should return true")
  936. check((yield* ctx.messages(ctx.state.session.id)).length === 0, "deleted message should not remain")
  937. }),
  938. ),
  939. http.protected
  940. .post("/session/{sessionID}/fork", "session.fork")
  941. .mutating()
  942. .seeded((ctx) => ctx.session({ title: "Fork source" }))
  943. .at((ctx) => ({
  944. path: route("/session/{sessionID}/fork", { sessionID: ctx.state.id }),
  945. headers: ctx.headers(),
  946. body: {},
  947. }))
  948. .json(
  949. 200,
  950. (body) => {
  951. object(body)
  952. check(typeof body.id === "string", "fork should return a session")
  953. },
  954. "status",
  955. ),
  956. http.protected
  957. .post("/session/{sessionID}/abort", "session.abort")
  958. .mutating()
  959. .seeded((ctx) => ctx.session({ title: "Abort session" }))
  960. .at((ctx) => ({ path: route("/session/{sessionID}/abort", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  961. .json(200, (body) => {
  962. check(body === true, "abort should return true")
  963. }),
  964. http.protected
  965. .post("/session/{sessionID}/abort", "session.abort.missing")
  966. .at((ctx) => ({
  967. path: route("/session/{sessionID}/abort", { sessionID: "ses_httpapi_missing" }),
  968. headers: ctx.headers(),
  969. }))
  970. .json(200, (body) => {
  971. check(body === true, "missing session abort should remain a no-op success")
  972. }),
  973. http.protected
  974. .post("/session/{sessionID}/init", "session.init")
  975. .preserveDatabase()
  976. .withLlm()
  977. .seeded((ctx) =>
  978. Effect.gen(function* () {
  979. const session = yield* ctx.session({ title: "Init session" })
  980. const message = yield* ctx.message(session.id, { text: "initialize" })
  981. yield* ctx.llmText("initialized")
  982. yield* ctx.llmText("initialized")
  983. return { session, message }
  984. }),
  985. )
  986. .at((ctx) => ({
  987. path: route("/session/{sessionID}/init", { sessionID: ctx.state.session.id }),
  988. headers: ctx.headers(),
  989. body: { providerID: "test", modelID: "test-model", messageID: ctx.state.message.info.id },
  990. }))
  991. .jsonEffect(200, (body, ctx) =>
  992. Effect.gen(function* () {
  993. check(body === true, "init should return true")
  994. yield* ctx.llmWait(1)
  995. }),
  996. ),
  997. http.protected
  998. .post("/session/{sessionID}/message", "session.prompt")
  999. .preserveDatabase()
  1000. .withLlm()
  1001. .seeded((ctx) =>
  1002. Effect.gen(function* () {
  1003. const session = yield* ctx.session({ title: "LLM prompt session" })
  1004. yield* ctx.llmText("fake assistant")
  1005. yield* ctx.llmText("fake assistant")
  1006. return session
  1007. }),
  1008. )
  1009. .at((ctx) => ({
  1010. path: route("/session/{sessionID}/message", { sessionID: ctx.state.id }),
  1011. headers: ctx.headers(),
  1012. body: {
  1013. agent: "build",
  1014. model: { providerID: "test", modelID: "test-model" },
  1015. parts: [{ type: "text", text: "hello llm" }],
  1016. },
  1017. }))
  1018. .jsonEffect(
  1019. 200,
  1020. (body, ctx) =>
  1021. Effect.gen(function* () {
  1022. object(body)
  1023. check(isRecord(body.info) && body.info.role === "assistant", "prompt should return assistant message")
  1024. check(
  1025. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.text === "fake assistant"),
  1026. "assistant message should use fake LLM text",
  1027. )
  1028. yield* ctx.llmWait(1)
  1029. }),
  1030. "status",
  1031. ),
  1032. http.protected
  1033. .post("/session/{sessionID}/prompt_async", "session.prompt_async")
  1034. .preserveDatabase()
  1035. .withLlm()
  1036. .seeded((ctx) =>
  1037. Effect.gen(function* () {
  1038. const session = yield* ctx.session({ title: "Async prompt session" })
  1039. yield* ctx.llmText("fake async assistant")
  1040. yield* ctx.llmText("fake async assistant")
  1041. return session
  1042. }),
  1043. )
  1044. .at((ctx) => ({
  1045. path: route("/session/{sessionID}/prompt_async", { sessionID: ctx.state.id }),
  1046. headers: ctx.headers(),
  1047. body: {
  1048. agent: "build",
  1049. model: { providerID: "test", modelID: "test-model" },
  1050. parts: [{ type: "text", text: "hello async" }],
  1051. },
  1052. }))
  1053. .status(204, (ctx) =>
  1054. Effect.gen(function* () {
  1055. yield* ctx.llmWait(1)
  1056. }),
  1057. ),
  1058. http.protected
  1059. .post("/session/{sessionID}/command", "session.command")
  1060. .preserveDatabase()
  1061. .withLlm()
  1062. .seeded((ctx) =>
  1063. Effect.gen(function* () {
  1064. const session = yield* ctx.session({ title: "Command session" })
  1065. yield* ctx.llmText("command done")
  1066. yield* ctx.llmText("command done")
  1067. return session
  1068. }),
  1069. )
  1070. .at((ctx) => ({
  1071. path: route("/session/{sessionID}/command", { sessionID: ctx.state.id }),
  1072. headers: ctx.headers(),
  1073. body: { command: "init", arguments: "", model: "test/test-model" },
  1074. }))
  1075. .jsonEffect(
  1076. 200,
  1077. (body, ctx) =>
  1078. Effect.gen(function* () {
  1079. object(body)
  1080. check(isRecord(body.info) && body.info.role === "assistant", "command should return assistant message")
  1081. yield* ctx.llmWait(1)
  1082. }),
  1083. "status",
  1084. ),
  1085. http.protected
  1086. .post("/session/{sessionID}/shell", "session.shell")
  1087. .preserveDatabase()
  1088. .mutating()
  1089. .seeded((ctx) => ctx.session({ title: "Shell session" }))
  1090. .at((ctx) => ({
  1091. path: route("/session/{sessionID}/shell", { sessionID: ctx.state.id }),
  1092. headers: ctx.headers(),
  1093. body: { agent: "build", model: { providerID: "test", modelID: "test-model" }, command: "printf shell-ok" },
  1094. }))
  1095. .json(
  1096. 200,
  1097. (body) => {
  1098. object(body)
  1099. check(isRecord(body.info) && body.info.role === "assistant", "shell should return assistant message")
  1100. check(
  1101. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.type === "tool"),
  1102. "shell should return a tool part",
  1103. )
  1104. },
  1105. "status",
  1106. ),
  1107. http.protected
  1108. .post("/session/{sessionID}/summarize", "session.summarize")
  1109. .preserveDatabase()
  1110. .withLlm()
  1111. .seeded((ctx) =>
  1112. Effect.gen(function* () {
  1113. const session = yield* ctx.session({ title: "Summarize session" })
  1114. yield* ctx.message(session.id, { text: "summarize this work" })
  1115. const summary = [
  1116. "## Goal",
  1117. "- Exercise session summarize.",
  1118. "",
  1119. "## Constraints & Preferences",
  1120. "- Use fake LLM.",
  1121. "",
  1122. "## Progress",
  1123. "### Done",
  1124. "- Summary generated.",
  1125. "",
  1126. "### In Progress",
  1127. "- (none)",
  1128. "",
  1129. "### Blocked",
  1130. "- (none)",
  1131. "",
  1132. "## Key Decisions",
  1133. "- Keep route local.",
  1134. "",
  1135. "## Next Steps",
  1136. "- (none)",
  1137. "",
  1138. "## Critical Context",
  1139. "- Test fixture.",
  1140. "",
  1141. "## Relevant Files",
  1142. "- test/server/httpapi-exercise/index.ts: scenario",
  1143. ].join("\n")
  1144. yield* ctx.llmText(summary)
  1145. yield* ctx.llmText(summary)
  1146. return session
  1147. }),
  1148. )
  1149. .at((ctx) => ({
  1150. path: route("/session/{sessionID}/summarize", { sessionID: ctx.state.id }),
  1151. headers: ctx.headers(),
  1152. body: { providerID: "test", modelID: "test-model", auto: false },
  1153. }))
  1154. .jsonEffect(
  1155. 200,
  1156. (body, ctx) =>
  1157. Effect.gen(function* () {
  1158. check(body === true, "summarize should return true")
  1159. const messages = yield* ctx.messages(ctx.state.id)
  1160. check(
  1161. messages.some((message) => message.info.role === "assistant" && message.info.summary === true),
  1162. "summarize should create a summary assistant message",
  1163. )
  1164. yield* ctx.llmWait(1)
  1165. }),
  1166. "status",
  1167. ),
  1168. http.protected
  1169. .post("/session/{sessionID}/revert", "session.revert")
  1170. .mutating()
  1171. .seeded((ctx) =>
  1172. Effect.gen(function* () {
  1173. const session = yield* ctx.session({ title: "Revert session" })
  1174. const message = yield* ctx.message(session.id, { text: "revert me" })
  1175. return { session, message }
  1176. }),
  1177. )
  1178. .at((ctx) => ({
  1179. path: route("/session/{sessionID}/revert", { sessionID: ctx.state.session.id }),
  1180. headers: ctx.headers(),
  1181. body: { messageID: ctx.state.message.info.id },
  1182. }))
  1183. .json(
  1184. 200,
  1185. (body, ctx) => {
  1186. object(body)
  1187. check(body.id === ctx.state.session.id, "revert should return the session")
  1188. check(
  1189. isRecord(body.revert) && body.revert.messageID === ctx.state.message.info.id,
  1190. "revert should record reverted message",
  1191. )
  1192. },
  1193. "status",
  1194. ),
  1195. http.protected
  1196. .post("/session/{sessionID}/unrevert", "session.unrevert")
  1197. .mutating()
  1198. .seeded((ctx) => ctx.session({ title: "Unrevert session" }))
  1199. .at((ctx) => ({
  1200. path: route("/session/{sessionID}/unrevert", { sessionID: ctx.state.id }),
  1201. headers: ctx.headers(),
  1202. }))
  1203. .json(
  1204. 200,
  1205. (body, ctx) => {
  1206. object(body)
  1207. check(body.id === ctx.state.id, "unrevert should return the session")
  1208. },
  1209. "status",
  1210. ),
  1211. http.protected
  1212. .post("/session/{sessionID}/permissions/{permissionID}", "permission.respond")
  1213. .seeded((ctx) => ctx.session({ title: "Deprecated permission session" }))
  1214. .at((ctx) => ({
  1215. path: route("/session/{sessionID}/permissions/{permissionID}", {
  1216. sessionID: ctx.state.id,
  1217. permissionID: "per_httpapi_deprecated",
  1218. }),
  1219. headers: ctx.headers(),
  1220. body: { response: "once" },
  1221. }))
  1222. .json(404, object, "status"),
  1223. http.protected
  1224. .post("/session/{sessionID}/share", "session.share")
  1225. .mutating()
  1226. .seeded((ctx) => ctx.session({ title: "Share session" }))
  1227. .at((ctx) => ({ path: route("/session/{sessionID}/share", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1228. .json(
  1229. 200,
  1230. (body, ctx) => {
  1231. object(body)
  1232. check(body.id === ctx.state.id, "share should return the session")
  1233. },
  1234. "status",
  1235. ),
  1236. http.protected
  1237. .delete("/session/{sessionID}/share", "session.unshare")
  1238. .mutating()
  1239. .seeded((ctx) => ctx.session({ title: "Unshare session" }))
  1240. .at((ctx) => ({ path: route("/session/{sessionID}/share", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1241. .json(
  1242. 200,
  1243. (body, ctx) => {
  1244. object(body)
  1245. check(body.id === ctx.state.id, "unshare should return the session")
  1246. },
  1247. "status",
  1248. ),
  1249. http.protected
  1250. .post("/tui/append-prompt", "tui.appendPrompt")
  1251. .at((ctx) => ({ path: "/tui/append-prompt", headers: ctx.headers(), body: { text: "hello" } }))
  1252. .json(200, boolean, "status"),
  1253. http.protected
  1254. .post("/tui/select-session", "tui.selectSession.invalid")
  1255. .at((ctx) => ({ path: "/tui/select-session", headers: ctx.headers(), body: { sessionID: "invalid" } }))
  1256. .status(400),
  1257. http.protected.post("/tui/open-help", "tui.openHelp").json(200, boolean, "status"),
  1258. http.protected.post("/tui/open-sessions", "tui.openSessions").json(200, boolean, "status"),
  1259. http.protected.post("/tui/open-themes", "tui.openThemes").json(200, boolean, "status"),
  1260. http.protected.post("/tui/open-models", "tui.openModels").json(200, boolean, "status"),
  1261. http.protected.post("/tui/submit-prompt", "tui.submitPrompt").json(200, boolean, "status"),
  1262. http.protected.post("/tui/clear-prompt", "tui.clearPrompt").json(200, boolean, "status"),
  1263. http.protected
  1264. .post("/tui/execute-command", "tui.executeCommand")
  1265. .at((ctx) => ({ path: "/tui/execute-command", headers: ctx.headers(), body: { command: "agent_cycle" } }))
  1266. .json(200, boolean, "status"),
  1267. http.protected
  1268. .post("/tui/show-toast", "tui.showToast")
  1269. .at((ctx) => ({
  1270. path: "/tui/show-toast",
  1271. headers: ctx.headers(),
  1272. body: { title: "Exercise", message: "covered", variant: "info", duration: 1000 },
  1273. }))
  1274. .json(200, boolean, "status"),
  1275. http.protected
  1276. .post("/tui/publish", "tui.publish")
  1277. .at((ctx) => ({
  1278. path: "/tui/publish",
  1279. headers: ctx.headers(),
  1280. body: { type: "tui.prompt.append", properties: { text: "published" } },
  1281. }))
  1282. .json(200, boolean, "status"),
  1283. http.protected
  1284. .post("/tui/select-session", "tui.selectSession")
  1285. .seeded((ctx) => ctx.session({ title: "TUI select" }))
  1286. .at((ctx) => ({ path: "/tui/select-session", headers: ctx.headers(), body: { sessionID: ctx.state.id } }))
  1287. .json(200, boolean, "status"),
  1288. http.protected
  1289. .post("/tui/control/response", "tui.control.response")
  1290. .at((ctx) => ({ path: "/tui/control/response", headers: ctx.headers(), body: { ok: true } }))
  1291. .json(200, boolean, "status"),
  1292. http.protected
  1293. .get("/tui/control/next", "tui.control.next")
  1294. .mutating()
  1295. .seeded((ctx) => ctx.tuiRequest({ path: "/tui/exercise", body: { text: "queued" } }))
  1296. .json(
  1297. 200,
  1298. (body) => {
  1299. object(body)
  1300. check(body.path === "/tui/exercise", "control next should return queued path")
  1301. object(body.body)
  1302. check(body.body.text === "queued", "control next should return queued body")
  1303. },
  1304. "status",
  1305. ),
  1306. http.protected
  1307. .post("/global/upgrade", "global.upgrade")
  1308. .global()
  1309. .probe({ path: "/global/upgrade", body: { target: 1 } })
  1310. .at(() => ({ path: "/global/upgrade", body: { target: 1 } }))
  1311. .status(400),
  1312. ]
  1313. const llmScenarios = new Set([
  1314. "session.init",
  1315. "session.prompt",
  1316. "session.prompt_async",
  1317. "session.command",
  1318. "session.summarize",
  1319. ])
  1320. const main = Effect.gen(function* () {
  1321. yield* Effect.addFinalizer(() => cleanupExercisePaths)
  1322. const options = parseOptions(Bun.argv.slice(2))
  1323. const modules = yield* Effect.promise(() => runtime())
  1324. const effectRoutes = routeKeys(OpenApi.fromApi(modules.PublicApi))
  1325. const selected = selectedScenarios(options, scenarios)
  1326. const missing = effectRoutes.filter((route) => !scenarios.some((scenario) => route === routeKey(scenario)))
  1327. const extra = scenarios.filter((scenario) => !effectRoutes.includes(routeKey(scenario)))
  1328. for (const scenario of scenarios) {
  1329. if (scenario.kind === "active" && llmScenarios.has(scenario.name) && !scenario.project?.llm) {
  1330. return yield* Effect.fail(new Error(`${scenario.name} must use TestLLMServer via .withLlm()`))
  1331. }
  1332. }
  1333. printHeader(options, effectRoutes, selected, missing, extra, {
  1334. database: exerciseDatabasePath,
  1335. global: exerciseGlobalRoot,
  1336. })
  1337. const results =
  1338. options.mode === "coverage"
  1339. ? selected.map(coverageResult)
  1340. : yield* Effect.forEach(
  1341. selected,
  1342. (scenario) =>
  1343. Effect.gen(function* () {
  1344. if (options.progress) console.log(`${color.dim}RUN ${routeKey(scenario)} ${scenario.name}${color.reset}`)
  1345. return yield* runScenario(options)(scenario)
  1346. }),
  1347. { concurrency: 1 },
  1348. )
  1349. printResults(results, missing, extra)
  1350. if (results.some((result) => result.status === "fail"))
  1351. return yield* Effect.fail(new Error("one or more scenarios failed"))
  1352. if (options.failOnSkip && results.some((result) => result.status === "skip"))
  1353. return yield* Effect.fail(new Error("one or more scenarios are skipped"))
  1354. if (options.failOnMissing && missing.length > 0)
  1355. return yield* Effect.fail(new Error("one or more routes have no scenario"))
  1356. return undefined
  1357. })
  1358. Effect.runPromise(main.pipe(Effect.provide(TestLLMServer.layer), Effect.scoped)).then(
  1359. () => process.exit(0),
  1360. (error: unknown) => {
  1361. console.error(`${color.red}${message(error)}${color.reset}`)
  1362. process.exit(1)
  1363. },
  1364. )