+21

![opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



![opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



James Long
Brendan Allan
Kit Langton
opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Affan Ali
affanali2k3
Frank
opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>
𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴
Aiden Cline
Jay V
Dax Raad
Aarav Sareen
OpeOginni
Luke Parker
Ben Guthrie
Dax
Filip
Max Anderson
Brendan Allan
Jack
Shoubhit Dash
Dustin Deus
starptech
Aiden Cline
usrnk1
Jay
runvip
opencode
Julian Coy
Vladimir Glafirov
8c94e9005f
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
203 lines
7.8 KiB
TypeScript
203 lines
7.8 KiB
TypeScript
import { describe, expect } from "bun:test"
|
|
import { $ } from "bun"
|
|
import path from "path"
|
|
import { eq } from "drizzle-orm"
|
|
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
|
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
|
|
import { Effect } from "effect"
|
|
import { Hash } from "@opencode-ai/core/util/hash"
|
|
import { AbsolutePath } from "@opencode-ai/core/schema"
|
|
import { Database } from "@opencode-ai/core/database/database"
|
|
import { ProjectDirectoryTable, ProjectTable } from "@opencode-ai/core/project/sql"
|
|
import { ProjectV2 } from "@opencode-ai/core/project"
|
|
import { Project } from "@/project/project"
|
|
import { tmpdirScoped } from "../fixture/fixture"
|
|
import { testEffect } from "../lib/effect"
|
|
|
|
const it = testEffect(LayerNode.compile(LayerNode.group([Project.node, Database.node, CrossSpawnSpawner.node])))
|
|
|
|
function directories(projectID: ProjectV2.ID) {
|
|
return Database.Service.use(({ db }) =>
|
|
db
|
|
.select()
|
|
.from(ProjectDirectoryTable)
|
|
.where(eq(ProjectDirectoryTable.project_id, projectID))
|
|
.all()
|
|
.pipe(
|
|
Effect.orDie,
|
|
Effect.map((rows) =>
|
|
rows
|
|
.map((row) => ({ directory: row.directory, strategy: row.strategy ?? undefined }))
|
|
.toSorted((a, b) => a.directory.localeCompare(b.directory)),
|
|
),
|
|
),
|
|
)
|
|
}
|
|
|
|
describe("Project directory persistence", () => {
|
|
it.live("stores the first opened checkout directory", () =>
|
|
Effect.gen(function* () {
|
|
const tmp = yield* tmpdirScoped({ git: true })
|
|
const project = yield* Project.Service
|
|
|
|
const result = yield* project.fromDirectory(tmp)
|
|
|
|
expect(yield* directories(result.project.id)).toEqual([
|
|
{ directory: AbsolutePath.make(tmp), strategy: undefined },
|
|
])
|
|
}),
|
|
)
|
|
|
|
it.live("stores a repeatedly opened checkout directory only once", () =>
|
|
Effect.gen(function* () {
|
|
const tmp = yield* tmpdirScoped({ git: true })
|
|
const project = yield* Project.Service
|
|
|
|
const result = yield* project.fromDirectory(tmp)
|
|
const next = yield* project.fromDirectory(tmp)
|
|
|
|
expect(next.project.id).toBe(result.project.id)
|
|
expect(yield* directories(result.project.id)).toEqual([
|
|
{ directory: AbsolutePath.make(tmp), strategy: undefined },
|
|
])
|
|
}),
|
|
)
|
|
|
|
it.live("stores an opened linked worktree directory", () =>
|
|
Effect.gen(function* () {
|
|
const tmp = yield* tmpdirScoped({ git: true })
|
|
const project = yield* Project.Service
|
|
const main = yield* project.fromDirectory(tmp)
|
|
const worktree = path.join(tmp, "..", path.basename(tmp) + "-project-directory-worktree")
|
|
yield* Effect.addFinalizer(() =>
|
|
Effect.promise(() => $`git worktree remove ${worktree}`.cwd(tmp).quiet().nothrow()).pipe(Effect.ignore),
|
|
)
|
|
yield* Effect.promise(() => $`git worktree add ${worktree} -b project-directory-${Date.now()}`.cwd(tmp).quiet())
|
|
|
|
yield* project.fromDirectory(worktree)
|
|
|
|
expect(yield* directories(main.project.id)).toEqual(
|
|
[
|
|
{ directory: AbsolutePath.make(tmp), strategy: undefined },
|
|
{ directory: AbsolutePath.make(worktree), strategy: undefined },
|
|
].toSorted((a, b) => a.directory.localeCompare(b.directory)),
|
|
)
|
|
}),
|
|
)
|
|
|
|
it.live("stores only the linked copy when first opened from an external linked worktree", () =>
|
|
Effect.gen(function* () {
|
|
const tmp = yield* tmpdirScoped({ git: true })
|
|
const worktree = path.join(tmp, "..", path.basename(tmp) + "-project-directory-first-worktree")
|
|
yield* Effect.addFinalizer(() =>
|
|
Effect.promise(() => $`git worktree remove ${worktree}`.cwd(tmp).quiet().nothrow()).pipe(Effect.ignore),
|
|
)
|
|
yield* Effect.promise(() => $`git worktree add --detach ${worktree} HEAD`.cwd(tmp).quiet())
|
|
const project = yield* Project.Service
|
|
|
|
const result = yield* project.fromDirectory(worktree)
|
|
|
|
expect(yield* directories(result.project.id)).toEqual([
|
|
{ directory: AbsolutePath.make(worktree), strategy: undefined },
|
|
])
|
|
}),
|
|
)
|
|
|
|
it.live("stores a separately opened clone as a secondary directory", () =>
|
|
Effect.gen(function* () {
|
|
const tmp = yield* tmpdirScoped({ git: true })
|
|
const bare = tmp + "-project-directory-bare"
|
|
const clone = tmp + "-project-directory-clone"
|
|
yield* Effect.addFinalizer(() =>
|
|
Effect.promise(() => $`rm -rf ${bare} ${clone}`.quiet().nothrow()).pipe(Effect.ignore),
|
|
)
|
|
yield* Effect.promise(() => $`git clone --bare ${tmp} ${bare}`.quiet())
|
|
yield* Effect.promise(() => $`git clone ${bare} ${clone}`.quiet())
|
|
const project = yield* Project.Service
|
|
const main = yield* project.fromDirectory(tmp)
|
|
|
|
yield* project.fromDirectory(clone)
|
|
|
|
expect(yield* directories(main.project.id)).toEqual(
|
|
[
|
|
{ directory: AbsolutePath.make(tmp), strategy: undefined },
|
|
{ directory: AbsolutePath.make(clone), strategy: undefined },
|
|
].toSorted((a, b) => a.directory.localeCompare(b.directory)),
|
|
)
|
|
}),
|
|
)
|
|
|
|
it.live("stores only the materialized worktree for a bare repository", () =>
|
|
Effect.gen(function* () {
|
|
const tmp = yield* tmpdirScoped({ git: true })
|
|
const bare = tmp + "-project-directory-bare-store.git"
|
|
const worktree = tmp + "-project-directory-bare-worktree"
|
|
yield* Effect.addFinalizer(() =>
|
|
Effect.promise(() => $`rm -rf ${bare} ${worktree}`.quiet().nothrow()).pipe(Effect.ignore),
|
|
)
|
|
yield* Effect.promise(() => $`git clone --bare ${tmp} ${bare}`.quiet())
|
|
yield* Effect.promise(() => $`git worktree add ${worktree} HEAD`.cwd(bare).quiet())
|
|
const project = yield* Project.Service
|
|
|
|
const result = yield* project.fromDirectory(worktree)
|
|
|
|
expect(yield* directories(result.project.id)).toEqual([
|
|
{ directory: AbsolutePath.make(worktree), strategy: undefined },
|
|
])
|
|
}),
|
|
)
|
|
|
|
it.live("records the active directory under its newly resolved project id", () =>
|
|
Effect.gen(function* () {
|
|
const tmp = yield* tmpdirScoped({ git: true })
|
|
const project = yield* Project.Service
|
|
yield* project.fromDirectory(tmp)
|
|
const remoteID = ProjectV2.ID.make(Hash.fast("git-remote:github.com/project-directory-test/collision"))
|
|
const { db } = yield* Database.Service
|
|
yield* db
|
|
.insert(ProjectTable)
|
|
.values({
|
|
id: remoteID,
|
|
worktree: AbsolutePath.make("/tmp/existing"),
|
|
vcs: "git",
|
|
time_created: Date.now(),
|
|
time_updated: Date.now(),
|
|
sandboxes: [],
|
|
})
|
|
.run()
|
|
.pipe(Effect.orDie)
|
|
yield* Effect.promise(() =>
|
|
$`git remote add origin git@github.com:project-directory-test/collision.git`.cwd(tmp).quiet(),
|
|
)
|
|
|
|
yield* project.fromDirectory(tmp)
|
|
|
|
expect(yield* directories(remoteID)).toEqual([{ directory: AbsolutePath.make(tmp), strategy: undefined }])
|
|
}),
|
|
)
|
|
|
|
it.live("clears stale directories when the project id changes", () =>
|
|
Effect.gen(function* () {
|
|
const tmp = yield* tmpdirScoped({ git: true })
|
|
const project = yield* Project.Service
|
|
const original = yield* project.fromDirectory(tmp)
|
|
const stale = AbsolutePath.make(tmp + "-stale-checkout")
|
|
const { db } = yield* Database.Service
|
|
yield* db
|
|
.insert(ProjectDirectoryTable)
|
|
.values({ project_id: original.project.id, directory: stale })
|
|
.run()
|
|
.pipe(Effect.orDie)
|
|
const remoteID = ProjectV2.ID.make(Hash.fast("git-remote:github.com/project-directory-test/migration"))
|
|
yield* Effect.promise(() =>
|
|
$`git remote add origin git@github.com:project-directory-test/migration.git`.cwd(tmp).quiet(),
|
|
)
|
|
|
|
yield* project.fromDirectory(tmp)
|
|
|
|
expect(yield* directories(original.project.id)).toEqual([])
|
|
expect(yield* directories(remoteID)).toEqual([{ directory: AbsolutePath.make(tmp), strategy: undefined }])
|
|
}),
|
|
)
|
|
})
|