|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off |
| 2 | +import * as NodePath from "node:path"; |
| 3 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | +import { it } from "@effect/vitest"; |
| 5 | +import { CheckpointRef } from "@t3tools/contracts"; |
| 6 | +import * as Effect from "effect/Effect"; |
| 7 | +import * as FileSystem from "effect/FileSystem"; |
| 8 | +import * as Layer from "effect/Layer"; |
| 9 | +import * as PlatformError from "effect/PlatformError"; |
| 10 | +import * as Scope from "effect/Scope"; |
| 11 | +import { expect } from "vite-plus/test"; |
| 12 | +import * as CheckpointStore from "./CheckpointStore.ts"; |
| 13 | +import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; |
| 14 | +import * as VcsProcess from "../vcs/VcsProcess.ts"; |
| 15 | +import { createJjRepo, describeJj, runGit, runJj } from "../vcs/testing/JjTestSupport.ts"; |
| 16 | +import * as ServerConfig from "../config.ts"; |
| 17 | + |
| 18 | +const layerServerConfig = ServerConfig.ServerConfig.layerTest(process.cwd(), { |
| 19 | + prefix: "t3-checkpoint-store-test-", |
| 20 | +}); |
| 21 | +const layerVcsProcessTest = VcsProcess.layer.pipe(Layer.provide(NodeServices.layer)); |
| 22 | +const layerVcsDriverTest = VcsDriverRegistry.layer.pipe(Layer.provide(layerVcsProcessTest)); |
| 23 | +const layerCheckpointStoreTest = CheckpointStore.layer.pipe( |
| 24 | + Layer.provideMerge(layerVcsDriverTest), |
| 25 | + Layer.provideMerge(NodeServices.layer), |
| 26 | +); |
| 27 | +const layerTest = layerCheckpointStoreTest.pipe( |
| 28 | + Layer.provideMerge(layerVcsProcessTest), |
| 29 | + Layer.provideMerge(layerVcsDriverTest), |
| 30 | + Layer.provideMerge(layerServerConfig), |
| 31 | + Layer.provideMerge(NodeServices.layer), |
| 32 | +); |
| 33 | + |
| 34 | +function makeTmpDir( |
| 35 | + prefix = "checkpoint-store-test-", |
| 36 | +): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> { |
| 37 | + return Effect.gen(function* () { |
| 38 | + const fileSystem = yield* FileSystem.FileSystem; |
| 39 | + return yield* fileSystem.makeTempDirectoryScoped({ prefix }); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +it.layer(layerTest)("CheckpointStore activation", (it) => { |
| 44 | + describeJj("with Jujutsu installed", () => { |
| 45 | + it.effect( |
| 46 | + "restores a Git-captured checkpoint after activation without a visible checkpoint commit", |
| 47 | + () => |
| 48 | + Effect.gen(function* () { |
| 49 | + const cwd = yield* makeTmpDir(); |
| 50 | + const fs = yield* FileSystem.FileSystem; |
| 51 | + yield* runGit(cwd, ["init", "--initial-branch=main"]); |
| 52 | + yield* runGit(cwd, ["config", "user.name", "Test"]); |
| 53 | + yield* runGit(cwd, ["config", "user.email", "[email protected]"]); |
| 54 | + const file = NodePath.join(cwd, "tracked.txt"); |
| 55 | + yield* fs.writeFileString(file, "initial\n"); |
| 56 | + yield* runGit(cwd, ["add", "."]); |
| 57 | + yield* runGit(cwd, ["commit", "-m", "initial"]); |
| 58 | + const head = (yield* runGit(cwd, ["rev-parse", "HEAD"])).trim(); |
| 59 | + yield* fs.writeFileString(file, "captured\n"); |
| 60 | + const store = yield* CheckpointStore.CheckpointStore; |
| 61 | + const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/activation/0"); |
| 62 | + yield* store.captureCheckpoint({ cwd, checkpointRef }); |
| 63 | + const captured = (yield* runGit(cwd, ["rev-parse", checkpointRef])).trim(); |
| 64 | + yield* createJjRepo(cwd); |
| 65 | + yield* fs.writeFileString(file, "later\n"); |
| 66 | + yield* store.restoreCheckpoint({ cwd, checkpointRef }); |
| 67 | + expect(yield* fs.readFileString(file)).toBe("captured\n"); |
| 68 | + expect((yield* runGit(cwd, ["rev-parse", "refs/heads/main"])).trim()).toBe(head); |
| 69 | + const visible = yield* runJj(cwd, [ |
| 70 | + "log", |
| 71 | + "--no-graph", |
| 72 | + "-r", |
| 73 | + "all()", |
| 74 | + "-T", |
| 75 | + 'commit_id ++ "\\n"', |
| 76 | + ]); |
| 77 | + expect(visible).not.toContain(captured); |
| 78 | + }), |
| 79 | + ); |
| 80 | + it.effect("returns true for a colocated Jujutsu repository", () => |
| 81 | + Effect.gen(function* () { |
| 82 | + const tmp = yield* makeTmpDir(); |
| 83 | + yield* createJjRepo(tmp); |
| 84 | + const checkpointStore = yield* CheckpointStore.CheckpointStore; |
| 85 | + expect(yield* checkpointStore.supportsCheckpoints(tmp)).toBe(true); |
| 86 | + }), |
| 87 | + ); |
| 88 | + it.effect("returns false for a Jujutsu repository with no colocated Git store", () => |
| 89 | + Effect.gen(function* () { |
| 90 | + const fileSystem = yield* FileSystem.FileSystem; |
| 91 | + const base = yield* makeTmpDir(); |
| 92 | + const store = NodePath.join(base, "store.git"); |
| 93 | + const root = NodePath.join(base, "project"); |
| 94 | + yield* fileSystem.makeDirectory(store, { recursive: true }); |
| 95 | + yield* fileSystem.makeDirectory(root, { recursive: true }); |
| 96 | + yield* runGit(store, ["init", "--bare", "--initial-branch=main", store]); |
| 97 | + yield* runJj(root, ["git", "init", "--git-repo", store]); |
| 98 | + const checkpointStore = yield* CheckpointStore.CheckpointStore; |
| 99 | + expect(yield* checkpointStore.supportsCheckpoints(root)).toBe(false); |
| 100 | + }), |
| 101 | + ); |
| 102 | + }); |
| 103 | +}); |
0 commit comments