Files
weichenandCursor e3b72ad64b fix(cli): require positive live-dev identity and exclusive backups
Live apply now accepts only :5433/agent_system_dev plus a data/dev
host root, maps stored work dirs fail-closed, and reports unique
O_EXCL backups after a parent-directory fsync.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-14 02:30:11 +00:00

139 lines
3.9 KiB
Go

package main
import "testing"
func TestValidateCLI_DefaultRefusesSharedDev(t *testing.T) {
err := validateCLI(cliConfig{
DSN: "postgres://app:x@127.0.0.1:5433/agent_system_dev?sslmode=disable",
RepoRoot: "/tmp/isolated",
})
if err == nil {
t.Fatal("default mode must refuse :5433")
}
err = validateCLI(cliConfig{
DSN: "postgres://app:x@127.0.0.1:55432/agent_itest?sslmode=disable",
RepoRoot: "/repo/data/dev",
})
if err == nil {
t.Fatal("default mode must refuse data/dev")
}
}
func validLiveApply() cliConfig {
return cliConfig{
DSN: "postgres://app:x@127.0.0.1:5433/agent_system_dev?sslmode=disable",
RepoRoot: "/repo/data/dev",
DocID: "doc_proj143_intake",
Apply: true,
Live: true,
Environment: "dev",
Confirm: confirmRepairSingleDev,
BackupDir: "/tmp/backup",
StoredDataRoot: "/app/data",
HostDataRoot: "/repo/data/dev",
}
}
func TestValidateCLI_LiveApplyRequiresConfirmAndDocID(t *testing.T) {
base := validLiveApply()
base.DocID = ""
if err := validateCLI(base); err == nil {
t.Fatal("live apply without --doc-id must fail")
}
base.DocID = "doc_proj143_intake"
base.Confirm = ""
if err := validateCLI(base); err == nil {
t.Fatal("live apply without confirm must fail")
}
base.Confirm = "wrong"
if err := validateCLI(base); err == nil {
t.Fatal("live apply with wrong confirm must fail")
}
base.Confirm = confirmRepairSingleDev
if err := validateCLI(base); err != nil {
t.Fatalf("expected live apply to pass: %v", err)
}
}
func TestValidateCLI_LiveRequiresPositiveDevDSNAndHostRoot(t *testing.T) {
cases := []struct {
name string
mut func(*cliConfig)
}{
{"port 5433 but wrong db", func(c *cliConfig) {
c.DSN = "postgres://app:x@127.0.0.1:5433/agent_itest?sslmode=disable"
}},
{"dev db name but isolated port", func(c *cliConfig) {
c.DSN = "postgres://app:x@127.0.0.1:55432/agent_system_dev?sslmode=disable"
}},
{"empty dsn env must not pass live", func(c *cliConfig) {
c.DSN = "postgres://app:x@127.0.0.1:55432/agent_itest?sslmode=disable"
}},
{"host root not data/dev", func(c *cliConfig) {
c.HostDataRoot = "/tmp/isolated-host"
}},
{"missing mapping roots", func(c *cliConfig) {
c.StoredDataRoot = ""
c.HostDataRoot = ""
}},
{"host root is data/test", func(c *cliConfig) {
c.HostDataRoot = "/repo/data/test"
}},
{"keyword dsn missing port", func(c *cliConfig) {
c.DSN = "host=127.0.0.1 dbname=agent_system_dev"
}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
cfg := validLiveApply()
tc.mut(&cfg)
if err := validateCLI(cfg); err == nil {
t.Fatalf("%s: live must fail closed", tc.name)
}
})
}
}
func TestValidateCLI_LiveKeywordDSNAccepted(t *testing.T) {
cfg := validLiveApply()
cfg.DSN = "host=127.0.0.1 port=5433 dbname=agent_system_dev sslmode=disable"
if err := validateCLI(cfg); err != nil {
t.Fatal(err)
}
}
func TestValidateCLI_LiveForbidsBatchAndNonDev(t *testing.T) {
err := validateCLI(cliConfig{
DSN: "postgres://app:x@127.0.0.1:5434/agent_system_test?sslmode=disable",
Live: true,
Environment: "dev",
DocID: "doc_x",
StoredDataRoot: "/app/data",
HostDataRoot: "/repo/data/dev",
})
if err == nil {
t.Fatal("live mode must refuse :5434")
}
err = validateCLI(cliConfig{
DSN: "postgres://app:x@127.0.0.1:5433/agent_system_dev?sslmode=disable",
Live: true,
Environment: "test",
DocID: "doc_x",
})
if err == nil {
t.Fatal("live mode must refuse non-dev environment")
}
}
func TestValidateCLI_IsolatedDefaultAllowsApply(t *testing.T) {
err := validateCLI(cliConfig{
DSN: "postgres://app:x@127.0.0.1:55432/agent_itest?sslmode=disable",
RepoRoot: "/tmp/isolated-root",
Apply: true,
BackupDir: "/tmp/backup",
})
if err != nil {
t.Fatalf("isolated apply should pass: %v", err)
}
}