httpapi-file.test.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { afterEach, describe, expect, test } from "bun:test"
  2. import { Context } from "effect"
  3. import path from "path"
  4. import { ExperimentalHttpApiServer } from "../../src/server/routes/instance/httpapi/server"
  5. import { FilePaths } from "../../src/server/routes/instance/httpapi/file"
  6. import { Instance } from "../../src/project/instance"
  7. import { Log } from "../../src/util"
  8. import { resetDatabase } from "../fixture/db"
  9. import { tmpdir } from "../fixture/fixture"
  10. void Log.init({ print: false })
  11. const context = Context.empty() as Context.Context<unknown>
  12. function request(route: string, directory: string, query?: Record<string, string>) {
  13. const url = new URL(`http://localhost${route}`)
  14. for (const [key, value] of Object.entries(query ?? {})) {
  15. url.searchParams.set(key, value)
  16. }
  17. return ExperimentalHttpApiServer.webHandler().handler(
  18. new Request(url, {
  19. headers: {
  20. "x-opencode-directory": directory,
  21. },
  22. }),
  23. context,
  24. )
  25. }
  26. afterEach(async () => {
  27. await Instance.disposeAll()
  28. await resetDatabase()
  29. })
  30. describe("file HttpApi", () => {
  31. test("serves read endpoints", async () => {
  32. await using tmp = await tmpdir({ git: true })
  33. await Bun.write(path.join(tmp.path, "hello.txt"), "hello")
  34. const [list, content, status] = await Promise.all([
  35. request(FilePaths.list, tmp.path, { path: "." }),
  36. request(FilePaths.content, tmp.path, { path: "hello.txt" }),
  37. request(FilePaths.status, tmp.path),
  38. ])
  39. expect(list.status).toBe(200)
  40. expect(await list.json()).toContainEqual(
  41. expect.objectContaining({ name: "hello.txt", path: "hello.txt", type: "file" }),
  42. )
  43. expect(content.status).toBe(200)
  44. expect(await content.json()).toMatchObject({ type: "text", content: "hello" })
  45. expect(status.status).toBe(200)
  46. expect(await status.json()).toContainEqual({ path: "hello.txt", added: 1, removed: 0, status: "added" })
  47. })
  48. })