|
| 1 | +import { $ } from "bun" |
| 2 | +import { describe, expect } from "bun:test" |
| 3 | +import fs from "fs/promises" |
| 4 | +import path from "path" |
| 5 | +import { Effect } from "effect" |
| 6 | +import { Agent } from "@opencode-ai/core/agent" |
| 7 | +import { Bus } from "@opencode-ai/core/bus" |
| 8 | +import { Database } from "@opencode-ai/core/database/database" |
| 9 | +import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder" |
| 10 | +import { LocationServiceMap } from "@opencode-ai/core/location-service-map" |
| 11 | +import { Model } from "@opencode-ai/core/model" |
| 12 | +import { PluginSupervisor } from "@opencode-ai/core/plugin/supervisor" |
| 13 | +import { Provider } from "@opencode-ai/core/provider" |
| 14 | +import { AbsolutePath } from "@opencode-ai/core/schema" |
| 15 | +import { Session } from "@opencode-ai/core/session" |
| 16 | +import { SessionEvent } from "@opencode-ai/core/session/event" |
| 17 | +import { SessionExecution } from "@opencode-ai/core/session/execution" |
| 18 | +import { SessionInbox } from "@opencode-ai/core/session/inbox" |
| 19 | +import { SessionMessage } from "@opencode-ai/core/session/message" |
| 20 | +import { SessionProjector } from "@opencode-ai/core/session/projector" |
| 21 | +import { Snapshot } from "@opencode-ai/core/snapshot" |
| 22 | +import { Money } from "@opencode-ai/schema/money" |
| 23 | +import { LayerNode } from "@opencode-ai/util/effect/layer-node" |
| 24 | +import { Global } from "@opencode-ai/util/global" |
| 25 | +import { tempGlobalLayer } from "./fixture/global" |
| 26 | +import { tmpdir } from "./fixture/tmpdir" |
| 27 | +import { testEffect } from "./lib/effect" |
| 28 | + |
| 29 | +const it = testEffect( |
| 30 | + AppNodeBuilder.build( |
| 31 | + LayerNode.group([Database.node, Bus.node, SessionProjector.node, Session.node, LocationServiceMap.node]), |
| 32 | + [ |
| 33 | + [Bus.node, Bus.configured({ persist: true })], |
| 34 | + [Global.node, tempGlobalLayer], |
| 35 | + [SessionExecution.node, SessionExecution.noopLayer], |
| 36 | + ], |
| 37 | + ), |
| 38 | +) |
| 39 | + |
| 40 | +describe("Session.revert files", () => { |
| 41 | + it.live("undoes and restores a file rename without losing either path", () => |
| 42 | + Effect.gen(function* () { |
| 43 | + const tmp = yield* Effect.acquireRelease( |
| 44 | + Effect.promise(() => tmpdir()), |
| 45 | + (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()), |
| 46 | + ) |
| 47 | + const directory = path.join(tmp.path, "project") |
| 48 | + const original = path.join(directory, "old name.txt") |
| 49 | + const renamed = path.join(directory, "new name.txt") |
| 50 | + yield* Effect.promise(async () => { |
| 51 | + await fs.mkdir(directory) |
| 52 | + await Bun.write(original, "Preserve this content.\n") |
| 53 | + await Bun.write(path.join(directory, "unrelated.txt"), "Unrelated content.\n") |
| 54 | + await $`git init -q`.cwd(directory).quiet() |
| 55 | + await $`git -c core.fsmonitor=false add .`.cwd(directory).quiet() |
| 56 | + }) |
| 57 | + |
| 58 | + const session = yield* Session.Service |
| 59 | + const database = yield* Database.Service |
| 60 | + const bus = yield* Bus.Service |
| 61 | + const created = yield* session.create({ location: { directory: AbsolutePath.make(directory) } }) |
| 62 | + const prompt = yield* session.prompt({ sessionID: created.id, text: "Rename the file", resume: false }) |
| 63 | + yield* SessionInbox.promote(database.db, bus, created.id, "steer") |
| 64 | + |
| 65 | + yield* Effect.gen(function* () { |
| 66 | + const plugins = yield* PluginSupervisor.Service |
| 67 | + yield* plugins.flush |
| 68 | + const snapshot = yield* Snapshot.Service |
| 69 | + const before = yield* snapshot.capture() |
| 70 | + if (!before) throw new Error("Initial snapshot missing") |
| 71 | + const assistantMessageID = SessionMessage.ID.create() |
| 72 | + yield* bus.publish(SessionEvent.Step.Started, { |
| 73 | + sessionID: created.id, |
| 74 | + assistantMessageID, |
| 75 | + agent: Agent.defaultID, |
| 76 | + model: { id: Model.ID.make("test-model"), providerID: Provider.ID.make("test-provider") }, |
| 77 | + snapshot: before, |
| 78 | + }) |
| 79 | + yield* Effect.promise(() => fs.rename(original, renamed)) |
| 80 | + const after = yield* snapshot.capture() |
| 81 | + if (!after) throw new Error("Renamed snapshot missing") |
| 82 | + yield* bus.publish(SessionEvent.Step.Ended, { |
| 83 | + sessionID: created.id, |
| 84 | + assistantMessageID, |
| 85 | + finish: "stop", |
| 86 | + cost: Money.USD.zero, |
| 87 | + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 88 | + snapshot: after, |
| 89 | + files: yield* snapshot.files({ from: before, to: after }), |
| 90 | + }) |
| 91 | + |
| 92 | + yield* Effect.promise(() => Bun.write(path.join(directory, "unrelated.txt"), "Keep this later edit.\n")) |
| 93 | + const reverted = yield* session.revert.stage({ sessionID: created.id, messageID: prompt.id }) |
| 94 | + expect({ |
| 95 | + original: yield* Effect.promise(() => Bun.file(original).exists()), |
| 96 | + renamed: yield* Effect.promise(() => Bun.file(renamed).exists()), |
| 97 | + }).toEqual({ original: true, renamed: false }) |
| 98 | + expect(yield* Effect.promise(() => Bun.file(original).text())).toBe("Preserve this content.\n") |
| 99 | + expect(reverted.files?.map((file) => [file.file, file.status])).toEqual([ |
| 100 | + ["new name.txt", "deleted"], |
| 101 | + ["old name.txt", "added"], |
| 102 | + ]) |
| 103 | + expect(yield* Effect.promise(() => Bun.file(path.join(directory, "unrelated.txt")).text())).toBe( |
| 104 | + "Keep this later edit.\n", |
| 105 | + ) |
| 106 | + |
| 107 | + yield* session.revert.clear(created.id) |
| 108 | + expect(yield* Effect.promise(() => Bun.file(original).exists())).toBe(false) |
| 109 | + expect(yield* Effect.promise(() => Bun.file(renamed).text())).toBe("Preserve this content.\n") |
| 110 | + expect(yield* Effect.promise(() => Bun.file(path.join(directory, "unrelated.txt")).text())).toBe( |
| 111 | + "Keep this later edit.\n", |
| 112 | + ) |
| 113 | + expect((yield* session.get(created.id)).revert).toBeUndefined() |
| 114 | + }).pipe(Effect.provide(LocationServiceMap.Service.get(created.location))) |
| 115 | + }), |
| 116 | + ) |
| 117 | +}) |
0 commit comments