Files
orchestra/internal/continuity/continuity_test.go
T

120 lines
3.9 KiB
Go

package continuity
import (
"crypto/sha256"
"encoding/hex"
"os"
"os/exec"
"path/filepath"
"strings"
"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"}, Action: "run the focused tests", Command: "go test ./..."}
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 TestHandoffRejectsFabricatedOrProseAuthoredFields(t *testing.T) {
base := Handoff{Meta: Meta{ID: "h", Reason: "manual"}, Anchor: Anchor{GitSHA: strings.Repeat("a", 40), Branch: "main"}, Action: "run the focused tests"}
if err := base.Validate(); err != nil {
t.Fatal(err)
}
badAction := base
badAction.Action = "Continue the task from the handoff"
if err := badAction.Validate(); err == nil {
t.Fatal("expected circular action rejection")
}
badProse := base
badProse.Remaining = []string{"short\n# markdown heading"}
if err := badProse.Validate(); err == nil {
t.Fatal("expected prose-in-list rejection")
}
if _, err := Decode([]byte(`{"meta":{"id":"h","reason":"manual","rotation_index":0},"anchor":{"git_sha":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","branch":"main"},"goal":"fabricated","action":"run tests"}`)); err == nil {
t.Fatal("expected goal rejection")
}
}
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")
}
}