|
| 1 | +package checkpoint |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + git "github.com/go-git/go-git/v6" |
| 10 | + "github.com/go-git/go-git/v6/plumbing" |
| 11 | + "github.com/go-git/go-git/v6/plumbing/object" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/entireio/cli/cmd/entire/cli/checkpoint/id" |
| 16 | + "github.com/entireio/cli/cmd/entire/cli/paths" |
| 17 | + "github.com/entireio/cli/cmd/entire/cli/testutil" |
| 18 | + "github.com/entireio/cli/redact" |
| 19 | +) |
| 20 | + |
| 21 | +// newTestRepo creates an isolated repo with a single "init" commit and returns |
| 22 | +// its directory, an open handle, and the commit hash. |
| 23 | +func newTestRepo(t *testing.T) (string, *git.Repository, plumbing.Hash) { |
| 24 | + t.Helper() |
| 25 | + dir := t.TempDir() |
| 26 | + testutil.InitRepo(t, dir) |
| 27 | + repo, err := git.PlainOpen(dir) |
| 28 | + require.NoError(t, err) |
| 29 | + return dir, repo, commitFile(t, repo, dir, "f.txt", "init", "init") |
| 30 | +} |
| 31 | + |
| 32 | +// commitFile commits content to path; successive calls build a linear chain. |
| 33 | +func commitFile(t *testing.T, repo *git.Repository, dir, path, content, msg string) plumbing.Hash { |
| 34 | + t.Helper() |
| 35 | + require.NoError(t, os.WriteFile(filepath.Join(dir, path), []byte(content), 0o644)) |
| 36 | + wt, err := repo.Worktree() |
| 37 | + require.NoError(t, err) |
| 38 | + _, err = wt.Add(path) |
| 39 | + require.NoError(t, err) |
| 40 | + h, err := wt.Commit(msg, &git.CommitOptions{Author: &object.Signature{Name: "Test", Email: "test@test.com"}}) |
| 41 | + require.NoError(t, err) |
| 42 | + return h |
| 43 | +} |
| 44 | + |
| 45 | +func setRef(t *testing.T, repo *git.Repository, name plumbing.ReferenceName, hash plumbing.Hash) { |
| 46 | + t.Helper() |
| 47 | + require.NoError(t, repo.Storer.SetReference(plumbing.NewHashReference(name, hash))) |
| 48 | +} |
| 49 | + |
| 50 | +func v1BranchRef() plumbing.ReferenceName { |
| 51 | + return plumbing.NewBranchReferenceName(paths.MetadataBranchName) |
| 52 | +} |
| 53 | +func customRef() plumbing.ReferenceName { return plumbing.ReferenceName(paths.MetadataRefName) } |
| 54 | +func originV1Ref() plumbing.ReferenceName { |
| 55 | + return plumbing.NewRemoteReferenceName("origin", paths.MetadataBranchName) |
| 56 | +} |
| 57 | + |
| 58 | +func customRefHash(t *testing.T, repo *git.Repository) (plumbing.Hash, bool) { |
| 59 | + t.Helper() |
| 60 | + ref, err := repo.Reference(customRef(), true) |
| 61 | + if err != nil { |
| 62 | + return plumbing.ZeroHash, false |
| 63 | + } |
| 64 | + return ref.Hash(), true |
| 65 | +} |
| 66 | + |
| 67 | +// writeV1Checkpoint writes a committed checkpoint to the v1 branch. |
| 68 | +func writeV1Checkpoint(t *testing.T, repo *git.Repository, cpID id.CheckpointID) { |
| 69 | + t.Helper() |
| 70 | + require.NoError(t, NewGitStore(repo).WriteCommitted(context.Background(), WriteCommittedOptions{ |
| 71 | + CheckpointID: cpID, |
| 72 | + SessionID: "session", |
| 73 | + Strategy: "manual-commit", |
| 74 | + Transcript: redact.AlreadyRedacted([]byte("transcript\n")), |
| 75 | + Prompts: []string{"prompt"}, |
| 76 | + AuthorName: "Test", |
| 77 | + AuthorEmail: "test@test.com", |
| 78 | + })) |
| 79 | +} |
| 80 | + |
| 81 | +// enableV11 chdirs into dir and opts into checkpoints v1.1. |
| 82 | +func enableV11(t *testing.T, dir string) { |
| 83 | + t.Helper() |
| 84 | + t.Chdir(dir) |
| 85 | + writeSettings(t, dir, `"1.1"`) |
| 86 | +} |
| 87 | + |
| 88 | +// writeSettings writes .entire/settings.json (empty version omits the option). |
| 89 | +func writeSettings(t *testing.T, dir, version string) { |
| 90 | + t.Helper() |
| 91 | + body := `{"enabled": true}` |
| 92 | + if version != "" { |
| 93 | + body = `{"enabled": true, "strategy_options": {"checkpoints_version": ` + version + `}}` |
| 94 | + } |
| 95 | + require.NoError(t, os.MkdirAll(filepath.Join(dir, ".entire"), 0o755)) |
| 96 | + require.NoError(t, os.WriteFile(filepath.Join(dir, ".entire", paths.SettingsFileName), []byte(body), 0o644)) |
| 97 | +} |
| 98 | + |
| 99 | +// blockCustomRefWrite occupies refs/entire with a file so refs/entire/* writes fail. |
| 100 | +func blockCustomRefWrite(t *testing.T, dir string) { |
| 101 | + t.Helper() |
| 102 | + require.NoError(t, os.WriteFile(filepath.Join(dir, ".git", "refs", "entire"), []byte("blocked"), 0o644)) |
| 103 | +} |
| 104 | + |
| 105 | +func TestGitStore_CommittedReadRef(t *testing.T) { |
| 106 | + t.Parallel() |
| 107 | + assert.Equal(t, v1BranchRef(), NewGitStore(nil).CommittedReadRef()) |
| 108 | + assert.Equal(t, customRef(), NewGitStoreWithRef(nil, customRef()).CommittedReadRef()) |
| 109 | +} |
| 110 | + |
| 111 | +func TestSyncV1CustomRefForRead(t *testing.T) { |
| 112 | + t.Parallel() |
| 113 | + tests := []struct { |
| 114 | + name string |
| 115 | + setup func(t *testing.T, dir string, repo *git.Repository, init plumbing.Hash) (want plumbing.Hash, exists bool) |
| 116 | + }{ |
| 117 | + {"seeds from local v1 when missing", func(t *testing.T, _ string, repo *git.Repository, init plumbing.Hash) (plumbing.Hash, bool) { |
| 118 | + setRef(t, repo, v1BranchRef(), init) |
| 119 | + return init, true |
| 120 | + }}, |
| 121 | + {"seeds from origin when local v1 missing", func(t *testing.T, _ string, repo *git.Repository, init plumbing.Hash) (plumbing.Hash, bool) { |
| 122 | + setRef(t, repo, originV1Ref(), init) |
| 123 | + return init, true |
| 124 | + }}, |
| 125 | + {"no-op when equal", func(t *testing.T, _ string, repo *git.Repository, init plumbing.Hash) (plumbing.Hash, bool) { |
| 126 | + setRef(t, repo, v1BranchRef(), init) |
| 127 | + setRef(t, repo, customRef(), init) |
| 128 | + return init, true |
| 129 | + }}, |
| 130 | + {"advances when ancestor", func(t *testing.T, dir string, repo *git.Repository, init plumbing.Hash) (plumbing.Hash, bool) { |
| 131 | + setRef(t, repo, customRef(), init) |
| 132 | + newHash := commitFile(t, repo, dir, "f2.txt", "more", "second") |
| 133 | + setRef(t, repo, v1BranchRef(), newHash) |
| 134 | + return newHash, true |
| 135 | + }}, |
| 136 | + {"leaves non-ancestor ref", func(t *testing.T, dir string, repo *git.Repository, init plumbing.Hash) (plumbing.Hash, bool) { |
| 137 | + ahead := commitFile(t, repo, dir, "f2.txt", "more", "second") |
| 138 | + setRef(t, repo, v1BranchRef(), init) // parent |
| 139 | + setRef(t, repo, customRef(), ahead) // child, not an ancestor of v1 |
| 140 | + return ahead, true |
| 141 | + }}, |
| 142 | + {"no v1 tip", func(_ *testing.T, _ string, _ *git.Repository, _ plumbing.Hash) (plumbing.Hash, bool) { |
| 143 | + return plumbing.ZeroHash, false |
| 144 | + }}, |
| 145 | + {"write failure leaves ref unset", func(t *testing.T, dir string, repo *git.Repository, init plumbing.Hash) (plumbing.Hash, bool) { |
| 146 | + setRef(t, repo, v1BranchRef(), init) |
| 147 | + blockCustomRefWrite(t, dir) |
| 148 | + return plumbing.ZeroHash, false |
| 149 | + }}, |
| 150 | + } |
| 151 | + for _, tt := range tests { |
| 152 | + t.Run(tt.name, func(t *testing.T) { |
| 153 | + t.Parallel() |
| 154 | + dir, repo, init := newTestRepo(t) |
| 155 | + want, exists := tt.setup(t, dir, repo, init) |
| 156 | + |
| 157 | + syncV1CustomRefForRead(context.Background(), repo) |
| 158 | + |
| 159 | + got, ok := customRefHash(t, repo) |
| 160 | + require.Equal(t, exists, ok) |
| 161 | + if exists { |
| 162 | + assert.Equal(t, want, got) |
| 163 | + } |
| 164 | + }) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +// Not parallel: uses t.Chdir() so settings.Load resolves the test repo. |
| 169 | +func TestNewCommittedReadStore_SelectsRefByVersion(t *testing.T) { |
| 170 | + dir, repo, h := newTestRepo(t) |
| 171 | + setRef(t, repo, v1BranchRef(), h) |
| 172 | + t.Chdir(dir) |
| 173 | + |
| 174 | + writeSettings(t, dir, "") // v1 only |
| 175 | + assert.Equal(t, v1BranchRef(), NewCommittedReadStore(context.Background(), repo).CommittedReadRef()) |
| 176 | + |
| 177 | + writeSettings(t, dir, `"1.1"`) |
| 178 | + assert.Equal(t, customRef(), NewCommittedReadStore(context.Background(), repo).CommittedReadRef()) |
| 179 | +} |
| 180 | + |
| 181 | +// v1.1 reads always go through the custom ref (no v1 fallback): a checkpoint is |
| 182 | +// found when the ref can be synced to v1, and not found when it can't. |
| 183 | +// Not parallel: subtests use t.Chdir(). |
| 184 | +func TestNewCommittedReadStore_V11Reads(t *testing.T) { |
| 185 | + tests := []struct { |
| 186 | + name string |
| 187 | + mutate func(t *testing.T, dir string, repo *git.Repository) |
| 188 | + wantFound bool |
| 189 | + }{ |
| 190 | + {"reads v1 data via custom ref", func(_ *testing.T, _ string, _ *git.Repository) {}, true}, |
| 191 | + {"reads remote-only metadata", func(t *testing.T, _ string, repo *git.Repository) { |
| 192 | + ref, err := repo.Reference(v1BranchRef(), true) |
| 193 | + require.NoError(t, err) |
| 194 | + setRef(t, repo, originV1Ref(), ref.Hash()) |
| 195 | + require.NoError(t, repo.Storer.RemoveReference(v1BranchRef())) |
| 196 | + }, true}, |
| 197 | + {"sync write fails", func(t *testing.T, dir string, _ *git.Repository) { |
| 198 | + blockCustomRefWrite(t, dir) |
| 199 | + }, false}, |
| 200 | + {"custom ref diverges", func(t *testing.T, dir string, repo *git.Repository) { |
| 201 | + setRef(t, repo, customRef(), commitFile(t, repo, dir, "other.txt", "diverged", "diverged")) |
| 202 | + }, false}, |
| 203 | + } |
| 204 | + for _, tt := range tests { |
| 205 | + t.Run(tt.name, func(t *testing.T) { |
| 206 | + dir, repo, _ := newTestRepo(t) |
| 207 | + enableV11(t, dir) |
| 208 | + cpID := id.MustCheckpointID("a1b2c3d4e5f6") |
| 209 | + writeV1Checkpoint(t, repo, cpID) |
| 210 | + tt.mutate(t, dir, repo) |
| 211 | + |
| 212 | + SyncCommittedReadRef(context.Background(), repo) |
| 213 | + store := NewCommittedReadStore(context.Background(), repo) |
| 214 | + require.Equal(t, customRef(), store.CommittedReadRef(), "must read the custom ref, not fall back to v1") |
| 215 | + |
| 216 | + summary, err := store.ReadCommitted(context.Background(), cpID) |
| 217 | + require.NoError(t, err) |
| 218 | + if tt.wantFound { |
| 219 | + require.NotNil(t, summary) |
| 220 | + assert.Equal(t, cpID, summary.CheckpointID) |
| 221 | + } else { |
| 222 | + assert.Nil(t, summary, "must not fall back to v1") |
| 223 | + } |
| 224 | + }) |
| 225 | + } |
| 226 | +} |
0 commit comments