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