11
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
package store_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"superwork-tui/internal/store"
|
||||
)
|
||||
|
||||
func TestReadConfirmed_MissingFile(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
got, err := store.ReadConfirmed(root, 12, "abc")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Errorf("got %v, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteConfirmed_Roundtrip(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
want := []string{"src/a.go", "src/b.go"}
|
||||
if err := store.WriteConfirmed(root, 12, "abc", want); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
got, err := store.ReadConfirmed(root, 12, "abc")
|
||||
if err != nil {
|
||||
t.Fatalf("read: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteConfirmed_EmptyDeletesKey(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := store.WriteConfirmed(root, 12, "abc", []string{"x.go"}); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
if err := store.WriteConfirmed(root, 12, "abc", nil); err != nil {
|
||||
t.Fatalf("write empty: %v", err)
|
||||
}
|
||||
m := readRawMap(t, root)
|
||||
if _, ok := m["12:abc"]; ok {
|
||||
t.Errorf("key 12:abc still present after empty write: %v", m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeyConvention(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := store.WriteConfirmed(root, 7, "deadbeef", []string{"f.go"}); err != nil {
|
||||
t.Fatalf("write file: %v", err)
|
||||
}
|
||||
if err := store.WriteConfirmedCommits(root, 7, []string{"deadbeef", "cafe"}); err != nil {
|
||||
t.Fatalf("write commits: %v", err)
|
||||
}
|
||||
m := readRawMap(t, root)
|
||||
if _, ok := m["7:deadbeef"]; !ok {
|
||||
t.Errorf("missing file-level key 7:deadbeef: %v", m)
|
||||
}
|
||||
if _, ok := m["7:commits"]; !ok {
|
||||
t.Errorf("missing commit-level key 7:commits: %v", m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfirmedCommits_Roundtrip(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
want := []string{"sha1", "sha2"}
|
||||
if err := store.WriteConfirmedCommits(root, 9, want); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
got, err := store.ReadConfirmedCommits(root, 9)
|
||||
if err != nil {
|
||||
t.Fatalf("read: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfirmedCommits_EmptyDeletesKey(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := store.WriteConfirmedCommits(root, 9, []string{"sha1"}); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
if err := store.WriteConfirmedCommits(root, 9, nil); err != nil {
|
||||
t.Fatalf("write empty: %v", err)
|
||||
}
|
||||
m := readRawMap(t, root)
|
||||
if _, ok := m["9:commits"]; ok {
|
||||
t.Errorf("key 9:commits still present after empty write: %v", m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadConfirmed_MalformedFile(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
spx := filepath.Join(root, ".spx")
|
||||
if err := os.MkdirAll(spx, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(spx, "pr-review-confirmed.json"), []byte("not json"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := store.ReadConfirmed(root, 1, "x")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Errorf("got %v, want empty for malformed file", got)
|
||||
}
|
||||
}
|
||||
|
||||
func readRawMap(t *testing.T, root string) map[string][]string {
|
||||
t.Helper()
|
||||
raw, err := os.ReadFile(filepath.Join(root, ".spx", "pr-review-confirmed.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("read raw: %v", err)
|
||||
}
|
||||
var m map[string][]string
|
||||
if err := json.Unmarshal(raw, &m); err != nil {
|
||||
t.Fatalf("unmarshal raw: %v", err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
Reference in New Issue
Block a user