global-session-list.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { describe, expect } from "bun:test"
  2. import { Deferred, Effect, Layer } from "effect"
  3. import { Project } from "@/project/project"
  4. import { Session as SessionNs } from "@/session/session"
  5. import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
  6. import * as Log from "@opencode-ai/core/util/log"
  7. import { provideInstance, TestInstance, tmpdirScoped } from "../fixture/fixture"
  8. import { testEffect } from "../lib/effect"
  9. void Log.init({ print: false })
  10. const it = testEffect(Layer.mergeAll(SessionNs.defaultLayer, Project.defaultLayer, CrossSpawnSpawner.defaultLayer))
  11. const withSession = (input?: Parameters<SessionNs.Interface["create"]>[0]) =>
  12. Effect.acquireRelease(
  13. SessionNs.use.create(input),
  14. (created) => SessionNs.Service.use((session) => session.remove(created.id).pipe(Effect.ignore)),
  15. )
  16. describe("session.listGlobal", () => {
  17. it.instance(
  18. "lists sessions across projects with project metadata",
  19. () =>
  20. Effect.gen(function* () {
  21. const first = yield* TestInstance
  22. const second = yield* tmpdirScoped({ git: true })
  23. const firstSession = yield* withSession({ title: "first-session" })
  24. const secondSession = yield* withSession({ title: "second-session" }).pipe(provideInstance(second))
  25. const sessions = yield* Effect.sync(() => [...SessionNs.listGlobal({ limit: 200 })])
  26. const ids = sessions.map((session) => session.id)
  27. expect(ids).toContain(firstSession.id)
  28. expect(ids).toContain(secondSession.id)
  29. const firstProject = yield* Project.use.get(firstSession.projectID)
  30. const secondProject = yield* Project.use.get(secondSession.projectID)
  31. const firstItem = sessions.find((session) => session.id === firstSession.id)
  32. const secondItem = sessions.find((session) => session.id === secondSession.id)
  33. expect(firstItem?.project?.id).toBe(firstProject?.id)
  34. expect(firstItem?.project?.worktree).toBe(firstProject?.worktree)
  35. expect(secondItem?.project?.id).toBe(secondProject?.id)
  36. expect(secondItem?.project?.worktree).toBe(secondProject?.worktree)
  37. expect(first.directory).not.toBe(second)
  38. }),
  39. { git: true },
  40. )
  41. it.instance(
  42. "excludes archived sessions by default",
  43. () =>
  44. Effect.gen(function* () {
  45. const archived = yield* withSession({ title: "archived-session" })
  46. yield* SessionNs.Service.use((session) => session.setArchived({ sessionID: archived.id, time: Date.now() }))
  47. const sessions = yield* Effect.sync(() => [...SessionNs.listGlobal({ limit: 200 })])
  48. const ids = sessions.map((session) => session.id)
  49. expect(ids).not.toContain(archived.id)
  50. const allSessions = yield* Effect.sync(() => [...SessionNs.listGlobal({ limit: 200, archived: true })])
  51. const allIds = allSessions.map((session) => session.id)
  52. expect(allIds).toContain(archived.id)
  53. }),
  54. { git: true },
  55. )
  56. it.instance(
  57. "supports cursor pagination",
  58. () =>
  59. Effect.gen(function* () {
  60. const test = yield* TestInstance
  61. const first = yield* withSession({ title: "page-one" })
  62. const ready = yield* Deferred.make<void>()
  63. yield* Deferred.succeed(ready, undefined).pipe(Effect.delay("5 millis"), Effect.forkScoped)
  64. yield* Deferred.await(ready).pipe(
  65. Effect.timeoutOrElse({
  66. duration: "1 second",
  67. orElse: () => Effect.fail(new Error("timed out waiting between session creates")),
  68. }),
  69. )
  70. const second = yield* withSession({ title: "page-two" })
  71. const page = yield* Effect.sync(() => [...SessionNs.listGlobal({ directory: test.directory, limit: 1 })])
  72. expect(page.length).toBe(1)
  73. expect(page[0].id).toBe(second.id)
  74. const next = yield* Effect.sync(() => [
  75. ...SessionNs.listGlobal({ directory: test.directory, limit: 10, cursor: page[0].time.updated }),
  76. ])
  77. const ids = next.map((session) => session.id)
  78. expect(ids).toContain(first.id)
  79. expect(ids).not.toContain(second.id)
  80. }),
  81. { git: true },
  82. )
  83. })