57c028f94f
The plan artifact was Changes{Target,Intent} plus three string lists, every
entry capped at 500 single-line characters. That bound makes a specification
impossible: a phase cannot carry a code block, a paragraph of reasoning, or a
verification command with its own argument list. renderSealed then flattened
what little survived through collapse(), so an implement session received a
summary of a summary.
plan.md replaces it. Markdown, 128 KiB, no per-line cap, sealed through the
existing path under the existing PlanRef. The parser enforces the structure the
brief states: required sections, phases numbered from 1 with no gaps, Files,
Changes and Verification per phase, and at least one automated or manual check,
because a phase nobody can verify can never be established as done. Automated
entries are JSON argv arrays, so a pipe is a literal argument rather than an
operator. Headings inside fenced blocks are content, so a plan may show
markdown without parsing its own example.
Citations resolve at seal time against the accepted research, on the
coordinator, which is the only party holding ResearchRef. A plan resting on a
finding nobody recorded fails on the planner while its session is still alive
to be told.
The plan now renders byte for byte into the implement launch, and a rotated
successor receives the same complete document. That is the property the whole
change exists for. collapse() stays for research findings, which really are
short claims.
DecodeStoredPlan reads pre-markdown refs and renders them into the same type,
labelled, so nothing downstream branches on which era a plan came from. A
legacy plan carries no phases, which is honest: the old artifact never named an
executable unit.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CVbaKucEYBjMqVeUgJUsc1
117 lines
3.7 KiB
Go
117 lines
3.7 KiB
Go
package authn
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
func openTestStore(t *testing.T) (*Store, string) {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), DatabaseFile)
|
|
store, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = store.Close() })
|
|
return store, path
|
|
}
|
|
|
|
func TestPasswordRecordPersistsAndAuthenticates(t *testing.T) {
|
|
store, path := openTestStore(t)
|
|
created, wasCreated, err := store.SetPassword("Kami", "correct horse battery")
|
|
if err != nil || !wasCreated || created.Username != "Kami" {
|
|
t.Fatalf("created=%+v new=%v err=%v", created, wasCreated, err)
|
|
}
|
|
if _, err := store.Authenticate("KAMI", "correct horse battery"); err != nil {
|
|
t.Fatalf("authenticate: %v", err)
|
|
}
|
|
if _, err := store.Authenticate("Kami", "wrong password"); !errors.Is(err, ErrInvalidCredentials) {
|
|
t.Fatalf("wrong password error = %v", err)
|
|
}
|
|
if err := store.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reopened, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer reopened.Close()
|
|
if _, err := reopened.Authenticate("kami", "correct horse battery"); err != nil {
|
|
t.Fatalf("persisted authentication: %v", err)
|
|
}
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := info.Mode().Perm(); got != databaseFileMode {
|
|
t.Fatalf("auth database permissions = %o, want %o", got, databaseFileMode)
|
|
}
|
|
}
|
|
|
|
func TestUpdateRequiresCurrentPasswordAndMovesUsername(t *testing.T) {
|
|
store, _ := openTestStore(t)
|
|
if _, _, err := store.SetPassword("operator", "original password"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := store.Update("operator", "wrong password", "kami", "replacement password"); !errors.Is(err, ErrInvalidCredentials) {
|
|
t.Fatalf("wrong current password error = %v", err)
|
|
}
|
|
updated, err := store.Update("operator", "original password", "kami", "replacement password")
|
|
if err != nil || updated.Username != "kami" {
|
|
t.Fatalf("updated=%+v err=%v", updated, err)
|
|
}
|
|
if _, err := store.Authenticate("operator", "original password"); !errors.Is(err, ErrInvalidCredentials) {
|
|
t.Fatalf("old credential error = %v", err)
|
|
}
|
|
if _, err := store.Authenticate("kami", "replacement password"); err != nil {
|
|
t.Fatalf("new credential: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestUpdateRefusesExistingUsername(t *testing.T) {
|
|
store, _ := openTestStore(t)
|
|
if _, _, err := store.SetPassword("one", "password one"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := store.SetPassword("two", "password two"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := store.Update("one", "password one", "TWO", ""); !errors.Is(err, ErrUsernameExists) {
|
|
t.Fatalf("collision error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLegacyHashImportsOnlyIntoEmptyDatabase(t *testing.T) {
|
|
store, _ := openTestStore(t)
|
|
hash, err := bcrypt.GenerateFromPassword([]byte("legacy password"), bcrypt.MinCost)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if imported, err := store.ImportBcrypt("legacy", string(hash)); err != nil || !imported {
|
|
t.Fatalf("imported=%v err=%v", imported, err)
|
|
}
|
|
if imported, err := store.ImportBcrypt("intruder", string(hash)); err != nil || imported {
|
|
t.Fatalf("second import=%v err=%v", imported, err)
|
|
}
|
|
if _, err := store.Authenticate("legacy", "legacy password"); err != nil {
|
|
t.Fatalf("imported credential: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCredentialValidation(t *testing.T) {
|
|
store, _ := openTestStore(t)
|
|
if _, _, err := store.SetPassword("", "a sufficiently long password"); err == nil {
|
|
t.Fatal("blank username accepted")
|
|
}
|
|
if _, _, err := store.SetPassword("operator", "short"); err == nil {
|
|
t.Fatal("short password accepted")
|
|
}
|
|
if _, _, err := store.SetPassword("operator", string(make([]byte, maximumPassword+1))); err == nil {
|
|
t.Fatal("oversized bcrypt password accepted")
|
|
}
|
|
}
|