session-messages.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { afterEach, describe, expect, test } from "bun:test"
  2. import { Effect } from "effect"
  3. import { Instance } from "../../src/project/instance"
  4. import { Server } from "../../src/server/server"
  5. import { Session as SessionNs } from "@/session/session"
  6. import { MessageV2 } from "../../src/session/message-v2"
  7. import { MessageID, PartID, type SessionID } from "../../src/session/schema"
  8. import * as Log from "@opencode-ai/core/util/log"
  9. import { tmpdir } from "../fixture/fixture"
  10. void Log.init({ print: false })
  11. function run<A, E>(fx: Effect.Effect<A, E, SessionNs.Service>) {
  12. return Effect.runPromise(fx.pipe(Effect.provide(SessionNs.defaultLayer)))
  13. }
  14. const svc = {
  15. ...SessionNs,
  16. create(input?: SessionNs.CreateInput) {
  17. return run(SessionNs.Service.use((svc) => svc.create(input)))
  18. },
  19. remove(id: SessionID) {
  20. return run(SessionNs.Service.use((svc) => svc.remove(id)))
  21. },
  22. updateMessage<T extends MessageV2.Info>(msg: T) {
  23. return run(SessionNs.Service.use((svc) => svc.updateMessage(msg)))
  24. },
  25. updatePart<T extends MessageV2.Part>(part: T) {
  26. return run(SessionNs.Service.use((svc) => svc.updatePart(part)))
  27. },
  28. }
  29. afterEach(async () => {
  30. await Instance.disposeAll()
  31. })
  32. async function withoutWatcher<T>(fn: () => Promise<T>) {
  33. if (process.platform !== "win32") return fn()
  34. const prev = process.env.OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER
  35. process.env.OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER = "true"
  36. try {
  37. return await fn()
  38. } finally {
  39. if (prev === undefined) delete process.env.OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER
  40. else process.env.OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER = prev
  41. }
  42. }
  43. async function fill(sessionID: SessionID, count: number, time = (i: number) => Date.now() + i) {
  44. const ids = [] as MessageID[]
  45. for (let i = 0; i < count; i++) {
  46. const id = MessageID.ascending()
  47. ids.push(id)
  48. await svc.updateMessage({
  49. id,
  50. sessionID,
  51. role: "user",
  52. time: { created: time(i) },
  53. agent: "test",
  54. model: { providerID: "test", modelID: "test" },
  55. tools: {},
  56. mode: "",
  57. } as unknown as MessageV2.Info)
  58. await svc.updatePart({
  59. id: PartID.ascending(),
  60. sessionID,
  61. messageID: id,
  62. type: "text",
  63. text: `m${i}`,
  64. })
  65. }
  66. return ids
  67. }
  68. describe("session messages endpoint", () => {
  69. test("returns cursor headers for older pages", async () => {
  70. await using tmp = await tmpdir({ git: true })
  71. await withoutWatcher(() =>
  72. Instance.provide({
  73. directory: tmp.path,
  74. fn: async () => {
  75. const session = await svc.create({})
  76. const ids = await fill(session.id, 5)
  77. const app = Server.Default().app
  78. const a = await app.request(`/session/${session.id}/message?limit=2`)
  79. expect(a.status).toBe(200)
  80. const aBody = (await a.json()) as MessageV2.WithParts[]
  81. expect(aBody.map((item) => item.info.id)).toEqual(ids.slice(-2))
  82. const cursor = a.headers.get("x-next-cursor")
  83. expect(cursor).toBeTruthy()
  84. expect(a.headers.get("link")).toContain('rel="next"')
  85. const b = await app.request(`/session/${session.id}/message?limit=2&before=${encodeURIComponent(cursor!)}`)
  86. expect(b.status).toBe(200)
  87. const bBody = (await b.json()) as MessageV2.WithParts[]
  88. expect(bBody.map((item) => item.info.id)).toEqual(ids.slice(-4, -2))
  89. await svc.remove(session.id)
  90. },
  91. }),
  92. )
  93. })
  94. test("keeps full-history responses when limit is omitted", async () => {
  95. await using tmp = await tmpdir({ git: true })
  96. await withoutWatcher(() =>
  97. Instance.provide({
  98. directory: tmp.path,
  99. fn: async () => {
  100. const session = await svc.create({})
  101. const ids = await fill(session.id, 3)
  102. const app = Server.Default().app
  103. const res = await app.request(`/session/${session.id}/message`)
  104. expect(res.status).toBe(200)
  105. const body = (await res.json()) as MessageV2.WithParts[]
  106. expect(body.map((item) => item.info.id)).toEqual(ids)
  107. await svc.remove(session.id)
  108. },
  109. }),
  110. )
  111. })
  112. test("rejects invalid cursors and missing sessions", async () => {
  113. await using tmp = await tmpdir({ git: true })
  114. await withoutWatcher(() =>
  115. Instance.provide({
  116. directory: tmp.path,
  117. fn: async () => {
  118. const session = await svc.create({})
  119. const app = Server.Default().app
  120. const bad = await app.request(`/session/${session.id}/message?limit=2&before=bad`)
  121. expect(bad.status).toBe(400)
  122. const miss = await app.request(`/session/ses_missing/message?limit=2`)
  123. expect(miss.status).toBe(404)
  124. await svc.remove(session.id)
  125. },
  126. }),
  127. )
  128. })
  129. test("does not truncate large legacy limit requests", async () => {
  130. await using tmp = await tmpdir({ git: true })
  131. await withoutWatcher(() =>
  132. Instance.provide({
  133. directory: tmp.path,
  134. fn: async () => {
  135. const session = await svc.create({})
  136. await fill(session.id, 520)
  137. const app = Server.Default().app
  138. const res = await app.request(`/session/${session.id}/message?limit=510`)
  139. expect(res.status).toBe(200)
  140. const body = (await res.json()) as MessageV2.WithParts[]
  141. expect(body).toHaveLength(510)
  142. await svc.remove(session.id)
  143. },
  144. }),
  145. )
  146. })
  147. })