session-select.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { afterEach, describe, expect, test } from "bun:test"
  2. import { Effect } from "effect"
  3. import { Session as SessionNs } from "../../src/session"
  4. import type { SessionID } from "../../src/session/schema"
  5. import { Log } from "../../src/util/log"
  6. import { Instance } from "../../src/project/instance"
  7. import { Server } from "../../src/server/server"
  8. import { tmpdir } from "../fixture/fixture"
  9. Log.init({ print: false })
  10. function run<A, E>(fx: Effect.Effect<A, E, SessionNs.Service>) {
  11. return Effect.runPromise(fx.pipe(Effect.provide(SessionNs.defaultLayer)))
  12. }
  13. const svc = {
  14. ...SessionNs,
  15. create(input?: SessionNs.CreateInput) {
  16. return run(SessionNs.Service.use((svc) => svc.create(input)))
  17. },
  18. remove(id: SessionID) {
  19. return run(SessionNs.Service.use((svc) => svc.remove(id)))
  20. },
  21. }
  22. afterEach(async () => {
  23. await Instance.disposeAll()
  24. })
  25. describe("tui.selectSession endpoint", () => {
  26. test("should return 200 when called with valid session", async () => {
  27. await using tmp = await tmpdir({ git: true })
  28. await Instance.provide({
  29. directory: tmp.path,
  30. fn: async () => {
  31. // #given
  32. const session = await svc.create({})
  33. // #when
  34. const app = Server.Default().app
  35. const response = await app.request("/tui/select-session", {
  36. method: "POST",
  37. headers: { "Content-Type": "application/json" },
  38. body: JSON.stringify({ sessionID: session.id }),
  39. })
  40. // #then
  41. expect(response.status).toBe(200)
  42. const body = await response.json()
  43. expect(body).toBe(true)
  44. await svc.remove(session.id)
  45. },
  46. })
  47. })
  48. test("should return 404 when session does not exist", async () => {
  49. await using tmp = await tmpdir({ git: true })
  50. await Instance.provide({
  51. directory: tmp.path,
  52. fn: async () => {
  53. // #given
  54. const nonExistentSessionID = "ses_nonexistent123"
  55. // #when
  56. const app = Server.Default().app
  57. const response = await app.request("/tui/select-session", {
  58. method: "POST",
  59. headers: { "Content-Type": "application/json" },
  60. body: JSON.stringify({ sessionID: nonExistentSessionID }),
  61. })
  62. // #then
  63. expect(response.status).toBe(404)
  64. },
  65. })
  66. })
  67. test("should return 400 when session ID format is invalid", async () => {
  68. await using tmp = await tmpdir({ git: true })
  69. await Instance.provide({
  70. directory: tmp.path,
  71. fn: async () => {
  72. // #given
  73. const invalidSessionID = "invalid_session_id"
  74. // #when
  75. const app = Server.Default().app
  76. const response = await app.request("/tui/select-session", {
  77. method: "POST",
  78. headers: { "Content-Type": "application/json" },
  79. body: JSON.stringify({ sessionID: invalidSessionID }),
  80. })
  81. // #then
  82. expect(response.status).toBe(400)
  83. },
  84. })
  85. })
  86. })