|
| 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 | + |
| 8 | +const projectRoot = path.join(__dirname, "../..") |
| 9 | +Log.init({ print: false }) |
| 10 | + |
| 11 | +describe("tui.selectSession endpoint", () => { |
| 12 | + test("should return 200 when called with valid session", async () => { |
| 13 | + await Instance.provide({ |
| 14 | + directory: projectRoot, |
| 15 | + fn: async () => { |
| 16 | + // #given |
| 17 | + const session = await Session.create({}) |
| 18 | + |
| 19 | + // #when |
| 20 | + const app = Server.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 | + |
| 27 | + // #then |
| 28 | + expect(response.status).toBe(200) |
| 29 | + const body = await response.json() |
| 30 | + expect(body).toBe(true) |
| 31 | + |
| 32 | + await Session.remove(session.id) |
| 33 | + }, |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + test("should return 404 when session does not exist", async () => { |
| 38 | + await Instance.provide({ |
| 39 | + directory: projectRoot, |
| 40 | + fn: async () => { |
| 41 | + // #given |
| 42 | + const nonExistentSessionID = "ses_nonexistent123" |
| 43 | + |
| 44 | + // #when |
| 45 | + const app = Server.App() |
| 46 | + const response = await app.request("/tui/select-session", { |
| 47 | + method: "POST", |
| 48 | + headers: { "Content-Type": "application/json" }, |
| 49 | + body: JSON.stringify({ sessionID: nonExistentSessionID }), |
| 50 | + }) |
| 51 | + |
| 52 | + // #then |
| 53 | + expect(response.status).toBe(404) |
| 54 | + }, |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + test("should return 400 when session ID format is invalid", async () => { |
| 59 | + await Instance.provide({ |
| 60 | + directory: projectRoot, |
| 61 | + fn: async () => { |
| 62 | + // #given |
| 63 | + const invalidSessionID = "invalid_session_id" |
| 64 | + |
| 65 | + // #when |
| 66 | + const app = Server.App() |
| 67 | + const response = await app.request("/tui/select-session", { |
| 68 | + method: "POST", |
| 69 | + headers: { "Content-Type": "application/json" }, |
| 70 | + body: JSON.stringify({ sessionID: invalidSessionID }), |
| 71 | + }) |
| 72 | + |
| 73 | + // #then |
| 74 | + expect(response.status).toBe(400) |
| 75 | + }, |
| 76 | + }) |
| 77 | + }) |
| 78 | +}) |
0 commit comments