| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- import { afterEach, describe, expect, test } from "bun:test"
- import { Instance } from "../../src/project/instance"
- import { Server } from "../../src/server/server"
- import { Session } from "../../src/session"
- import { MessageV2 } from "../../src/session/message-v2"
- import { MessageID, PartID, type SessionID } from "../../src/session/schema"
- import { Log } from "../../src/util/log"
- import { tmpdir } from "../fixture/fixture"
- Log.init({ print: false })
- afterEach(async () => {
- await Instance.disposeAll()
- })
- async function withoutWatcher<T>(fn: () => Promise<T>) {
- if (process.platform !== "win32") return fn()
- const prev = process.env.KILO_EXPERIMENTAL_DISABLE_FILEWATCHER
- process.env.KILO_EXPERIMENTAL_DISABLE_FILEWATCHER = "true"
- try {
- return await fn()
- } finally {
- if (prev === undefined) delete process.env.KILO_EXPERIMENTAL_DISABLE_FILEWATCHER
- else process.env.KILO_EXPERIMENTAL_DISABLE_FILEWATCHER = prev
- }
- }
- async function fill(sessionID: SessionID, count: number, time = (i: number) => Date.now() + i) {
- const ids = [] as MessageID[]
- for (let i = 0; i < count; i++) {
- const id = MessageID.ascending()
- ids.push(id)
- await Session.updateMessage({
- id,
- sessionID,
- role: "user",
- time: { created: time(i) },
- agent: "test",
- model: { providerID: "test", modelID: "test" },
- tools: {},
- mode: "",
- } as unknown as MessageV2.Info)
- await Session.updatePart({
- id: PartID.ascending(),
- sessionID,
- messageID: id,
- type: "text",
- text: `m${i}`,
- })
- }
- return ids
- }
- describe("session messages endpoint", () => {
- test("returns cursor headers for older pages", async () => {
- await using tmp = await tmpdir({ git: true })
- await withoutWatcher(() =>
- Instance.provide({
- directory: tmp.path,
- fn: async () => {
- const session = await Session.create({})
- const ids = await fill(session.id, 5)
- const app = Server.Default().app
- const a = await app.request(`/session/${session.id}/message?limit=2`)
- expect(a.status).toBe(200)
- const aBody = (await a.json()) as MessageV2.WithParts[]
- expect(aBody.map((item) => item.info.id)).toEqual(ids.slice(-2))
- const cursor = a.headers.get("x-next-cursor")
- expect(cursor).toBeTruthy()
- expect(a.headers.get("link")).toContain('rel="next"')
- const b = await app.request(`/session/${session.id}/message?limit=2&before=${encodeURIComponent(cursor!)}`)
- expect(b.status).toBe(200)
- const bBody = (await b.json()) as MessageV2.WithParts[]
- expect(bBody.map((item) => item.info.id)).toEqual(ids.slice(-4, -2))
- await Session.remove(session.id)
- },
- }),
- )
- })
- test("keeps full-history responses when limit is omitted", async () => {
- await using tmp = await tmpdir({ git: true })
- await withoutWatcher(() =>
- Instance.provide({
- directory: tmp.path,
- fn: async () => {
- const session = await Session.create({})
- const ids = await fill(session.id, 3)
- const app = Server.Default().app
- const res = await app.request(`/session/${session.id}/message`)
- expect(res.status).toBe(200)
- const body = (await res.json()) as MessageV2.WithParts[]
- expect(body.map((item) => item.info.id)).toEqual(ids)
- await Session.remove(session.id)
- },
- }),
- )
- })
- test("rejects invalid cursors and missing sessions", async () => {
- await using tmp = await tmpdir({ git: true })
- await withoutWatcher(() =>
- Instance.provide({
- directory: tmp.path,
- fn: async () => {
- const session = await Session.create({})
- const app = Server.Default().app
- const bad = await app.request(`/session/${session.id}/message?limit=2&before=bad`)
- expect(bad.status).toBe(400)
- const miss = await app.request(`/session/ses_missing/message?limit=2`)
- expect(miss.status).toBe(404)
- await Session.remove(session.id)
- },
- }),
- )
- })
- test("does not truncate large legacy limit requests", async () => {
- await using tmp = await tmpdir({ git: true })
- await withoutWatcher(() =>
- Instance.provide({
- directory: tmp.path,
- fn: async () => {
- const session = await Session.create({})
- await fill(session.id, 520)
- const app = Server.Default().app
- const res = await app.request(`/session/${session.id}/message?limit=510`)
- expect(res.status).toBe(200)
- const body = (await res.json()) as MessageV2.WithParts[]
- expect(body).toHaveLength(510)
- await Session.remove(session.id)
- },
- }),
- )
- })
- })
- describe("session.prompt_async error handling", () => {
- test("prompt_async route has error handler for detached prompt call", async () => {
- const src = await Bun.file(new URL("../../src/server/routes/session.ts", import.meta.url)).text()
- const start = src.indexOf('"/:sessionID/prompt_async"')
- const end = src.indexOf('"/:sessionID/command"', start)
- expect(start).toBeGreaterThan(-1)
- expect(end).toBeGreaterThan(start)
- const route = src.slice(start, end)
- expect(route).toContain(".catch(")
- expect(route).toContain("Bus.publish(Session.Event.Error")
- })
- })
|