Files
Agent-System-Backend/internal/store/account_cursor_key_test.go
T
weichen adcd8fefd2 Fence U3 account writes and stabilize directory cursors
Why: Account disable could be followed by late Backend business writes, API key creation could implicitly create users, account bootstrap locks could invert, and directory cursors were process-local or leaked out-of-scope changes.

Evidence: Local go test ./... and isolated PostgreSQL package suites passed on the staged candidate; exact committed-head SELF replay and stage 6 human review remain pending.

Impact: Adds final account transactions and external-effect checks across HTTP, Host, Bus and run manager; tightens API key issuance and persists versioned cursor signing. Delegated RDM rename still needs the separately proposed real-operator contract, so A1 and stage 6 are not complete.

Refs: Documents U3 r1, approved A1-A4 amendment fb3a821, pending delegated-projection amendment 5783940, Dev feature/iam-u3-account-directory-recovery.
2026-10-14 07:33:54 +00:00

94 lines
2.4 KiB
Go

package store
import (
"os"
"path/filepath"
"sync"
"testing"
)
func TestAccountCursorKeySharedAcrossStoreOpens(t *testing.T) {
path := filepath.Join(t.TempDir(), "accounts.db")
first, err := OpenSQLite(path)
if err != nil {
t.Fatal(err)
}
defer first.Close()
second, err := OpenExistingSQLite(path)
if err != nil {
t.Fatal(err)
}
defer second.Close()
key1, err := first.GetAccountCursorKey()
if err != nil {
t.Fatal(err)
}
key2, err := second.GetAccountCursorKey()
if err != nil {
t.Fatal(err)
}
if key1.Version != 1 || key1 != key2 || key1.Secret == ([32]byte{}) {
t.Fatal("distinct instances did not share a nonzero versioned key")
}
}
func TestAccountCursorKeyMigrationAddsToExistingU3Schema(t *testing.T) {
path := filepath.Join(t.TempDir(), "accounts.db")
st, err := OpenSQLite(path)
if err != nil {
t.Fatal(err)
}
if err := st.CreateAccount("existing", "Existing", "", false); err != nil {
t.Fatal(err)
}
if err := st.MigrateDown(33); err != nil {
t.Fatal(err)
}
st.Close()
st, err = OpenSQLite(path)
if err != nil {
t.Fatal(err)
}
defer st.Close()
if _, err := st.GetAccountByOwner("existing"); err != nil {
t.Fatalf("v34 migration lost existing account: %v", err)
}
if key, err := st.GetAccountCursorKey(); err != nil || key.Version != 1 {
t.Fatalf("v34 migration failed to initialize key: %+v %v", key.Version, err)
}
}
func TestPGAccountCursorKeyConcurrentInstances(t *testing.T) {
baseDSN := os.Getenv("AGENT_PG_TEST_DSN")
if baseDSN == "" {
t.Skip("isolated PostgreSQL test DSN required")
}
first := openTestStore(t)
if err := first.EnsureSystemIdentity("cursor-key-test", "pg"); err != nil {
t.Fatal(err)
}
var schema string
if err := first.DB().QueryRow(`SELECT current_schema()`).Scan(&schema); err != nil {
t.Fatal(err)
}
second, err := OpenExistingPostgres(withTestSearchPath(baseDSN, schema), filepath.Join(t.TempDir(), "artifacts"))
if err != nil {
t.Fatal(err)
}
defer second.Close()
var keys [2]AccountCursorKey
var failures [2]error
var wg sync.WaitGroup
for i, st := range []*Store{first, second} {
wg.Add(1)
go func() {
defer wg.Done()
keys[i], failures[i] = st.GetAccountCursorKey()
}()
}
wg.Wait()
if failures[0] != nil || failures[1] != nil || keys[0] != keys[1] || keys[0].Version != 1 || keys[0].Secret == ([32]byte{}) {
t.Fatalf("concurrent instances did not converge: %v %v", failures[0], failures[1])
}
}