|
1 | 1 | package checkpoint |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "context" |
| 6 | + "encoding/json" |
5 | 7 | "testing" |
6 | 8 |
|
7 | 9 | "github.com/go-git/go-git/v6" |
@@ -63,3 +65,99 @@ func TestWrite_ImportedSurfacesOnList(t *testing.T) { |
63 | 65 | t.Fatalf("expected 1 imported checkpoint, got %+v", infos) |
64 | 66 | } |
65 | 67 | } |
| 68 | + |
| 69 | +func TestImportedCheckpoint_CommitSHAPersisted(t *testing.T) { |
| 70 | + t.Parallel() |
| 71 | + tempDir := t.TempDir() |
| 72 | + testutil.InitRepo(t, tempDir) |
| 73 | + repo, err := git.PlainOpen(tempDir) |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("open repo: %v", err) |
| 76 | + } |
| 77 | + // Initial commit so HEAD exists. |
| 78 | + wt, err := repo.Worktree() |
| 79 | + if err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + testutil.WriteFile(t, tempDir, "f.txt", "x") |
| 83 | + if _, err := wt.Add("f.txt"); err != nil { |
| 84 | + t.Fatal(err) |
| 85 | + } |
| 86 | + if _, err := wt.Commit("init", &git.CommitOptions{ |
| 87 | + Author: &object.Signature{Name: "Test", Email: "test@test.com"}, |
| 88 | + }); err != nil { |
| 89 | + t.Fatal(err) |
| 90 | + } |
| 91 | + |
| 92 | + store := NewGitStore(repo, DefaultV1Refs()) |
| 93 | + red, err := redact.JSONLBytes([]byte(`{"type":"user","uuid":"u1","message":{"role":"user","content":"hi"}}` + "\n")) |
| 94 | + if err != nil { |
| 95 | + t.Fatal(err) |
| 96 | + } |
| 97 | + |
| 98 | + const commitSHA = "b01b59663fd4860fd15a9939499be44a14dbf168" |
| 99 | + ctx := context.Background() |
| 100 | + cid := id.MustCheckpointID("aabbccddeeff") |
| 101 | + err = store.Write(ctx, Session{ |
| 102 | + CheckpointID: cid, |
| 103 | + SessionID: "s1", |
| 104 | + Strategy: "import", |
| 105 | + Kind: "imported", |
| 106 | + Agent: agent.AgentTypeClaudeCode, |
| 107 | + Transcript: red, |
| 108 | + Prompts: []string{"hi"}, |
| 109 | + CheckpointsCount: 1, |
| 110 | + CommitSHA: commitSHA, |
| 111 | + }) |
| 112 | + if err != nil { |
| 113 | + t.Fatalf("write imported checkpoint: %v", err) |
| 114 | + } |
| 115 | + |
| 116 | + md, err := store.ReadSessionMetadata(ctx, cid, 0) |
| 117 | + if err != nil { |
| 118 | + t.Fatalf("read session metadata: %v", err) |
| 119 | + } |
| 120 | + if md.CommitSHA != commitSHA { |
| 121 | + t.Fatalf("expected session metadata commit_sha %q, got %q", commitSHA, md.CommitSHA) |
| 122 | + } |
| 123 | + |
| 124 | + summary, err := store.Read(ctx, cid) |
| 125 | + if err != nil { |
| 126 | + t.Fatalf("read checkpoint summary: %v", err) |
| 127 | + } |
| 128 | + if summary.CommitSHA != commitSHA { |
| 129 | + t.Fatalf("expected checkpoint summary commit_sha %q, got %q", commitSHA, summary.CommitSHA) |
| 130 | + } |
| 131 | + |
| 132 | + // omitempty guard: a checkpoint written without CommitSHA must not surface |
| 133 | + // "commit_sha" in the marshaled metadata at all. |
| 134 | + cid2 := id.MustCheckpointID("112233445566") |
| 135 | + err = store.Write(ctx, Session{ |
| 136 | + CheckpointID: cid2, |
| 137 | + SessionID: "s1", |
| 138 | + Strategy: "import", |
| 139 | + Kind: "imported", |
| 140 | + Agent: agent.AgentTypeClaudeCode, |
| 141 | + Transcript: red, |
| 142 | + Prompts: []string{"hi"}, |
| 143 | + CheckpointsCount: 1, |
| 144 | + }) |
| 145 | + if err != nil { |
| 146 | + t.Fatalf("write second imported checkpoint: %v", err) |
| 147 | + } |
| 148 | + |
| 149 | + md2, err := store.ReadSessionMetadata(ctx, cid2, 0) |
| 150 | + if err != nil { |
| 151 | + t.Fatalf("read second session metadata: %v", err) |
| 152 | + } |
| 153 | + if md2.CommitSHA != "" { |
| 154 | + t.Fatalf("expected empty commit_sha, got %q", md2.CommitSHA) |
| 155 | + } |
| 156 | + rawMD, err := json.Marshal(md2) |
| 157 | + if err != nil { |
| 158 | + t.Fatalf("marshal metadata: %v", err) |
| 159 | + } |
| 160 | + if bytes.Contains(rawMD, []byte(`"commit_sha"`)) { |
| 161 | + t.Fatalf("expected marshaled metadata to omit commit_sha when unset, got %s", rawMD) |
| 162 | + } |
| 163 | +} |
0 commit comments