migrate-global.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { describe, expect, test } from "bun:test"
  2. import { Project } from "@/project/project"
  3. import { Database } from "@/storage/db"
  4. import { eq } from "drizzle-orm"
  5. import { SessionTable } from "../../src/session/session.sql"
  6. import { ProjectTable } from "../../src/project/project.sql"
  7. import { ProjectID } from "../../src/project/schema"
  8. import { SessionID } from "../../src/session/schema"
  9. import * as Log from "@opencode-ai/core/util/log"
  10. import { $ } from "bun"
  11. import { tmpdir } from "../fixture/fixture"
  12. import { Effect } from "effect"
  13. Log.init({ print: false })
  14. function run<A>(fn: (svc: Project.Interface) => Effect.Effect<A>) {
  15. return Effect.runPromise(
  16. Effect.gen(function* () {
  17. const svc = yield* Project.Service
  18. return yield* fn(svc)
  19. }).pipe(Effect.provide(Project.defaultLayer)),
  20. )
  21. }
  22. function legacySessionID() {
  23. // Global-session migration covers persisted IDs from before prefixed session IDs.
  24. return crypto.randomUUID() as SessionID
  25. }
  26. function seed(opts: { id: SessionID; dir: string; project: ProjectID }) {
  27. const now = Date.now()
  28. Database.use((db) =>
  29. db
  30. .insert(SessionTable)
  31. .values({
  32. id: opts.id,
  33. project_id: opts.project,
  34. slug: opts.id,
  35. directory: opts.dir,
  36. title: "test",
  37. version: "0.0.0-test",
  38. time_created: now,
  39. time_updated: now,
  40. })
  41. .run(),
  42. )
  43. }
  44. function ensureGlobal() {
  45. Database.use((db) =>
  46. db
  47. .insert(ProjectTable)
  48. .values({
  49. id: ProjectID.global,
  50. worktree: "/",
  51. time_created: Date.now(),
  52. time_updated: Date.now(),
  53. sandboxes: [],
  54. })
  55. .onConflictDoNothing()
  56. .run(),
  57. )
  58. }
  59. describe("migrateFromGlobal", () => {
  60. test("migrates global sessions on first project creation", async () => {
  61. // 1. Start with git init but no commits — creates "global" project row
  62. await using tmp = await tmpdir()
  63. await $`git init`.cwd(tmp.path).quiet()
  64. await $`git config user.name "Test"`.cwd(tmp.path).quiet()
  65. await $`git config user.email "test@opencode.test"`.cwd(tmp.path).quiet()
  66. await $`git config commit.gpgsign false`.cwd(tmp.path).quiet()
  67. const { project: pre } = await run((svc) => svc.fromDirectory(tmp.path))
  68. expect(pre.id).toBe(ProjectID.global)
  69. // 2. Seed a session under "global" with matching directory
  70. const id = legacySessionID()
  71. seed({ id, dir: tmp.path, project: ProjectID.global })
  72. // 3. Make a commit so the project gets a real ID
  73. await $`git commit --allow-empty -m "root"`.cwd(tmp.path).quiet()
  74. const { project: real } = await run((svc) => svc.fromDirectory(tmp.path))
  75. expect(real.id).not.toBe(ProjectID.global)
  76. // 4. The session should have been migrated to the real project ID
  77. const row = Database.use((db) => db.select().from(SessionTable).where(eq(SessionTable.id, id)).get())
  78. expect(row).toBeDefined()
  79. expect(row!.project_id).toBe(real.id)
  80. })
  81. test("migrates global sessions even when project row already exists", async () => {
  82. // 1. Create a repo with a commit — real project ID created immediately
  83. await using tmp = await tmpdir({ git: true })
  84. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  85. expect(project.id).not.toBe(ProjectID.global)
  86. // 2. Ensure "global" project row exists (as it would from a prior no-git session)
  87. ensureGlobal()
  88. // 3. Seed a session under "global" with matching directory.
  89. // This simulates a session created before git init that wasn't
  90. // present when the real project row was first created.
  91. const id = legacySessionID()
  92. seed({ id, dir: tmp.path, project: ProjectID.global })
  93. // 4. Call fromDirectory again — project row already exists,
  94. // so the current code skips migration entirely. This is the bug.
  95. await run((svc) => svc.fromDirectory(tmp.path))
  96. const row = Database.use((db) => db.select().from(SessionTable).where(eq(SessionTable.id, id)).get())
  97. expect(row).toBeDefined()
  98. expect(row!.project_id).toBe(project.id)
  99. })
  100. test("does not claim sessions with empty directory", async () => {
  101. await using tmp = await tmpdir({ git: true })
  102. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  103. expect(project.id).not.toBe(ProjectID.global)
  104. ensureGlobal()
  105. // Legacy sessions may lack a directory value.
  106. // Without a matching origin directory, they should remain global.
  107. const id = legacySessionID()
  108. seed({ id, dir: "", project: ProjectID.global })
  109. await run((svc) => svc.fromDirectory(tmp.path))
  110. const row = Database.use((db) => db.select().from(SessionTable).where(eq(SessionTable.id, id)).get())
  111. expect(row).toBeDefined()
  112. expect(row!.project_id).toBe(ProjectID.global)
  113. })
  114. test("does not steal sessions from unrelated directories", async () => {
  115. await using tmp = await tmpdir({ git: true })
  116. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  117. expect(project.id).not.toBe(ProjectID.global)
  118. ensureGlobal()
  119. // Seed a session under "global" but for a DIFFERENT directory
  120. const id = legacySessionID()
  121. seed({ id, dir: "/some/other/dir", project: ProjectID.global })
  122. await run((svc) => svc.fromDirectory(tmp.path))
  123. const row = Database.use((db) => db.select().from(SessionTable).where(eq(SessionTable.id, id)).get())
  124. expect(row).toBeDefined()
  125. // Should remain under "global" — not stolen
  126. expect(row!.project_id).toBe(ProjectID.global)
  127. })
  128. })