project-directories.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { describe, expect } from "bun:test"
  2. import { Effect, Layer, Schema } from "effect"
  3. import { Database } from "@opencode-ai/core/database/database"
  4. import { EventV2 } from "@opencode-ai/core/event"
  5. import { Project } from "@opencode-ai/core/project"
  6. import { ProjectDirectories } from "@opencode-ai/core/project/directories"
  7. import { ProjectTable } from "@opencode-ai/core/project/sql"
  8. import { AbsolutePath } from "@opencode-ai/core/schema"
  9. import { testEffect } from "./lib/effect"
  10. const it = testEffect(Layer.mergeAll(Database.defaultLayer, EventV2.defaultLayer, ProjectDirectories.defaultLayer))
  11. const projectID = Project.ID.make("project-directories")
  12. const directory = AbsolutePath.make("/tmp/project-directories")
  13. function setup() {
  14. return Database.Service.use(({ db }) =>
  15. db
  16. .insert(ProjectTable)
  17. .values({ id: projectID, worktree: directory, sandboxes: [], time_created: 1, time_updated: 1 })
  18. .onConflictDoNothing()
  19. .run()
  20. .pipe(Effect.orDie),
  21. )
  22. }
  23. describe("ProjectDirectories", () => {
  24. it.effect("decodes directory schemas", () =>
  25. Effect.sync(() => {
  26. expect(Schema.decodeUnknownSync(ProjectDirectories.ListInput)({ projectID })).toEqual({ projectID })
  27. expect(Schema.decodeUnknownSync(ProjectDirectories.ListOutput)([{ directory }])).toEqual([{ directory }])
  28. }),
  29. )
  30. it.effect("creates once and ignores conflicts", () =>
  31. Effect.gen(function* () {
  32. yield* setup()
  33. const service = yield* ProjectDirectories.Service
  34. expect(yield* service.create({ projectID, directory })).toBe(true)
  35. expect(yield* service.create({ projectID, directory, strategy: "git_worktree" })).toBe(false)
  36. expect(yield* service.list(projectID)).toEqual([{ directory, strategy: undefined }])
  37. }),
  38. )
  39. it.effect("replaces the strategy when requested", () =>
  40. Effect.gen(function* () {
  41. yield* setup()
  42. const service = yield* ProjectDirectories.Service
  43. yield* service.create({ projectID, directory, strategy: "old/strategy" })
  44. expect(yield* service.create({ projectID, directory, strategy: "new/strategy", behavior: "replace" })).toBe(true)
  45. expect(yield* service.create({ projectID, directory, strategy: "new/strategy", behavior: "replace" })).toBe(false)
  46. expect(yield* service.create({ projectID, directory, behavior: "replace" })).toBe(true)
  47. expect(yield* service.create({ projectID, directory, behavior: "replace" })).toBe(false)
  48. expect(yield* service.create({ projectID, directory, strategy: "new/strategy", behavior: "replace" })).toBe(true)
  49. expect(yield* service.list(projectID)).toEqual([{ directory, strategy: "new/strategy" }])
  50. }),
  51. )
  52. })