session-list.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { afterEach, describe, expect, test } from "bun:test"
  2. import { Effect } from "effect"
  3. import { Instance } from "../../src/project/instance"
  4. import { Session as SessionNs } from "../../src/session"
  5. import { Log } from "../../src/util/log"
  6. import { tmpdir } from "../fixture/fixture"
  7. Log.init({ print: false })
  8. function run<A, E>(fx: Effect.Effect<A, E, SessionNs.Service>) {
  9. return Effect.runPromise(fx.pipe(Effect.provide(SessionNs.defaultLayer)))
  10. }
  11. const svc = {
  12. ...SessionNs,
  13. create(input?: SessionNs.CreateInput) {
  14. return run(SessionNs.Service.use((svc) => svc.create(input)))
  15. },
  16. }
  17. afterEach(async () => {
  18. await Instance.disposeAll()
  19. })
  20. describe("session.list", () => {
  21. test("filters by directory", async () => {
  22. await using tmp = await tmpdir({ git: true })
  23. await Instance.provide({
  24. directory: tmp.path,
  25. fn: async () => {
  26. const first = await svc.create({})
  27. await using other = await tmpdir({ git: true })
  28. const second = await Instance.provide({
  29. directory: other.path,
  30. fn: async () => svc.create({}),
  31. })
  32. const sessions = [...svc.list({ directory: tmp.path })]
  33. const ids = sessions.map((s) => s.id)
  34. expect(ids).toContain(first.id)
  35. expect(ids).not.toContain(second.id)
  36. },
  37. })
  38. })
  39. test("filters root sessions", async () => {
  40. await using tmp = await tmpdir({ git: true })
  41. await Instance.provide({
  42. directory: tmp.path,
  43. fn: async () => {
  44. const root = await svc.create({ title: "root-session" })
  45. const child = await svc.create({ title: "child-session", parentID: root.id })
  46. const sessions = [...svc.list({ roots: true })]
  47. const ids = sessions.map((s) => s.id)
  48. expect(ids).toContain(root.id)
  49. expect(ids).not.toContain(child.id)
  50. },
  51. })
  52. })
  53. test("filters by start time", async () => {
  54. await using tmp = await tmpdir({ git: true })
  55. await Instance.provide({
  56. directory: tmp.path,
  57. fn: async () => {
  58. const session = await svc.create({ title: "new-session" })
  59. const futureStart = Date.now() + 86400000
  60. const sessions = [...svc.list({ start: futureStart })]
  61. expect(sessions.length).toBe(0)
  62. },
  63. })
  64. })
  65. test("filters by search term", async () => {
  66. await using tmp = await tmpdir({ git: true })
  67. await Instance.provide({
  68. directory: tmp.path,
  69. fn: async () => {
  70. await svc.create({ title: "unique-search-term-abc" })
  71. await svc.create({ title: "other-session-xyz" })
  72. const sessions = [...svc.list({ search: "unique-search" })]
  73. const titles = sessions.map((s) => s.title)
  74. expect(titles).toContain("unique-search-term-abc")
  75. expect(titles).not.toContain("other-session-xyz")
  76. },
  77. })
  78. })
  79. test("respects limit parameter", async () => {
  80. await using tmp = await tmpdir({ git: true })
  81. await Instance.provide({
  82. directory: tmp.path,
  83. fn: async () => {
  84. await svc.create({ title: "session-1" })
  85. await svc.create({ title: "session-2" })
  86. await svc.create({ title: "session-3" })
  87. const sessions = [...svc.list({ limit: 2 })]
  88. expect(sessions.length).toBe(2)
  89. },
  90. })
  91. })
  92. })