index.ts 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  1. /**
  2. * End-to-end exerciser for the Effect HttpApi routes.
  3. *
  4. * The goal is not to be a normal unit test file. This is a route-coverage harness:
  5. * every public route should have a small scenario that proves the route decodes
  6. * requests, uses the right instance context, mutates storage when expected, and
  7. * returns the expected response shape.
  8. *
  9. * The script intentionally isolates `OPENCODE_DB` before importing modules that touch
  10. * storage. Scenarios may create/delete sessions and reset the database after each run,
  11. * so this must never point at a developer's real session database.
  12. *
  13. * DSL shape:
  14. * - `http.protected.get/post/...` starts a scenario for one OpenAPI route key.
  15. * - `.seeded(...)` creates typed per-scenario state using Effect helpers on `ctx`.
  16. * - `.at(...)` builds the request from that typed state.
  17. * - `.json(...)` / `.jsonEffect(...)` assert response shape and optional side effects.
  18. * - `.mutating()` tells the runner to reset isolated state after destructive routes.
  19. */
  20. import { Effect } from "effect"
  21. import { OpenApi } from "effect/unstable/httpapi"
  22. import { TestLLMServer } from "../../lib/llm-server"
  23. import path from "path"
  24. import { array, boolean, check, isRecord, message, object, stable } from "./assertions"
  25. import { controlledPtyInput, http, route } from "./dsl"
  26. import {
  27. cleanupExercisePaths,
  28. exerciseConfigDirectory,
  29. exerciseDataDirectory,
  30. exerciseDatabasePath,
  31. exerciseGlobalRoot,
  32. } from "./environment"
  33. import { color, printHeader, printResults } from "./report"
  34. import { coverageResult, parseOptions, routeKey, routeKeys, selectedScenarios } from "./routing"
  35. import { runScenario } from "./runner"
  36. import { disposeApps } from "./backend"
  37. import { runtime } from "./runtime"
  38. import { type Scenario } from "./types"
  39. function cursor(input: Record<string, unknown>) {
  40. return Buffer.from(JSON.stringify(input)).toString("base64url")
  41. }
  42. function data(validate: (value: any) => void) {
  43. return (body: any) => {
  44. object(body)
  45. validate(body.data)
  46. }
  47. }
  48. function locationData(validate: (value: any) => void) {
  49. return (body: any) => {
  50. object(body)
  51. object(body.location)
  52. object(body.location.project)
  53. validate(body.data)
  54. }
  55. }
  56. const scenarios: Scenario[] = [
  57. http.protected
  58. .get("/global/health", "global.health")
  59. .global()
  60. .json(200, (body) => {
  61. object(body)
  62. check(body.healthy === true, "server should report healthy")
  63. }),
  64. http.protected
  65. .get("/global/event", "global.event")
  66. .global()
  67. .stream()
  68. .status(
  69. 200,
  70. (_ctx, result) =>
  71. Effect.sync(() => {
  72. check(result.contentType.includes("text/event-stream"), "global event should be an SSE stream")
  73. check(result.text.includes("server.connected"), "global event should emit initial connection event")
  74. }),
  75. "status",
  76. ),
  77. http.protected.get("/global/config", "global.config.get").global().json(),
  78. http.protected
  79. .patch("/global/config", "global.config.update")
  80. .global()
  81. .seeded(() =>
  82. Effect.promise(() =>
  83. Bun.write(
  84. path.join(exerciseConfigDirectory, "opencode.jsonc"),
  85. JSON.stringify({ username: "httpapi-global" }, null, 2),
  86. ),
  87. ),
  88. )
  89. .at(() => ({ path: "/global/config", body: { username: "httpapi-global" } }))
  90. .jsonEffect(
  91. 200,
  92. (body) =>
  93. Effect.gen(function* () {
  94. object(body)
  95. check(body.username === "httpapi-global", "global config update should return patched config")
  96. const text = yield* Effect.promise(() =>
  97. Bun.file(path.join(exerciseConfigDirectory, "opencode.jsonc")).text(),
  98. )
  99. check(text.includes('"username": "httpapi-global"'), "global config update should write isolated config file")
  100. }),
  101. "status",
  102. ),
  103. http.protected
  104. .post("/global/dispose", "global.dispose")
  105. .global()
  106. .mutating()
  107. .json(
  108. 200,
  109. (body) => {
  110. check(body === true, "global dispose should return true")
  111. },
  112. "status",
  113. ),
  114. http.protected.get("/path", "path.get").json(200, (body, ctx) => {
  115. object(body)
  116. check(body.directory === ctx.directory, "directory should resolve from x-opencode-directory")
  117. check(body.worktree === ctx.directory, "worktree should resolve from x-opencode-directory")
  118. }),
  119. http.protected.get("/vcs", "vcs.get").json(),
  120. http.protected.get("/vcs/status", "vcs.status").json(200, array),
  121. http.protected
  122. .get("/vcs/diff", "vcs.diff")
  123. .at((ctx) => ({ path: "/vcs/diff?mode=git", headers: ctx.headers() }))
  124. .json(200, array),
  125. http.protected.get("/vcs/diff/raw", "vcs.diff.raw").status(
  126. 200,
  127. (_ctx, result) =>
  128. Effect.sync(() => {
  129. check(typeof result.text === "string", "raw VCS diff should return text")
  130. }),
  131. "status",
  132. ),
  133. http.protected
  134. .post("/vcs/apply", "vcs.apply")
  135. .inProject({ git: false })
  136. .at((ctx) => ({ path: "/vcs/apply", headers: ctx.headers(), body: { patch: "" } }))
  137. .status(400, undefined, "status"),
  138. http.protected.get("/command", "command.list").json(200, array, "status"),
  139. http.protected.get("/agent", "app.agents").json(200, array, "status"),
  140. http.protected.get("/skill", "app.skills").json(200, array, "status"),
  141. http.protected.get("/lsp", "lsp.status").json(200, array),
  142. http.protected.get("/formatter", "formatter.status").json(200, array),
  143. http.protected.get("/config", "config.get").json(200, undefined, "status"),
  144. http.protected
  145. .patch("/config", "config.update")
  146. .mutating()
  147. .at((ctx) => ({ path: "/config", headers: ctx.headers(), body: { username: "httpapi-local" } }))
  148. .json(
  149. 200,
  150. (body) => {
  151. object(body)
  152. check(body.username === "httpapi-local", "local config update should return patched config")
  153. },
  154. "status",
  155. ),
  156. http.protected
  157. .patch("/config", "config.update.invalid")
  158. .at((ctx) => ({ path: "/config", headers: ctx.headers(), body: { username: 1 } }))
  159. .status(400),
  160. http.protected.get("/config/providers", "config.providers").json(),
  161. http.protected.get("/project", "project.list").json(200, array, "status"),
  162. http.protected.get("/project/current", "project.current").json(
  163. 200,
  164. (body, ctx) => {
  165. object(body)
  166. check(body.worktree === ctx.directory, "current project should resolve from scenario directory")
  167. },
  168. "status",
  169. ),
  170. http.protected
  171. .patch("/project/{projectID}", "project.update")
  172. .mutating()
  173. .seeded((ctx) => ctx.project())
  174. .at((ctx) => ({
  175. path: route("/project/{projectID}", { projectID: ctx.state.id }),
  176. headers: ctx.headers(),
  177. body: { name: "HTTP API Project", commands: { start: "bun --version" } },
  178. }))
  179. .json(
  180. 200,
  181. (body) => {
  182. object(body)
  183. check(body.name === "HTTP API Project", "project update should return patched name")
  184. check(
  185. isRecord(body.commands) && body.commands.start === "bun --version",
  186. "project update should return patched command",
  187. )
  188. },
  189. "status",
  190. ),
  191. http.protected
  192. .patch("/project/{projectID}", "project.update.missing")
  193. .mutating()
  194. .at((ctx) => ({
  195. path: route("/project/{projectID}", { projectID: "project_httpapi_missing" }),
  196. headers: ctx.headers(),
  197. body: { name: "Missing Project" },
  198. }))
  199. .json(404, object, "status"),
  200. http.protected
  201. .post("/project/git/init", "project.initGit")
  202. .mutating()
  203. .inProject({ git: false })
  204. .json(
  205. 200,
  206. (body, ctx) => {
  207. object(body)
  208. check(body.worktree === ctx.directory, "git init should return current project")
  209. check(body.vcs === "git", "git init should mark the project as git-backed")
  210. },
  211. "status",
  212. ),
  213. http.protected
  214. .get("/project/{projectID}/directories", "project.directories")
  215. .seeded((ctx) => ctx.project())
  216. .at((ctx) => ({
  217. path: route("/project/{projectID}/directories", { projectID: ctx.state.id }),
  218. headers: ctx.headers(),
  219. }))
  220. .json(200, array, "status"),
  221. http.protected
  222. .post("/experimental/project/{projectID}/copy/generate-name", "experimental.projectCopy.generateName")
  223. .seeded((ctx) => ctx.project())
  224. .at((ctx) => ({
  225. path: route("/experimental/project/{projectID}/copy/generate-name", { projectID: ctx.state.id }),
  226. headers: ctx.headers(),
  227. body: {},
  228. }))
  229. .json(200, (body) => {
  230. object(body)
  231. check(typeof body.name === "string" && body.name.length > 0, "generated copy name should be non-empty")
  232. }),
  233. http.protected
  234. .post("/experimental/project/{projectID}/copy", "experimental.projectCopy.create")
  235. .seeded((ctx) => ctx.project())
  236. .at((ctx) => ({
  237. path: route("/experimental/project/{projectID}/copy", { projectID: ctx.state.id }),
  238. headers: ctx.headers(),
  239. body: {},
  240. }))
  241. .status(400),
  242. http.protected
  243. .delete("/experimental/project/{projectID}/copy", "experimental.projectCopy.remove")
  244. .seeded((ctx) => ctx.project())
  245. .at((ctx) => ({
  246. path: route("/experimental/project/{projectID}/copy", { projectID: ctx.state.id }),
  247. headers: ctx.headers(),
  248. body: {},
  249. }))
  250. .status(400),
  251. http.protected
  252. .post("/experimental/project/{projectID}/copy/refresh", "experimental.projectCopy.refresh")
  253. .mutating()
  254. .seeded((ctx) => ctx.project())
  255. .at((ctx) => ({
  256. path: route("/experimental/project/{projectID}/copy/refresh", { projectID: ctx.state.id }),
  257. headers: ctx.headers(),
  258. }))
  259. .status(204, undefined, "status"),
  260. http.protected.get("/provider", "provider.list").json(),
  261. http.protected.get("/provider/auth", "provider.auth").json(),
  262. http.protected
  263. .post("/provider/{providerID}/oauth/authorize", "provider.oauth.authorize")
  264. .at((ctx) => ({
  265. path: route("/provider/{providerID}/oauth/authorize", { providerID: "httpapi" }),
  266. headers: ctx.headers(),
  267. body: { method: "bad" },
  268. }))
  269. .status(400),
  270. http.protected
  271. .post("/provider/{providerID}/oauth/callback", "provider.oauth.callback")
  272. .at((ctx) => ({
  273. path: route("/provider/{providerID}/oauth/callback", { providerID: "httpapi" }),
  274. headers: ctx.headers(),
  275. body: { method: "bad" },
  276. }))
  277. .status(400),
  278. http.protected.get("/permission", "permission.list").json(200, array),
  279. http.protected
  280. .post("/permission/{requestID}/reply", "permission.reply.invalid")
  281. .at((ctx) => ({
  282. path: route("/permission/{requestID}/reply", { requestID: "per_httpapi" }),
  283. headers: ctx.headers(),
  284. body: { reply: "bad" },
  285. }))
  286. .status(400),
  287. http.protected
  288. .post("/permission/{requestID}/reply", "permission.reply")
  289. .at((ctx) => ({
  290. path: route("/permission/{requestID}/reply", { requestID: "per_httpapi" }),
  291. headers: ctx.headers(),
  292. body: { reply: "once" },
  293. }))
  294. .json(404, object, "status"),
  295. http.protected.get("/question", "question.list").json(200, array),
  296. http.protected
  297. .post("/question/{requestID}/reply", "question.reply.invalid")
  298. .at((ctx) => ({
  299. path: route("/question/{requestID}/reply", { requestID: "que_httpapi_reply" }),
  300. headers: ctx.headers(),
  301. body: { answers: "Yes" },
  302. }))
  303. .status(400),
  304. http.protected
  305. .post("/question/{requestID}/reply", "question.reply")
  306. .at((ctx) => ({
  307. path: route("/question/{requestID}/reply", { requestID: "que_httpapi_reply" }),
  308. headers: ctx.headers(),
  309. body: { answers: [["Yes"]] },
  310. }))
  311. .json(404, object, "status"),
  312. http.protected
  313. .post("/question/{requestID}/reject", "question.reject")
  314. .at((ctx) => ({
  315. path: route("/question/{requestID}/reject", { requestID: "que_httpapi_reject" }),
  316. headers: ctx.headers(),
  317. }))
  318. .json(404, object, "status"),
  319. http.protected
  320. .get("/file", "file.list")
  321. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  322. .at((ctx) => ({ path: `/file?${new URLSearchParams({ path: "." })}`, headers: ctx.headers() }))
  323. .json(200, array),
  324. http.protected
  325. .get("/file/content", "file.read")
  326. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  327. .at((ctx) => ({ path: `/file/content?${new URLSearchParams({ path: "hello.txt" })}`, headers: ctx.headers() }))
  328. .json(200, (body) => {
  329. object(body)
  330. check(body.content === "hello", `content should match seeded file: ${JSON.stringify(body)}`)
  331. }),
  332. http.protected
  333. .get("/file/content", "file.read.missing")
  334. .at((ctx) => ({ path: `/file/content?${new URLSearchParams({ path: "missing.txt" })}`, headers: ctx.headers() }))
  335. .json(200, (body) => {
  336. object(body)
  337. check(body.type === "text" && body.content === "", "missing file content should return an empty text result")
  338. }),
  339. http.protected.get("/file/status", "file.status").json(200, array),
  340. http.protected
  341. .get("/find", "find.text")
  342. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  343. .at((ctx) => ({ path: `/find?${new URLSearchParams({ pattern: "hello" })}`, headers: ctx.headers() }))
  344. .json(200, array),
  345. http.protected
  346. .get("/find/file", "find.files")
  347. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  348. .at((ctx) => ({
  349. path: `/find/file?${new URLSearchParams({ query: "hello", dirs: "false" })}`,
  350. headers: ctx.headers(),
  351. }))
  352. .json(200, array),
  353. http.protected
  354. .get("/find/symbol", "find.symbols")
  355. .seeded((ctx) => ctx.file("hello.ts", "export const hello = 1\n"))
  356. .at((ctx) => ({ path: `/find/symbol?${new URLSearchParams({ query: "hello" })}`, headers: ctx.headers() }))
  357. .json(200, array),
  358. http.protected
  359. .get("/event", "event.stream")
  360. .stream()
  361. .status(
  362. 200,
  363. (_ctx, result) =>
  364. Effect.sync(() => {
  365. check(result.contentType.includes("text/event-stream"), "event should be an SSE stream")
  366. check(result.text.includes("server.connected"), "event should emit initial connection event")
  367. }),
  368. "status",
  369. ),
  370. http.protected.get("/mcp", "mcp.status").json(),
  371. http.protected
  372. .post("/mcp", "mcp.add")
  373. .mutating()
  374. .at((ctx) => ({
  375. path: "/mcp",
  376. headers: ctx.headers(),
  377. body: { name: "httpapi-disabled", config: { type: "local", command: ["bun", "--version"], enabled: false } },
  378. }))
  379. .json(
  380. 200,
  381. (body) => {
  382. object(body)
  383. object(body["httpapi-disabled"])
  384. check(body["httpapi-disabled"].status === "disabled", "disabled MCP server should be added without spawning")
  385. },
  386. "status",
  387. ),
  388. http.protected
  389. .post("/mcp", "mcp.add.invalid")
  390. .at((ctx) => ({
  391. path: "/mcp",
  392. headers: ctx.headers(),
  393. body: { name: "httpapi-invalid", config: { type: "invalid" } },
  394. }))
  395. .status(400),
  396. http.protected
  397. .post("/mcp/{name}/auth", "mcp.auth.start")
  398. .at((ctx) => ({ path: route("/mcp/{name}/auth", { name: "httpapi-missing" }), headers: ctx.headers() }))
  399. .json(404, object, "status"),
  400. http.protected
  401. .delete("/mcp/{name}/auth", "mcp.auth.remove")
  402. .mutating()
  403. .at((ctx) => ({ path: route("/mcp/{name}/auth", { name: "httpapi-missing" }), headers: ctx.headers() }))
  404. .json(404, object, "status"),
  405. http.protected
  406. .post("/mcp/{name}/auth/authenticate", "mcp.auth.authenticate")
  407. .at((ctx) => ({
  408. path: route("/mcp/{name}/auth/authenticate", { name: "httpapi-missing" }),
  409. headers: ctx.headers(),
  410. }))
  411. .json(404, object, "status"),
  412. http.protected
  413. .post("/mcp/{name}/auth/callback", "mcp.auth.callback")
  414. .at((ctx) => ({
  415. path: route("/mcp/{name}/auth/callback", { name: "httpapi-missing" }),
  416. headers: ctx.headers(),
  417. body: { code: "code" },
  418. }))
  419. .json(404, object, "status"),
  420. http.protected
  421. .post("/mcp/{name}/connect", "mcp.connect")
  422. .mutating()
  423. .at((ctx) => ({ path: route("/mcp/{name}/connect", { name: "httpapi-missing" }), headers: ctx.headers() }))
  424. .json(404, object, "status"),
  425. http.protected
  426. .post("/mcp/{name}/disconnect", "mcp.disconnect")
  427. .mutating()
  428. .at((ctx) => ({ path: route("/mcp/{name}/disconnect", { name: "httpapi-missing" }), headers: ctx.headers() }))
  429. .json(404, object, "status"),
  430. http.protected.get("/pty/shells", "pty.shells").json(200, array),
  431. http.protected.get("/pty", "pty.list").json(200, array),
  432. http.protected
  433. .post("/pty", "pty.create")
  434. .mutating()
  435. .at((ctx) => ({ path: "/pty", headers: ctx.headers(), body: controlledPtyInput("HTTP API PTY") }))
  436. .json(
  437. 200,
  438. (body, ctx) => {
  439. object(body)
  440. check(body.title === "HTTP API PTY", "PTY create should return requested title")
  441. check(body.command === "/bin/sh", "PTY create should use controlled shell command")
  442. check(body.cwd === ctx.directory, "PTY create should default cwd to scenario directory")
  443. },
  444. "status",
  445. ),
  446. http.protected
  447. .post("/pty", "pty.create.invalid")
  448. .at((ctx) => ({ path: "/pty", headers: ctx.headers(), body: { command: 1 } }))
  449. .status(400),
  450. http.protected
  451. .post("/pty/{ptyID}/connect-token", "pty.connectToken.invalid")
  452. .at((ctx) => ({
  453. path: route("/pty/{ptyID}/connect-token", { ptyID: "pty_httpapi_missing" }),
  454. headers: ctx.headers(),
  455. }))
  456. .status(403, undefined, "status"),
  457. http.protected
  458. .get("/pty/{ptyID}", "pty.get")
  459. .at((ctx) => ({ path: route("/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  460. .status(404),
  461. http.protected
  462. .put("/pty/{ptyID}", "pty.update")
  463. .mutating()
  464. .at((ctx) => ({
  465. path: route("/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }),
  466. headers: ctx.headers(),
  467. body: { size: { rows: 0, cols: 0 } },
  468. }))
  469. .status(400),
  470. http.protected
  471. .delete("/pty/{ptyID}", "pty.remove")
  472. .mutating()
  473. .at((ctx) => ({ path: route("/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  474. .json(404, object, "status"),
  475. http.protected
  476. .get("/pty/{ptyID}/connect", "pty.connect")
  477. .at((ctx) => ({ path: route("/pty/{ptyID}/connect", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  478. .status(404, undefined, "none"),
  479. http.protected.get("/experimental/console", "experimental.console.get").json(),
  480. http.protected.get("/experimental/console/orgs", "experimental.console.listOrgs").json(),
  481. http.protected
  482. .post("/experimental/console/switch", "experimental.console.switchOrg")
  483. .at((ctx) => ({
  484. path: "/experimental/console/switch",
  485. headers: ctx.headers(),
  486. body: { accountID: "httpapi-account", orgID: "httpapi-org" },
  487. }))
  488. .status(400, undefined, "none"),
  489. http.protected.get("/experimental/workspace/adapter", "experimental.workspace.adapter.list").json(200, array),
  490. http.protected.get("/experimental/workspace", "experimental.workspace.list").json(200, array),
  491. http.protected.get("/experimental/workspace/status", "experimental.workspace.status").json(200, array),
  492. http.protected
  493. .post("/experimental/workspace", "experimental.workspace.create")
  494. .at((ctx) => ({ path: "/experimental/workspace", headers: ctx.headers(), body: {} }))
  495. .status(400),
  496. http.protected
  497. .post("/experimental/workspace/sync-list", "experimental.workspace.syncList")
  498. .status(204, undefined, "status"),
  499. http.protected
  500. .delete("/experimental/workspace/{id}", "experimental.workspace.remove")
  501. .mutating()
  502. .at((ctx) => ({
  503. path: route("/experimental/workspace/{id}", { id: "wrk_httpapi_missing" }),
  504. headers: ctx.headers(),
  505. }))
  506. .status(200),
  507. http.protected
  508. .post("/experimental/workspace/warp", "experimental.workspace.warp")
  509. .at((ctx) => ({
  510. path: "/experimental/workspace/warp",
  511. headers: ctx.headers(),
  512. body: {},
  513. }))
  514. .status(400),
  515. http.protected
  516. .post("/experimental/control-plane/move-session", "experimental.controlPlane.moveSession")
  517. .global()
  518. .at(() => ({
  519. path: "/experimental/control-plane/move-session",
  520. body: {},
  521. }))
  522. .status(400),
  523. http.protected
  524. .get("/experimental/tool", "tool.list")
  525. .at((ctx) => ({
  526. path: `/experimental/tool?${new URLSearchParams({ provider: "opencode", model: "test" })}`,
  527. headers: ctx.headers(),
  528. }))
  529. .json(200, array, "status"),
  530. http.protected.get("/experimental/tool/ids", "tool.ids").json(200, array),
  531. http.protected.get("/experimental/worktree", "worktree.list").json(200, array),
  532. http.protected
  533. .post("/experimental/worktree", "worktree.create")
  534. .mutating()
  535. .at((ctx) => ({ path: "/experimental/worktree", headers: ctx.headers(), body: { name: "api-dsl" } }))
  536. .jsonEffect(
  537. 200,
  538. (body, ctx) =>
  539. Effect.gen(function* () {
  540. object(body)
  541. check(typeof body.directory === "string", "created worktree should include directory")
  542. yield* ctx.worktreeRemove(body.directory)
  543. }),
  544. "status",
  545. ),
  546. http.protected
  547. .post("/experimental/worktree", "worktree.create.invalid")
  548. .at((ctx) => ({ path: "/experimental/worktree", headers: ctx.headers(), body: { name: 1 } }))
  549. .status(400),
  550. http.protected
  551. .delete("/experimental/worktree", "worktree.remove")
  552. .mutating()
  553. .seeded((ctx) => ctx.worktree({ name: "api-remove" }))
  554. .at((ctx) => ({ path: "/experimental/worktree", headers: ctx.headers(), body: { directory: ctx.state.directory } }))
  555. .json(200, (body) => {
  556. check(body === true, "worktree remove should return true")
  557. }),
  558. http.protected
  559. .post("/experimental/worktree/reset", "worktree.reset")
  560. .mutating()
  561. .seeded((ctx) => ctx.worktree({ name: "api-reset" }))
  562. .at((ctx) => ({
  563. path: "/experimental/worktree/reset",
  564. headers: ctx.headers(),
  565. body: { directory: ctx.state.directory },
  566. }))
  567. .jsonEffect(200, (body, ctx) =>
  568. Effect.gen(function* () {
  569. check(body === true, "worktree reset should return true")
  570. yield* ctx.worktreeRemove(ctx.state.directory)
  571. }),
  572. ),
  573. http.protected
  574. .get("/experimental/session", "experimental.session.list")
  575. .at((ctx) => ({ path: "/experimental/session?roots=false&archived=false", headers: ctx.headers() }))
  576. .json(200, array),
  577. http.protected.get("/experimental/capabilities", "experimental.capabilities.get").json(200, (body) => {
  578. check(typeof body === "object" && body !== null, "capabilities should be an object")
  579. check("backgroundSubagents" in body, "capabilities should report background subagents")
  580. }),
  581. http.protected
  582. .post("/experimental/session/{sessionID}/background", "experimental.session.background")
  583. .mutating()
  584. .seeded((ctx) => ctx.session({ title: "Background route owner" }))
  585. .at((ctx) => ({
  586. path: route("/experimental/session/{sessionID}/background", { sessionID: ctx.state.id }),
  587. headers: ctx.headers(),
  588. }))
  589. .json(200, (body) => {
  590. check(body === false, "background route should be a no-op without running subagents")
  591. }),
  592. http.protected.get("/experimental/resource", "experimental.resource.list").json(),
  593. http.protected
  594. .post("/sync/history", "sync.history.list")
  595. .at((ctx) => ({ path: "/sync/history", headers: ctx.headers(), body: {} }))
  596. .json(200, array),
  597. http.protected
  598. .post("/sync/replay", "sync.replay")
  599. .at((ctx) => ({ path: "/sync/replay", headers: ctx.headers(), body: { directory: ctx.directory, events: [] } }))
  600. .status(400),
  601. http.protected
  602. .post("/sync/steal", "sync.steal.invalid")
  603. .at((ctx) => ({ path: "/sync/steal", headers: ctx.headers(), body: {} }))
  604. .status(400, undefined, "status"),
  605. http.protected
  606. .post("/sync/start", "sync.start")
  607. .mutating()
  608. .preserveDatabase()
  609. .json(200, (body) => {
  610. check(body === true, "sync start should return true when no workspace sessions exist")
  611. }),
  612. http.protected
  613. .post("/instance/dispose", "instance.dispose")
  614. .mutating()
  615. .json(200, (body) => {
  616. check(body === true, "instance dispose should return true")
  617. }),
  618. http.protected
  619. .post("/log", "app.log")
  620. .global()
  621. .at(() => ({ path: "/log", body: { service: "httpapi-exercise", level: "info", message: "route coverage" } }))
  622. .json(200, (body) => {
  623. check(body === true, "log route should return true")
  624. }),
  625. http.protected
  626. .put("/auth/{providerID}", "auth.set")
  627. .global()
  628. .at(() => ({ path: route("/auth/{providerID}", { providerID: "test" }), body: { type: "api", key: "test-key" } }))
  629. .jsonEffect(200, (body) =>
  630. Effect.gen(function* () {
  631. check(body === true, "auth set should return true")
  632. const auth = yield* Effect.promise(() => Bun.file(path.join(exerciseDataDirectory, "auth.json")).json())
  633. object(auth)
  634. check(isRecord(auth.test) && auth.test.key === "test-key", "auth set should write isolated auth file")
  635. }),
  636. ),
  637. http.protected
  638. .delete("/auth/{providerID}", "auth.remove")
  639. .global()
  640. .seeded(() =>
  641. Effect.promise(() =>
  642. Bun.write(
  643. path.join(exerciseDataDirectory, "auth.json"),
  644. JSON.stringify({ test: { type: "api", key: "remove-me" } }),
  645. ),
  646. ),
  647. )
  648. .at(() => ({ path: route("/auth/{providerID}", { providerID: "test" }) }))
  649. .jsonEffect(200, (body) =>
  650. Effect.gen(function* () {
  651. check(body === true, "auth remove should return true")
  652. const auth = yield* Effect.promise(() => Bun.file(path.join(exerciseDataDirectory, "auth.json")).json())
  653. object(auth)
  654. check(auth.test === undefined, "auth remove should delete provider from isolated auth file")
  655. }),
  656. ),
  657. http.protected.get("/api/health", "v2.health.get").json(200, (body) => {
  658. object(body)
  659. check(body.healthy === true, "v2 server should report healthy")
  660. }),
  661. http.protected.get("/api/location", "v2.location.get").json(200, object),
  662. http.protected.get("/api/agent", "v2.agent.list").json(200, locationData(array)),
  663. http.protected.get("/api/model", "v2.model.list").json(200, locationData(array)),
  664. http.protected.get("/api/provider", "v2.provider.list").json(200, locationData(array)),
  665. http.protected.get("/api/integration", "v2.integration.list").json(200, locationData(array)),
  666. http.protected
  667. .get("/api/integration/{integrationID}", "v2.integration.get")
  668. .at((ctx) => ({
  669. path: route("/api/integration/{integrationID}", { integrationID: "missing" }),
  670. headers: ctx.headers(),
  671. }))
  672. .json(200, object),
  673. http.protected
  674. .post("/api/integration/{integrationID}/connect/key", "v2.integration.connect.key")
  675. .at((ctx) => ({
  676. path: route("/api/integration/{integrationID}/connect/key", { integrationID: "missing" }),
  677. headers: ctx.headers(),
  678. body: { key: "test" },
  679. }))
  680. .status(500, undefined, "status"),
  681. http.protected
  682. .post("/api/integration/{integrationID}/connect/oauth", "v2.integration.connect.oauth")
  683. .at((ctx) => ({
  684. path: route("/api/integration/{integrationID}/connect/oauth", { integrationID: "missing" }),
  685. headers: ctx.headers(),
  686. body: { methodID: "missing", inputs: {} },
  687. }))
  688. .status(500, undefined, "status"),
  689. http.protected
  690. .get("/api/integration/attempt/{attemptID}", "v2.integration.attempt.status")
  691. .at((ctx) => ({
  692. path: route("/api/integration/attempt/{attemptID}", { attemptID: "con_missing" }),
  693. headers: ctx.headers(),
  694. }))
  695. .status(500, undefined, "status"),
  696. http.protected
  697. .post("/api/integration/attempt/{attemptID}/complete", "v2.integration.attempt.complete")
  698. .at((ctx) => ({
  699. path: route("/api/integration/attempt/{attemptID}/complete", { attemptID: "con_missing" }),
  700. headers: ctx.headers(),
  701. body: {},
  702. }))
  703. .status(500, undefined, "status"),
  704. http.protected
  705. .delete("/api/integration/attempt/{attemptID}", "v2.integration.attempt.cancel")
  706. .at((ctx) => ({
  707. path: route("/api/integration/attempt/{attemptID}", { attemptID: "con_missing" }),
  708. headers: ctx.headers(),
  709. }))
  710. .status(204, undefined, "status"),
  711. http.protected
  712. .delete("/api/credential/{credentialID}", "v2.credential.remove")
  713. .at((ctx) => ({
  714. path: route("/api/credential/{credentialID}", { credentialID: "cred_missing" }),
  715. headers: ctx.headers(),
  716. }))
  717. .status(204, undefined, "status"),
  718. http.protected
  719. .patch("/api/credential/{credentialID}", "v2.credential.update")
  720. .at((ctx) => ({
  721. path: route("/api/credential/{credentialID}", { credentialID: "cred_missing" }),
  722. headers: ctx.headers(),
  723. body: { label: "Work" },
  724. }))
  725. .status(204, undefined, "status"),
  726. http.protected.get("/api/command", "v2.command.list").json(200, locationData(array)),
  727. http.protected.get("/api/skill", "v2.skill.list").json(200, locationData(array)),
  728. http.protected
  729. .get("/api/event", "v2.event.subscribe")
  730. .stream()
  731. .status(
  732. 200,
  733. (_ctx, result) =>
  734. Effect.sync(() => {
  735. check(result.contentType.includes("text/event-stream"), "v2 event should be an SSE stream")
  736. check(result.text.includes("server.connected"), "v2 event should emit initial connection event")
  737. check(!result.text.includes('"location"'), "v2 connection event should not be scoped to a location")
  738. }),
  739. "status",
  740. ),
  741. http.protected
  742. .get("/api/fs/read/*", "v2.fs.read")
  743. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  744. .at((ctx) => ({ path: "/api/fs/read/hello.txt", headers: ctx.headers() }))
  745. .status(
  746. 200,
  747. (_ctx, result) =>
  748. Effect.sync(() => {
  749. check(result.text === "hello\n", "v2 fs read should return the file body")
  750. check(result.contentType.includes("text/plain"), "v2 fs read should return the file content type")
  751. }),
  752. "status",
  753. ),
  754. http.protected.get("/api/fs/list", "v2.fs.list").json(200, locationData(array)),
  755. http.protected
  756. .get("/api/fs/find", "v2.fs.find")
  757. .seeded((ctx) => ctx.file("hello.txt", "hello\n"))
  758. .at((ctx) => ({ path: "/api/fs/find?query=hello&type=file", headers: ctx.headers() }))
  759. .json(200, locationData(array)),
  760. http.protected.get("/api/pty", "v2.pty.list").json(200, locationData(array)),
  761. http.protected
  762. .post("/api/pty", "v2.pty.create")
  763. .mutating()
  764. .at((ctx) => ({ path: "/api/pty", headers: ctx.headers(), body: controlledPtyInput("HTTP API V2 PTY") }))
  765. .json(200, locationData(object)),
  766. http.protected
  767. .get("/api/pty/{ptyID}", "v2.pty.get")
  768. .at((ctx) => ({ path: route("/api/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  769. .json(404, object, "status"),
  770. http.protected
  771. .put("/api/pty/{ptyID}", "v2.pty.update")
  772. .mutating()
  773. .at((ctx) => ({
  774. path: route("/api/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }),
  775. headers: ctx.headers(),
  776. body: { title: "missing" },
  777. }))
  778. .json(404, object, "status"),
  779. http.protected
  780. .delete("/api/pty/{ptyID}", "v2.pty.remove")
  781. .mutating()
  782. .at((ctx) => ({ path: route("/api/pty/{ptyID}", { ptyID: "pty_httpapi_missing" }), headers: ctx.headers() }))
  783. .json(404, object, "status"),
  784. http.protected
  785. .post("/api/pty/{ptyID}/connect-token", "v2.pty.connectToken")
  786. .at((ctx) => ({
  787. path: route("/api/pty/{ptyID}/connect-token", { ptyID: "pty_httpapi_missing" }),
  788. headers: { ...ctx.headers(), "x-opencode-ticket": "1" },
  789. }))
  790. .json(404, object, "status"),
  791. http.protected
  792. .get("/api/pty/{ptyID}/connect", "v2.pty.connect")
  793. .at((ctx) => ({
  794. path: route("/api/pty/{ptyID}/connect", { ptyID: "pty_httpapi_missing" }),
  795. headers: ctx.headers(),
  796. }))
  797. .status(404, undefined, "none"),
  798. http.protected.get("/api/reference", "v2.reference.list").json(200, object),
  799. http.protected
  800. .get("/api/provider/{providerID}", "v2.provider.get")
  801. .at((ctx) => ({ path: route("/api/provider/{providerID}", { providerID: "missing" }), headers: ctx.headers() }))
  802. .json(404, object, "status"),
  803. http.protected.get("/api/permission/request", "v2.permission.request.list").json(200, (body) => {
  804. object(body)
  805. object(body.location)
  806. array(body.data)
  807. }),
  808. http.protected.get("/api/question/request", "v2.question.request.list").json(200, (body) => {
  809. object(body)
  810. object(body.location)
  811. array(body.data)
  812. }),
  813. http.protected
  814. .get("/api/session/{sessionID}/permission", "v2.session.permission.list")
  815. .seeded((ctx) => ctx.session({ title: "Permission list owner" }))
  816. .at((ctx) => ({
  817. path: route("/api/session/{sessionID}/permission", { sessionID: ctx.state.id }),
  818. headers: ctx.headers(),
  819. }))
  820. .json(200, data(array)),
  821. http.protected
  822. .get("/api/session/{sessionID}/question", "v2.session.question.list")
  823. .seeded((ctx) => ctx.session({ title: "Question list owner" }))
  824. .at((ctx) => ({
  825. path: route("/api/session/{sessionID}/question", { sessionID: ctx.state.id }),
  826. headers: ctx.headers(),
  827. }))
  828. .json(200, data(array)),
  829. http.protected
  830. .post("/api/session/{sessionID}/permission/{requestID}/reply", "v2.session.permission.reply")
  831. .seeded((ctx) => ctx.session({ title: "Permission owner" }))
  832. .at((ctx) => ({
  833. path: route("/api/session/{sessionID}/permission/{requestID}/reply", {
  834. sessionID: ctx.state.id,
  835. requestID: "per_httpapi_missing",
  836. }),
  837. headers: ctx.headers(),
  838. body: { reply: "once" },
  839. }))
  840. .json(404, object, "status"),
  841. http.protected
  842. .post("/api/session/{sessionID}/question/{requestID}/reply", "v2.session.question.reply")
  843. .seeded((ctx) => ctx.session({ title: "Question reply owner" }))
  844. .at((ctx) => ({
  845. path: route("/api/session/{sessionID}/question/{requestID}/reply", {
  846. sessionID: ctx.state.id,
  847. requestID: "que_httpapi_missing",
  848. }),
  849. headers: ctx.headers(),
  850. body: { answers: [] },
  851. }))
  852. .json(404, object, "status"),
  853. http.protected
  854. .post("/api/session/{sessionID}/question/{requestID}/reject", "v2.session.question.reject")
  855. .seeded((ctx) => ctx.session({ title: "Question reject owner" }))
  856. .at((ctx) => ({
  857. path: route("/api/session/{sessionID}/question/{requestID}/reject", {
  858. sessionID: ctx.state.id,
  859. requestID: "que_httpapi_missing",
  860. }),
  861. headers: ctx.headers(),
  862. }))
  863. .json(404, object, "status"),
  864. http.protected.get("/api/permission/saved", "v2.permission.saved.list").json(200, (body) => {
  865. object(body)
  866. array(body.data)
  867. }),
  868. http.protected
  869. .delete("/api/permission/saved/{id}", "v2.permission.saved.remove")
  870. .at((ctx) => ({ path: route("/api/permission/saved/{id}", { id: "psv_httpapi_missing" }), headers: ctx.headers() }))
  871. .status(204, undefined, "status"),
  872. http.protected
  873. .get("/api/session", "v2.session.list")
  874. .at((ctx) => ({ path: "/api/session?roots=true", headers: ctx.headers() }))
  875. .json(
  876. 200,
  877. (body) => {
  878. object(body)
  879. array(body.data)
  880. object(body.cursor)
  881. },
  882. "none",
  883. ),
  884. http.protected
  885. .get("/api/session", "v2.session.list.filters")
  886. .at((ctx) => ({
  887. path: `/api/session?${new URLSearchParams({
  888. limit: "2",
  889. order: "asc",
  890. path: ".",
  891. roots: "false",
  892. start: "0",
  893. search: "missing",
  894. directory: ctx.directory ?? "",
  895. })}`,
  896. headers: ctx.headers(),
  897. }))
  898. .json(
  899. 200,
  900. (body) => {
  901. object(body)
  902. array(body.data)
  903. object(body.cursor)
  904. },
  905. "none",
  906. ),
  907. http.protected
  908. .get("/api/session", "v2.session.list.cursor")
  909. .at((ctx) => ({
  910. path: `/api/session?${new URLSearchParams({
  911. limit: "2",
  912. cursor: cursor({
  913. order: "desc",
  914. directory: ctx.directory,
  915. anchor: { id: "ses_httpapi_missing", time: 0, direction: "next" },
  916. }),
  917. })}`,
  918. headers: ctx.headers(),
  919. }))
  920. .json(
  921. 200,
  922. (body) => {
  923. object(body)
  924. array(body.data)
  925. object(body.cursor)
  926. },
  927. "none",
  928. ),
  929. http.protected
  930. .get("/api/session", "v2.session.list.cursor.invalid")
  931. .at((ctx) => ({
  932. path: `/api/session?${new URLSearchParams({
  933. cursor: "invalid",
  934. })}`,
  935. headers: ctx.headers(),
  936. }))
  937. .status(400, undefined, "none"),
  938. http.protected
  939. .post("/api/session", "v2.session.create")
  940. .at((ctx) => ({
  941. path: "/api/session",
  942. headers: { ...ctx.headers(), "content-type": "application/json" },
  943. body: {},
  944. }))
  945. .json(200, data(object)),
  946. http.protected
  947. .get("/api/session/{sessionID}", "v2.session.get")
  948. .seeded((ctx) => ctx.session({ title: "Session get" }))
  949. .at((ctx) => ({
  950. path: route("/api/session/{sessionID}", { sessionID: ctx.state.id }),
  951. headers: ctx.headers(),
  952. }))
  953. .json(200, data(object)),
  954. http.protected
  955. .post("/api/session/{sessionID}/agent", "v2.session.switchAgent")
  956. .seeded((ctx) => ctx.session({ title: "Switch agent" }))
  957. .at((ctx) => ({
  958. path: route("/api/session/{sessionID}/agent", { sessionID: ctx.state.id }),
  959. headers: { ...ctx.headers(), "content-type": "application/json" },
  960. body: { agent: "plan" },
  961. }))
  962. .status(204, undefined, "none"),
  963. http.protected
  964. .post("/api/session/{sessionID}/model", "v2.session.switchModel")
  965. .seeded((ctx) => ctx.session({ title: "Switch model" }))
  966. .at((ctx) => ({
  967. path: route("/api/session/{sessionID}/model", { sessionID: ctx.state.id }),
  968. headers: { ...ctx.headers(), "content-type": "application/json" },
  969. body: { model: { providerID: "opencode", id: "big-pickle" } },
  970. }))
  971. .status(204, undefined, "none"),
  972. http.protected
  973. .get("/api/session/{sessionID}/context", "v2.session.context")
  974. .at((ctx) => ({
  975. path: route("/api/session/{sessionID}/context", { sessionID: "ses_httpapi_missing" }),
  976. headers: ctx.headers(),
  977. }))
  978. .json(404, object, "status"),
  979. http.protected
  980. .get("/api/session/{sessionID}/message", "v2.session.messages")
  981. .at((ctx) => ({
  982. path: route("/api/session/{sessionID}/message", { sessionID: "ses_httpapi_missing" }),
  983. headers: ctx.headers(),
  984. }))
  985. .json(404, object, "status"),
  986. http.protected
  987. .get("/api/session/{sessionID}/message", "v2.session.messages.params")
  988. .at((ctx) => ({
  989. path: `${route("/api/session/{sessionID}/message", { sessionID: "ses_httpapi_missing" })}?${new URLSearchParams({
  990. limit: "2",
  991. order: "asc",
  992. })}`,
  993. headers: ctx.headers(),
  994. }))
  995. .json(404, object, "status"),
  996. http.protected
  997. .get("/api/session/{sessionID}/message", "v2.session.messages.cursor")
  998. .at((ctx) => ({
  999. path: `${route("/api/session/{sessionID}/message", { sessionID: "ses_httpapi_missing" })}?${new URLSearchParams({
  1000. limit: "2",
  1001. directory: ctx.directory ?? "",
  1002. cursor: cursor({ id: "msg_httpapi_missing", time: 0, order: "desc", direction: "next" }),
  1003. })}`,
  1004. headers: ctx.headers(),
  1005. }))
  1006. .json(404, object, "status"),
  1007. http.protected
  1008. .get("/api/session/{sessionID}/message", "v2.session.messages.cursor.invalid")
  1009. .seeded((ctx) => ctx.session({ title: "Invalid message cursor owner" }))
  1010. .at((ctx) => ({
  1011. path: `${route("/api/session/{sessionID}/message", { sessionID: ctx.state.id })}?${new URLSearchParams({
  1012. cursor: cursor({ id: "msg_httpapi_missing", time: 0, order: "desc", direction: "next" }),
  1013. order: "asc",
  1014. })}`,
  1015. headers: ctx.headers(),
  1016. }))
  1017. .status(400, undefined, "none"),
  1018. http.protected
  1019. .post("/api/session/{sessionID}/prompt", "v2.session.prompt.invalid")
  1020. .seeded((ctx) => ctx.session({ title: "Invalid prompt owner" }))
  1021. .at((ctx) => ({
  1022. path: route("/api/session/{sessionID}/prompt", { sessionID: ctx.state.id }),
  1023. headers: ctx.headers(),
  1024. body: {},
  1025. }))
  1026. .status(400, undefined, "none"),
  1027. http.protected
  1028. .post("/api/session/{sessionID}/compact", "v2.session.compact")
  1029. .at((ctx) => ({
  1030. path: route("/api/session/{sessionID}/compact", { sessionID: "ses_httpapi_missing" }),
  1031. headers: ctx.headers(),
  1032. }))
  1033. .status(404, undefined, "status"),
  1034. http.protected
  1035. .post("/api/session/{sessionID}/wait", "v2.session.wait")
  1036. .at((ctx) => ({
  1037. path: route("/api/session/{sessionID}/wait", { sessionID: "ses_httpapi_missing" }),
  1038. headers: ctx.headers(),
  1039. }))
  1040. .status(404, undefined, "status"),
  1041. http.protected
  1042. .get("/session", "session.list")
  1043. .seeded((ctx) => ctx.session({ title: "List me" }))
  1044. .at((ctx) => ({ path: "/session?roots=true", headers: ctx.headers() }))
  1045. .json(200, (body, ctx) => {
  1046. array(body)
  1047. check(
  1048. body.some((item) => isRecord(item) && item.id === ctx.state.id && item.title === "List me"),
  1049. "seeded session should be listed",
  1050. )
  1051. }),
  1052. http.protected
  1053. .get("/session/status", "session.status")
  1054. .seeded((ctx) => ctx.session({ title: "Status session" }))
  1055. .json(200, object),
  1056. http.protected
  1057. .post("/session", "session.create")
  1058. .mutating()
  1059. .at((ctx) => ({ path: "/session", headers: ctx.headers(), body: { title: "Created session" } }))
  1060. .json(
  1061. 200,
  1062. (body, ctx) => {
  1063. object(body)
  1064. check(body.title === "Created session", "created session should use requested title")
  1065. check(body.directory === ctx.directory, "created session should use scenario directory")
  1066. },
  1067. "status",
  1068. ),
  1069. http.protected
  1070. .get("/session/{sessionID}", "session.get")
  1071. .seeded((ctx) => ctx.session({ title: "Get me" }))
  1072. .at((ctx) => ({ path: route("/session/{sessionID}", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1073. .json(200, (body, ctx) => {
  1074. object(body)
  1075. check(body.id === ctx.state.id, "should return requested session")
  1076. check(body.title === "Get me", "should preserve seeded title")
  1077. }),
  1078. http.protected
  1079. .get("/session/{sessionID}", "session.get.missing")
  1080. .at((ctx) => ({
  1081. path: route("/session/{sessionID}", { sessionID: "ses_httpapi_missing" }),
  1082. headers: ctx.headers(),
  1083. }))
  1084. .status(404),
  1085. http.protected
  1086. .patch("/session/{sessionID}", "session.update")
  1087. .mutating()
  1088. .seeded((ctx) => ctx.session({ title: "Before rename" }))
  1089. .at((ctx) => ({
  1090. path: route("/session/{sessionID}", { sessionID: ctx.state.id }),
  1091. headers: ctx.headers(),
  1092. body: { title: "After rename" },
  1093. }))
  1094. .json(
  1095. 200,
  1096. (body) => {
  1097. object(body)
  1098. check(body.title === "After rename", "updated session should use new title")
  1099. },
  1100. "status",
  1101. ),
  1102. http.protected
  1103. .patch("/session/{sessionID}", "session.update.invalid")
  1104. .mutating()
  1105. .at((ctx) => ({
  1106. path: route("/session/{sessionID}", { sessionID: "ses_httpapi_missing" }),
  1107. headers: ctx.headers(),
  1108. body: { title: 1 },
  1109. }))
  1110. .status(400),
  1111. http.protected
  1112. .delete("/session/{sessionID}", "session.delete")
  1113. .mutating()
  1114. .seeded((ctx) => ctx.session({ title: "Delete me" }))
  1115. .at((ctx) => ({ path: route("/session/{sessionID}", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1116. .jsonEffect(200, (body, ctx) =>
  1117. Effect.gen(function* () {
  1118. check(body === true, "delete should return true")
  1119. check((yield* ctx.sessionGet(ctx.state.id)) === undefined, "deleted session should not remain in storage")
  1120. }),
  1121. ),
  1122. http.protected
  1123. .get("/session/{sessionID}/children", "session.children")
  1124. .seeded((ctx) =>
  1125. Effect.gen(function* () {
  1126. const parent = yield* ctx.session({ title: "Parent" })
  1127. const child = yield* ctx.session({ title: "Child", parentID: parent.id })
  1128. return { parent, child }
  1129. }),
  1130. )
  1131. .at((ctx) => ({
  1132. path: route("/session/{sessionID}/children", { sessionID: ctx.state.parent.id }),
  1133. headers: ctx.headers(),
  1134. }))
  1135. .json(200, (body, ctx) => {
  1136. array(body)
  1137. check(
  1138. body.some((item) => isRecord(item) && item.id === ctx.state.child.id && item.parentID === ctx.state.parent.id),
  1139. "children should include seeded child",
  1140. )
  1141. }),
  1142. http.protected
  1143. .get("/session/{sessionID}/todo", "session.todo")
  1144. .seeded((ctx) =>
  1145. Effect.gen(function* () {
  1146. const session = yield* ctx.session({ title: "Todo session" })
  1147. const todos = [{ content: "cover session todo", status: "pending", priority: "high" }]
  1148. yield* ctx.todos(session.id, todos)
  1149. return { session, todos }
  1150. }),
  1151. )
  1152. .at((ctx) => ({
  1153. path: route("/session/{sessionID}/todo", { sessionID: ctx.state.session.id }),
  1154. headers: ctx.headers(),
  1155. }))
  1156. .json(200, (body, ctx) => {
  1157. check(stable(body) === stable(ctx.state.todos), "todos should match seeded state")
  1158. }),
  1159. http.protected
  1160. .get("/session/{sessionID}/diff", "session.diff")
  1161. .seeded((ctx) => ctx.session({ title: "Diff session" }))
  1162. .at((ctx) => ({ path: route("/session/{sessionID}/diff", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1163. .json(200, array),
  1164. http.protected
  1165. .get("/session/{sessionID}/message", "session.messages")
  1166. .seeded((ctx) => ctx.session({ title: "Messages session" }))
  1167. .at((ctx) => ({ path: route("/session/{sessionID}/message", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1168. .json(200, (body) => {
  1169. array(body)
  1170. check(body.length === 0, "new session should have no messages")
  1171. }),
  1172. http.protected
  1173. .get("/session/{sessionID}/message/{messageID}", "session.message")
  1174. .seeded((ctx) =>
  1175. Effect.gen(function* () {
  1176. const session = yield* ctx.session({ title: "Message get session" })
  1177. const message = yield* ctx.message(session.id, { text: "read me" })
  1178. return { session, message }
  1179. }),
  1180. )
  1181. .at((ctx) => ({
  1182. path: route("/session/{sessionID}/message/{messageID}", {
  1183. sessionID: ctx.state.session.id,
  1184. messageID: ctx.state.message.info.id,
  1185. }),
  1186. headers: ctx.headers(),
  1187. }))
  1188. .json(200, (body, ctx) => {
  1189. object(body)
  1190. check(isRecord(body.info) && body.info.id === ctx.state.message.info.id, "should return requested message")
  1191. check(
  1192. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.id === ctx.state.message.part.id),
  1193. "message should include seeded part",
  1194. )
  1195. }),
  1196. http.protected
  1197. .patch("/session/{sessionID}/message/{messageID}/part/{partID}", "part.update")
  1198. .mutating()
  1199. .seeded((ctx) =>
  1200. Effect.gen(function* () {
  1201. const session = yield* ctx.session({ title: "Part update session" })
  1202. const message = yield* ctx.message(session.id, { text: "before" })
  1203. return { session, message }
  1204. }),
  1205. )
  1206. .at((ctx) => ({
  1207. path: route("/session/{sessionID}/message/{messageID}/part/{partID}", {
  1208. sessionID: ctx.state.session.id,
  1209. messageID: ctx.state.message.info.id,
  1210. partID: ctx.state.message.part.id,
  1211. }),
  1212. headers: ctx.headers(),
  1213. body: { ...ctx.state.message.part, text: "after" },
  1214. }))
  1215. .json(
  1216. 200,
  1217. (body) => {
  1218. object(body)
  1219. check(body.type === "text" && body.text === "after", "updated part should be returned")
  1220. },
  1221. "status",
  1222. ),
  1223. http.protected
  1224. .delete("/session/{sessionID}/message/{messageID}/part/{partID}", "part.delete")
  1225. .mutating()
  1226. .seeded((ctx) =>
  1227. Effect.gen(function* () {
  1228. const session = yield* ctx.session({ title: "Part delete session" })
  1229. const message = yield* ctx.message(session.id, { text: "delete part" })
  1230. return { session, message }
  1231. }),
  1232. )
  1233. .at((ctx) => ({
  1234. path: route("/session/{sessionID}/message/{messageID}/part/{partID}", {
  1235. sessionID: ctx.state.session.id,
  1236. messageID: ctx.state.message.info.id,
  1237. partID: ctx.state.message.part.id,
  1238. }),
  1239. headers: ctx.headers(),
  1240. }))
  1241. .jsonEffect(200, (body, ctx) =>
  1242. Effect.gen(function* () {
  1243. check(body === true, "delete part should return true")
  1244. const messages = yield* ctx.messages(ctx.state.session.id)
  1245. check(messages[0]?.parts.length === 0, "deleted part should not remain on message")
  1246. }),
  1247. ),
  1248. http.protected
  1249. .delete("/session/{sessionID}/message/{messageID}", "session.deleteMessage")
  1250. .mutating()
  1251. .seeded((ctx) =>
  1252. Effect.gen(function* () {
  1253. const session = yield* ctx.session({ title: "Message delete session" })
  1254. const message = yield* ctx.message(session.id, { text: "delete message" })
  1255. return { session, message }
  1256. }),
  1257. )
  1258. .at((ctx) => ({
  1259. path: route("/session/{sessionID}/message/{messageID}", {
  1260. sessionID: ctx.state.session.id,
  1261. messageID: ctx.state.message.info.id,
  1262. }),
  1263. headers: ctx.headers(),
  1264. }))
  1265. .jsonEffect(200, (body, ctx) =>
  1266. Effect.gen(function* () {
  1267. check(body === true, "delete message should return true")
  1268. check((yield* ctx.messages(ctx.state.session.id)).length === 0, "deleted message should not remain")
  1269. }),
  1270. ),
  1271. http.protected
  1272. .post("/session/{sessionID}/fork", "session.fork")
  1273. .mutating()
  1274. .seeded((ctx) => ctx.session({ title: "Fork source" }))
  1275. .at((ctx) => ({
  1276. path: route("/session/{sessionID}/fork", { sessionID: ctx.state.id }),
  1277. headers: ctx.headers(),
  1278. body: {},
  1279. }))
  1280. .json(
  1281. 200,
  1282. (body) => {
  1283. object(body)
  1284. check(typeof body.id === "string", "fork should return a session")
  1285. },
  1286. "status",
  1287. ),
  1288. http.protected
  1289. .post("/session/{sessionID}/abort", "session.abort")
  1290. .mutating()
  1291. .seeded((ctx) => ctx.session({ title: "Abort session" }))
  1292. .at((ctx) => ({ path: route("/session/{sessionID}/abort", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1293. .json(200, (body) => {
  1294. check(body === true, "abort should return true")
  1295. }),
  1296. http.protected
  1297. .post("/session/{sessionID}/abort", "session.abort.missing")
  1298. .at((ctx) => ({
  1299. path: route("/session/{sessionID}/abort", { sessionID: "ses_httpapi_missing" }),
  1300. headers: ctx.headers(),
  1301. }))
  1302. .json(200, (body) => {
  1303. check(body === true, "missing session abort should remain a no-op success")
  1304. }),
  1305. http.protected
  1306. .post("/session/{sessionID}/init", "session.init")
  1307. .preserveDatabase()
  1308. .withLlm()
  1309. .seeded((ctx) =>
  1310. Effect.gen(function* () {
  1311. const session = yield* ctx.session({ title: "Init session" })
  1312. const message = yield* ctx.message(session.id, { text: "initialize" })
  1313. yield* ctx.llmText("initialized")
  1314. yield* ctx.llmText("initialized")
  1315. return { session, message }
  1316. }),
  1317. )
  1318. .at((ctx) => ({
  1319. path: route("/session/{sessionID}/init", { sessionID: ctx.state.session.id }),
  1320. headers: ctx.headers(),
  1321. body: { providerID: "test", modelID: "test-model", messageID: ctx.state.message.info.id },
  1322. }))
  1323. .jsonEffect(200, (body, ctx) =>
  1324. Effect.gen(function* () {
  1325. check(body === true, "init should return true")
  1326. yield* ctx.llmWait(1)
  1327. }),
  1328. ),
  1329. http.protected
  1330. .post("/session/{sessionID}/message", "session.prompt")
  1331. .preserveDatabase()
  1332. .withLlm()
  1333. .seeded((ctx) =>
  1334. Effect.gen(function* () {
  1335. const session = yield* ctx.session({ title: "LLM prompt session" })
  1336. yield* ctx.llmText("fake assistant")
  1337. yield* ctx.llmText("fake assistant")
  1338. return session
  1339. }),
  1340. )
  1341. .at((ctx) => ({
  1342. path: route("/session/{sessionID}/message", { sessionID: ctx.state.id }),
  1343. headers: ctx.headers(),
  1344. body: {
  1345. agent: "build",
  1346. model: { providerID: "test", modelID: "test-model" },
  1347. parts: [{ type: "text", text: "hello llm" }],
  1348. },
  1349. }))
  1350. .jsonEffect(
  1351. 200,
  1352. (body, ctx) =>
  1353. Effect.gen(function* () {
  1354. object(body)
  1355. check(isRecord(body.info) && body.info.role === "assistant", "prompt should return assistant message")
  1356. check(
  1357. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.text === "fake assistant"),
  1358. "assistant message should use fake LLM text",
  1359. )
  1360. yield* ctx.llmWait(1)
  1361. }),
  1362. "status",
  1363. ),
  1364. http.protected
  1365. .post("/session/{sessionID}/prompt_async", "session.prompt_async")
  1366. .preserveDatabase()
  1367. .withLlm()
  1368. .seeded((ctx) =>
  1369. Effect.gen(function* () {
  1370. const session = yield* ctx.session({ title: "Async prompt session" })
  1371. yield* ctx.llmText("fake async assistant")
  1372. yield* ctx.llmText("fake async assistant")
  1373. return session
  1374. }),
  1375. )
  1376. .at((ctx) => ({
  1377. path: route("/session/{sessionID}/prompt_async", { sessionID: ctx.state.id }),
  1378. headers: ctx.headers(),
  1379. body: {
  1380. agent: "build",
  1381. model: { providerID: "test", modelID: "test-model" },
  1382. parts: [{ type: "text", text: "hello async" }],
  1383. },
  1384. }))
  1385. .status(204, (ctx) =>
  1386. Effect.gen(function* () {
  1387. yield* ctx.llmWait(1)
  1388. }),
  1389. ),
  1390. http.protected
  1391. .post("/session/{sessionID}/command", "session.command")
  1392. .preserveDatabase()
  1393. .withLlm()
  1394. .seeded((ctx) =>
  1395. Effect.gen(function* () {
  1396. const session = yield* ctx.session({ title: "Command session" })
  1397. yield* ctx.llmText("command done")
  1398. yield* ctx.llmText("command done")
  1399. return session
  1400. }),
  1401. )
  1402. .at((ctx) => ({
  1403. path: route("/session/{sessionID}/command", { sessionID: ctx.state.id }),
  1404. headers: ctx.headers(),
  1405. body: { command: "init", arguments: "", model: "test/test-model" },
  1406. }))
  1407. .jsonEffect(
  1408. 200,
  1409. (body, ctx) =>
  1410. Effect.gen(function* () {
  1411. object(body)
  1412. check(isRecord(body.info) && body.info.role === "assistant", "command should return assistant message")
  1413. yield* ctx.llmWait(1)
  1414. }),
  1415. "status",
  1416. ),
  1417. http.protected
  1418. .post("/session/{sessionID}/shell", "session.shell")
  1419. .preserveDatabase()
  1420. .mutating()
  1421. .seeded((ctx) => ctx.session({ title: "Shell session" }))
  1422. .at((ctx) => ({
  1423. path: route("/session/{sessionID}/shell", { sessionID: ctx.state.id }),
  1424. headers: ctx.headers(),
  1425. body: { agent: "build", model: { providerID: "test", modelID: "test-model" }, command: "printf shell-ok" },
  1426. }))
  1427. .json(
  1428. 200,
  1429. (body) => {
  1430. object(body)
  1431. check(isRecord(body.info) && body.info.role === "assistant", "shell should return assistant message")
  1432. check(
  1433. Array.isArray(body.parts) && body.parts.some((part) => isRecord(part) && part.type === "tool"),
  1434. "shell should return a tool part",
  1435. )
  1436. },
  1437. "status",
  1438. ),
  1439. http.protected
  1440. .post("/session/{sessionID}/summarize", "session.summarize")
  1441. .preserveDatabase()
  1442. .withLlm()
  1443. .seeded((ctx) =>
  1444. Effect.gen(function* () {
  1445. const session = yield* ctx.session({ title: "Summarize session" })
  1446. yield* ctx.message(session.id, { text: "summarize this work" })
  1447. const summary = [
  1448. "## Goal",
  1449. "- Exercise session summarize.",
  1450. "",
  1451. "## Constraints & Preferences",
  1452. "- Use fake LLM.",
  1453. "",
  1454. "## Progress",
  1455. "### Done",
  1456. "- Summary generated.",
  1457. "",
  1458. "### In Progress",
  1459. "- (none)",
  1460. "",
  1461. "### Blocked",
  1462. "- (none)",
  1463. "",
  1464. "## Key Decisions",
  1465. "- Keep route local.",
  1466. "",
  1467. "## Next Steps",
  1468. "- (none)",
  1469. "",
  1470. "## Critical Context",
  1471. "- Test fixture.",
  1472. "",
  1473. "## Relevant Files",
  1474. "- test/server/httpapi-exercise/index.ts: scenario",
  1475. ].join("\n")
  1476. yield* ctx.llmText(summary)
  1477. yield* ctx.llmText(summary)
  1478. return session
  1479. }),
  1480. )
  1481. .at((ctx) => ({
  1482. path: route("/session/{sessionID}/summarize", { sessionID: ctx.state.id }),
  1483. headers: ctx.headers(),
  1484. body: { providerID: "test", modelID: "test-model", auto: false },
  1485. }))
  1486. .jsonEffect(
  1487. 200,
  1488. (body, ctx) =>
  1489. Effect.gen(function* () {
  1490. check(body === true, "summarize should return true")
  1491. const messages = yield* ctx.messages(ctx.state.id)
  1492. check(
  1493. messages.some((message) => message.info.role === "assistant" && message.info.summary === true),
  1494. "summarize should create a summary assistant message",
  1495. )
  1496. yield* ctx.llmWait(1)
  1497. }),
  1498. "status",
  1499. ),
  1500. http.protected
  1501. .post("/session/{sessionID}/revert", "session.revert")
  1502. .mutating()
  1503. .seeded((ctx) =>
  1504. Effect.gen(function* () {
  1505. const session = yield* ctx.session({ title: "Revert session" })
  1506. const message = yield* ctx.message(session.id, { text: "revert me" })
  1507. return { session, message }
  1508. }),
  1509. )
  1510. .at((ctx) => ({
  1511. path: route("/session/{sessionID}/revert", { sessionID: ctx.state.session.id }),
  1512. headers: ctx.headers(),
  1513. body: { messageID: ctx.state.message.info.id },
  1514. }))
  1515. .json(
  1516. 200,
  1517. (body, ctx) => {
  1518. object(body)
  1519. check(body.id === ctx.state.session.id, "revert should return the session")
  1520. check(
  1521. isRecord(body.revert) && body.revert.messageID === ctx.state.message.info.id,
  1522. "revert should record reverted message",
  1523. )
  1524. },
  1525. "status",
  1526. ),
  1527. http.protected
  1528. .post("/session/{sessionID}/unrevert", "session.unrevert")
  1529. .mutating()
  1530. .seeded((ctx) => ctx.session({ title: "Unrevert session" }))
  1531. .at((ctx) => ({
  1532. path: route("/session/{sessionID}/unrevert", { sessionID: ctx.state.id }),
  1533. headers: ctx.headers(),
  1534. }))
  1535. .json(
  1536. 200,
  1537. (body, ctx) => {
  1538. object(body)
  1539. check(body.id === ctx.state.id, "unrevert should return the session")
  1540. },
  1541. "status",
  1542. ),
  1543. http.protected
  1544. .post("/session/{sessionID}/permissions/{permissionID}", "permission.respond")
  1545. .seeded((ctx) => ctx.session({ title: "Deprecated permission session" }))
  1546. .at((ctx) => ({
  1547. path: route("/session/{sessionID}/permissions/{permissionID}", {
  1548. sessionID: ctx.state.id,
  1549. permissionID: "per_httpapi_deprecated",
  1550. }),
  1551. headers: ctx.headers(),
  1552. body: { response: "once" },
  1553. }))
  1554. .json(404, object, "status"),
  1555. http.protected
  1556. .post("/session/{sessionID}/share", "session.share")
  1557. .mutating()
  1558. .seeded((ctx) => ctx.session({ title: "Share session" }))
  1559. .at((ctx) => ({ path: route("/session/{sessionID}/share", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1560. .json(
  1561. 200,
  1562. (body, ctx) => {
  1563. object(body)
  1564. check(body.id === ctx.state.id, "share should return the session")
  1565. },
  1566. "status",
  1567. ),
  1568. http.protected
  1569. .delete("/session/{sessionID}/share", "session.unshare")
  1570. .mutating()
  1571. .seeded((ctx) => ctx.session({ title: "Unshare session" }))
  1572. .at((ctx) => ({ path: route("/session/{sessionID}/share", { sessionID: ctx.state.id }), headers: ctx.headers() }))
  1573. .json(
  1574. 200,
  1575. (body, ctx) => {
  1576. object(body)
  1577. check(body.id === ctx.state.id, "unshare should return the session")
  1578. },
  1579. "status",
  1580. ),
  1581. http.protected
  1582. .post("/tui/append-prompt", "tui.appendPrompt")
  1583. .at((ctx) => ({ path: "/tui/append-prompt", headers: ctx.headers(), body: { text: "hello" } }))
  1584. .json(200, boolean, "status"),
  1585. http.protected
  1586. .post("/tui/select-session", "tui.selectSession.invalid")
  1587. .at((ctx) => ({ path: "/tui/select-session", headers: ctx.headers(), body: { sessionID: "invalid" } }))
  1588. .status(400),
  1589. http.protected.post("/tui/open-help", "tui.openHelp").json(200, boolean, "status"),
  1590. http.protected.post("/tui/open-sessions", "tui.openSessions").json(200, boolean, "status"),
  1591. http.protected.post("/tui/open-themes", "tui.openThemes").json(200, boolean, "status"),
  1592. http.protected.post("/tui/open-models", "tui.openModels").json(200, boolean, "status"),
  1593. http.protected.post("/tui/submit-prompt", "tui.submitPrompt").json(200, boolean, "status"),
  1594. http.protected.post("/tui/clear-prompt", "tui.clearPrompt").json(200, boolean, "status"),
  1595. http.protected
  1596. .post("/tui/execute-command", "tui.executeCommand")
  1597. .at((ctx) => ({ path: "/tui/execute-command", headers: ctx.headers(), body: { command: "agent_cycle" } }))
  1598. .json(200, boolean, "status"),
  1599. http.protected
  1600. .post("/tui/show-toast", "tui.showToast")
  1601. .at((ctx) => ({
  1602. path: "/tui/show-toast",
  1603. headers: ctx.headers(),
  1604. body: { title: "Exercise", message: "covered", variant: "info", duration: 1000 },
  1605. }))
  1606. .json(200, boolean, "status"),
  1607. http.protected
  1608. .post("/tui/publish", "tui.publish")
  1609. .at((ctx) => ({
  1610. path: "/tui/publish",
  1611. headers: ctx.headers(),
  1612. body: { type: "tui.prompt.append", properties: { text: "published" } },
  1613. }))
  1614. .json(200, boolean, "status"),
  1615. http.protected
  1616. .post("/tui/select-session", "tui.selectSession")
  1617. .seeded((ctx) => ctx.session({ title: "TUI select" }))
  1618. .at((ctx) => ({ path: "/tui/select-session", headers: ctx.headers(), body: { sessionID: ctx.state.id } }))
  1619. .json(200, boolean, "status"),
  1620. http.protected
  1621. .post("/tui/control/response", "tui.control.response")
  1622. .at((ctx) => ({ path: "/tui/control/response", headers: ctx.headers(), body: { ok: true } }))
  1623. .json(200, boolean, "status"),
  1624. http.protected
  1625. .get("/tui/control/next", "tui.control.next")
  1626. .mutating()
  1627. .seeded((ctx) => ctx.tuiRequest({ path: "/tui/exercise", body: { text: "queued" } }))
  1628. .json(
  1629. 200,
  1630. (body) => {
  1631. object(body)
  1632. check(body.path === "/tui/exercise", "control next should return queued path")
  1633. object(body.body)
  1634. check(body.body.text === "queued", "control next should return queued body")
  1635. },
  1636. "status",
  1637. ),
  1638. http.protected
  1639. .post("/global/upgrade", "global.upgrade")
  1640. .global()
  1641. .probe({ path: "/global/upgrade", body: { target: 1 } })
  1642. .at(() => ({ path: "/global/upgrade", body: { target: 1 } }))
  1643. .status(400),
  1644. ]
  1645. const llmScenarios = new Set([
  1646. "session.init",
  1647. "session.prompt",
  1648. "session.prompt_async",
  1649. "session.command",
  1650. "session.summarize",
  1651. ])
  1652. const main = Effect.gen(function* () {
  1653. yield* Effect.addFinalizer(() => Effect.promise(() => disposeApps()).pipe(Effect.andThen(cleanupExercisePaths)))
  1654. const options = parseOptions(Bun.argv.slice(2))
  1655. const modules = yield* Effect.promise(() => runtime())
  1656. const effectRoutes = routeKeys(OpenApi.fromApi(modules.PublicApi))
  1657. const selected = selectedScenarios(options, scenarios)
  1658. const missing = effectRoutes.filter((route) => !scenarios.some((scenario) => route === routeKey(scenario)))
  1659. const extra = scenarios.filter((scenario) => !effectRoutes.includes(routeKey(scenario)))
  1660. for (const scenario of scenarios) {
  1661. if (scenario.kind === "active" && llmScenarios.has(scenario.name) && !scenario.project?.llm) {
  1662. return yield* Effect.fail(new Error(`${scenario.name} must use TestLLMServer via .withLlm()`))
  1663. }
  1664. }
  1665. printHeader(options, effectRoutes, selected, missing, extra, {
  1666. database: exerciseDatabasePath,
  1667. global: exerciseGlobalRoot,
  1668. })
  1669. const results =
  1670. options.mode === "coverage"
  1671. ? selected.map(coverageResult)
  1672. : yield* Effect.forEach(
  1673. selected,
  1674. (scenario) =>
  1675. Effect.gen(function* () {
  1676. if (options.progress) console.log(`${color.dim}RUN ${routeKey(scenario)} ${scenario.name}${color.reset}`)
  1677. return yield* runScenario(options)(scenario)
  1678. }),
  1679. { concurrency: 1 },
  1680. )
  1681. printResults(results, missing, extra)
  1682. if (results.some((result) => result.status === "fail"))
  1683. return yield* Effect.fail(new Error("one or more scenarios failed"))
  1684. if (options.failOnSkip && results.some((result) => result.status === "skip"))
  1685. return yield* Effect.fail(new Error("one or more scenarios are skipped"))
  1686. if (options.failOnMissing && missing.length > 0)
  1687. return yield* Effect.fail(new Error("one or more routes have no scenario"))
  1688. return undefined
  1689. })
  1690. Effect.runPromise(main.pipe(Effect.provide(TestLLMServer.layer), Effect.scoped)).then(
  1691. () => process.exit(0),
  1692. (error: unknown) => {
  1693. console.error(`${color.red}${message(error)}${color.reset}`)
  1694. process.exit(1)
  1695. },
  1696. )