99 lines
3.1 KiB
Go
99 lines
3.1 KiB
Go
package continuity
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"orchestra/internal/store"
|
|
)
|
|
|
|
func TestHandoffCASAndPickup(t *testing.T) {
|
|
root := t.TempDir()
|
|
run := func(a ...string) {
|
|
c := exec.Command("git", append([]string{"-C", root}, a...)...)
|
|
c.Env = append(os.Environ(), "GIT_AUTHOR_NAME=test", "GIT_AUTHOR_EMAIL=test@example", "GIT_COMMITTER_NAME=test", "GIT_COMMITTER_EMAIL=test@example")
|
|
if b, e := c.CombinedOutput(); e != nil {
|
|
t.Fatalf("git: %s %v", b, e)
|
|
}
|
|
}
|
|
os.WriteFile(filepath.Join(root, "TASK.md"), []byte("original"), 0644)
|
|
run("init")
|
|
run("add", "TASK.md")
|
|
run("commit", "-m", "init")
|
|
head := runOut(t, root, "rev-parse", "HEAD")
|
|
task := sha256.Sum256([]byte("original"))
|
|
s, _ := store.Open(t.TempDir())
|
|
h := Handoff{Meta: Meta{ID: "h1", Reason: "manual"}, Anchor: Anchor{GitSHA: head, Branch: "main"}, Goal: "ship", DoneWhen: []string{"tests pass"}, Action: "test", Command: "go test ./...", LastResult: Result{AtSHA: head}}
|
|
ref, e := Save(h, s)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
got, e := Load(ref, s)
|
|
if e != nil || got.Meta.ID != "h1" {
|
|
t.Fatalf("load: %v", e)
|
|
}
|
|
if e = ValidatePickup(root, got, hex.EncodeToString(task[:])); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
func runOut(t *testing.T, root string, a ...string) string {
|
|
b, e := exec.Command("git", append([]string{"-C", root}, a...)...).Output()
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
return string(b[:len(b)-1])
|
|
}
|
|
|
|
func TestDecodeRejectsUnknownKnowledgeFields(t *testing.T) {
|
|
_, e := Decode([]byte(`{"meta":{"id":"x","reason":"manual","rotation_index":0},"unknown":1}`))
|
|
if e == nil {
|
|
t.Fatal("expected strict schema error")
|
|
}
|
|
}
|
|
|
|
func TestScratchCommitProtectsTask(t *testing.T) {
|
|
root := t.TempDir()
|
|
run := func(a ...string) {
|
|
c := exec.Command("git", append([]string{"-C", root}, a...)...)
|
|
c.Env = append(os.Environ(), "GIT_AUTHOR_NAME=test", "GIT_AUTHOR_EMAIL=test@example", "GIT_COMMITTER_NAME=test", "GIT_COMMITTER_EMAIL=test@example")
|
|
if b, e := c.CombinedOutput(); e != nil {
|
|
t.Fatalf("git: %s %v", b, e)
|
|
}
|
|
}
|
|
os.WriteFile(filepath.Join(root, "TASK.md"), []byte("fixed"), 0644)
|
|
run("init")
|
|
run("add", ".")
|
|
run("commit", "-m", "init")
|
|
os.WriteFile(filepath.Join(root, "wip.txt"), []byte("wip"), 0644)
|
|
if err := ScratchCommit(root, "scratch/task", "wip"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(root, "TASK.md"), []byte("changed"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ScratchCommit(root, "scratch/other", "bad"); err == nil {
|
|
t.Fatal("expected immutable TASK.md rejection")
|
|
}
|
|
}
|
|
|
|
func TestVerifyTaskFileRejectsMutation(t *testing.T) {
|
|
root := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(root, "TASK.md"), []byte("task"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sum := sha256.Sum256([]byte("task"))
|
|
if err := VerifyTaskFile(root, hex.EncodeToString(sum[:])); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(root, "TASK.md"), []byte("changed"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := VerifyTaskFile(root, hex.EncodeToString(sum[:])); err == nil {
|
|
t.Fatal("expected immutable task check to fail")
|
|
}
|
|
}
|