|
| 1 | +package sidecar |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "gotest.tools/v3/assert" |
| 9 | + |
| 10 | + "github.com/CircleCI-Public/chunk-cli/internal/config" |
| 11 | +) |
| 12 | + |
| 13 | +func TestSaveAndLoadActiveSnapshot(t *testing.T) { |
| 14 | + dir := t.TempDir() |
| 15 | + t.Chdir(dir) |
| 16 | + |
| 17 | + want := ActiveSnapshot{ID: "snap-abc", Name: "my-snap"} |
| 18 | + assert.NilError(t, SaveActiveSnapshot(want)) |
| 19 | + |
| 20 | + got, err := LoadActiveSnapshot() |
| 21 | + assert.NilError(t, err) |
| 22 | + assert.Assert(t, got != nil, "expected non-nil ActiveSnapshot") |
| 23 | + assert.Equal(t, got.ID, want.ID) |
| 24 | + assert.Equal(t, got.Name, want.Name) |
| 25 | +} |
| 26 | + |
| 27 | +func TestLoadActiveSnapshotReturnsNilWhenMissing(t *testing.T) { |
| 28 | + dir := t.TempDir() |
| 29 | + t.Chdir(dir) |
| 30 | + |
| 31 | + got, err := LoadActiveSnapshot() |
| 32 | + assert.NilError(t, err) |
| 33 | + assert.Assert(t, got == nil, "expected nil when no snapshot file") |
| 34 | +} |
| 35 | + |
| 36 | +func TestClearActiveSnapshot(t *testing.T) { |
| 37 | + dir := t.TempDir() |
| 38 | + t.Chdir(dir) |
| 39 | + |
| 40 | + assert.NilError(t, SaveActiveSnapshot(ActiveSnapshot{ID: "snap-xyz"})) |
| 41 | + |
| 42 | + got, err := LoadActiveSnapshot() |
| 43 | + assert.NilError(t, err) |
| 44 | + assert.Assert(t, got != nil) |
| 45 | + |
| 46 | + assert.NilError(t, ClearActiveSnapshot()) |
| 47 | + |
| 48 | + got, err = LoadActiveSnapshot() |
| 49 | + assert.NilError(t, err) |
| 50 | + assert.Assert(t, got == nil) |
| 51 | +} |
| 52 | + |
| 53 | +func TestClearActiveSnapshotNoopWhenMissing(t *testing.T) { |
| 54 | + dir := t.TempDir() |
| 55 | + t.Chdir(dir) |
| 56 | + |
| 57 | + assert.NilError(t, ClearActiveSnapshot()) |
| 58 | +} |
| 59 | + |
| 60 | +func TestSnapshotSessionKeyed(t *testing.T) { |
| 61 | + dir := t.TempDir() |
| 62 | + t.Chdir(dir) |
| 63 | + |
| 64 | + // Save without a session — generic file. |
| 65 | + assert.NilError(t, SaveActiveSnapshot(ActiveSnapshot{ID: "snap-generic"})) |
| 66 | + |
| 67 | + // With a session ID set, load should return nil (isolated from the generic file). |
| 68 | + t.Setenv(config.EnvClaudeSession, "sess-abc") |
| 69 | + got, err := LoadActiveSnapshot() |
| 70 | + assert.NilError(t, err) |
| 71 | + assert.Assert(t, got == nil, "session-keyed load should not see generic file") |
| 72 | + |
| 73 | + // Save under the session. |
| 74 | + assert.NilError(t, SaveActiveSnapshot(ActiveSnapshot{ID: "snap-session"})) |
| 75 | + |
| 76 | + got, err = LoadActiveSnapshot() |
| 77 | + assert.NilError(t, err) |
| 78 | + assert.Assert(t, got != nil) |
| 79 | + assert.Equal(t, got.ID, "snap-session") |
| 80 | + |
| 81 | + // Without the session env var, the original generic file is still intact. |
| 82 | + t.Setenv(config.EnvClaudeSession, "") |
| 83 | + got, err = LoadActiveSnapshot() |
| 84 | + assert.NilError(t, err) |
| 85 | + assert.Assert(t, got != nil) |
| 86 | + assert.Equal(t, got.ID, "snap-generic") |
| 87 | +} |
| 88 | + |
| 89 | +func TestLoadActiveSnapshotWalksUpToParent(t *testing.T) { |
| 90 | + parent := t.TempDir() |
| 91 | + child := filepath.Join(parent, "sub", "dir") |
| 92 | + assert.NilError(t, os.MkdirAll(child, 0o755)) |
| 93 | + |
| 94 | + assert.NilError(t, os.MkdirAll(filepath.Join(parent, ".git"), 0o755)) |
| 95 | + assert.NilError(t, os.MkdirAll(filepath.Join(parent, ".chunk"), 0o755)) |
| 96 | + data := []byte(`{"id":"snap-parent","name":"parent-snap"}`) |
| 97 | + assert.NilError(t, os.WriteFile(filepath.Join(parent, ".chunk", "snapshot.json"), data, 0o644)) |
| 98 | + |
| 99 | + t.Chdir(child) |
| 100 | + |
| 101 | + got, err := LoadActiveSnapshot() |
| 102 | + assert.NilError(t, err) |
| 103 | + assert.Assert(t, got != nil) |
| 104 | + assert.Equal(t, got.ID, "snap-parent") |
| 105 | +} |
| 106 | + |
| 107 | +func TestLoadActiveSnapshotStopsAtGitRoot(t *testing.T) { |
| 108 | + grandparent := t.TempDir() |
| 109 | + parent := filepath.Join(grandparent, "repo") |
| 110 | + child := filepath.Join(parent, "sub") |
| 111 | + assert.NilError(t, os.MkdirAll(child, 0o755)) |
| 112 | + assert.NilError(t, os.MkdirAll(filepath.Join(parent, ".git"), 0o755)) |
| 113 | + assert.NilError(t, os.MkdirAll(filepath.Join(grandparent, ".chunk"), 0o755)) |
| 114 | + data := []byte(`{"id":"snap-grandparent"}`) |
| 115 | + assert.NilError(t, os.WriteFile(filepath.Join(grandparent, ".chunk", "snapshot.json"), data, 0o644)) |
| 116 | + |
| 117 | + t.Chdir(child) |
| 118 | + |
| 119 | + got, err := LoadActiveSnapshot() |
| 120 | + assert.NilError(t, err) |
| 121 | + assert.Assert(t, got == nil, "walk should not cross the git root boundary") |
| 122 | +} |
0 commit comments