global-session-list.test.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { describe, expect, test } from "bun:test"
  2. import { Instance } from "../../src/project/instance"
  3. import { Project } from "../../src/project/project"
  4. import { Session } from "../../src/session"
  5. import { Log } from "../../src/util/log"
  6. import { tmpdir } from "../fixture/fixture"
  7. Log.init({ print: false })
  8. describe("Session.listGlobal", () => {
  9. test("lists sessions across projects with project metadata", async () => {
  10. await using first = await tmpdir({ git: true })
  11. await using second = await tmpdir({ git: true })
  12. const firstSession = await Instance.provide({
  13. directory: first.path,
  14. fn: async () => Session.create({ title: "first-session" }),
  15. })
  16. const secondSession = await Instance.provide({
  17. directory: second.path,
  18. fn: async () => Session.create({ title: "second-session" }),
  19. })
  20. const sessions = [...Session.listGlobal({ limit: 200 })]
  21. const ids = sessions.map((session) => session.id)
  22. expect(ids).toContain(firstSession.id)
  23. expect(ids).toContain(secondSession.id)
  24. const firstProject = Project.get(firstSession.projectID)
  25. const secondProject = Project.get(secondSession.projectID)
  26. const firstItem = sessions.find((session) => session.id === firstSession.id)
  27. const secondItem = sessions.find((session) => session.id === secondSession.id)
  28. expect(firstItem?.project?.id).toBe(firstProject?.id)
  29. expect(firstItem?.project?.worktree).toBe(firstProject?.worktree)
  30. expect(secondItem?.project?.id).toBe(secondProject?.id)
  31. expect(secondItem?.project?.worktree).toBe(secondProject?.worktree)
  32. })
  33. test("excludes archived sessions by default", async () => {
  34. await using tmp = await tmpdir({ git: true })
  35. const archived = await Instance.provide({
  36. directory: tmp.path,
  37. fn: async () => Session.create({ title: "archived-session" }),
  38. })
  39. await Instance.provide({
  40. directory: tmp.path,
  41. fn: async () => Session.setArchived({ sessionID: archived.id, time: Date.now() }),
  42. })
  43. const sessions = [...Session.listGlobal({ limit: 200 })]
  44. const ids = sessions.map((session) => session.id)
  45. expect(ids).not.toContain(archived.id)
  46. const allSessions = [...Session.listGlobal({ limit: 200, archived: true })]
  47. const allIds = allSessions.map((session) => session.id)
  48. expect(allIds).toContain(archived.id)
  49. })
  50. test("supports cursor pagination", async () => {
  51. await using tmp = await tmpdir({ git: true })
  52. const first = await Instance.provide({
  53. directory: tmp.path,
  54. fn: async () => Session.create({ title: "page-one" }),
  55. })
  56. await new Promise((resolve) => setTimeout(resolve, 5))
  57. const second = await Instance.provide({
  58. directory: tmp.path,
  59. fn: async () => Session.create({ title: "page-two" }),
  60. })
  61. const page = [...Session.listGlobal({ directory: tmp.path, limit: 1 })]
  62. expect(page.length).toBe(1)
  63. expect(page[0].id).toBe(second.id)
  64. const next = [...Session.listGlobal({ directory: tmp.path, limit: 10, cursor: page[0].time.updated })]
  65. const ids = next.map((session) => session.id)
  66. expect(ids).toContain(first.id)
  67. expect(ids).not.toContain(second.id)
  68. })
  69. })