2
0

session-select.test.ts 2.2 KB

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