session-select.test.ts 2.4 KB

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