package forge import ( "context" "errors" "fmt" "os" "path/filepath" "testing" "time" "amadan.net/oficina/equipo/internal/equipo/team" "amadan.net/oficina/equipo/internal/runnerproto" ) func assertNoEffect(t *testing.T, f *fixture, err error) { t.Helper() if !errors.Is(err, team.ErrNotDispatched) || errors.Is(err, team.ErrUncertain) || f.pushes != 0 { t.Fatalf("want definitive no dispatch, got %v; pushes=%d", err, f.pushes) } r, err := f.p.Reconcile(ctx, f.auth.Ticket.Dispatch) if !errors.Is(err, team.ErrNotDispatched) || r.Receipt != nil { t.Fatalf("reconcile did not confirm no dispatch: %+v %v", r, err) } } func TestPublishNoEffect(t *testing.T) { for _, name := range []string{"authorize", "validate", "revalidate", "credential", "mkdir", "export", "init", "rev-parse", "expired_export", "journal", "query", "final_authority", "final_validate", "final_objects", "description", "cancel_description", "push_marker", "context"} { t.Run(name, func(t *testing.T) { f := newFixture(t) cleanup := func() {} switch name { case "authorize": f.p.cfg.Authorize = func(context.Context, team.Dispatch) (Authorization, error) { return Authorization{}, team.ErrFence } case "validate": f.auth.Ticket.Incarnation++ case "revalidate": f.p.cfg.Revalidate = func(context.Context, Authorization) error { return team.ErrFence } case "credential": f.p.cfg.Credential = func(context.Context, string) (Credential, error) { return Credential{}, team.ErrUncertain } case "mkdir": original := f.p.cfg.Credential f.p.cfg.Credential = func(c context.Context, s string) (Credential, error) { must(t, os.Rename(f.p.cfg.Root, f.p.cfg.Root+"-saved")) must(t, os.WriteFile(f.p.cfg.Root, nil, 0600)) return original(c, s) } cleanup = func() { must(t, os.Remove(f.p.cfg.Root)); must(t, os.Rename(f.p.cfg.Root+"-saved", f.p.cfg.Root)) } case "export": f.p.cfg.Export = func(context.Context, team.GitCandidate, string) error { return team.ErrUncertain } case "init", "rev-parse": wrapper := filepath.Join(f.root, "git-fail") must(t, os.WriteFile(wrapper, []byte(fmt.Sprintf("#!/bin/sh\nfor arg do\n if [ \"$arg\" = %s ]; then exit 79; fi\ndone\nexec /usr/bin/git \"$@\"\n", name)), 0700)) f.p.cfg.GitPath = wrapper case "expired_export": original := f.p.cfg.Export f.p.cfg.Export = func(c context.Context, g team.GitCandidate, s string) error { e := original(c, g, s) f.p.cfg.Clock = func() time.Time { return f.auth.Ticket.ExpiresAt } return e } case "journal": original := f.p.cfg.Export f.p.cfg.Export = func(c context.Context, g team.GitCandidate, s string) error { e := original(c, g, s) must(t, os.Mkdir(f.p.path(f.auth.Ticket.Dispatch), 0700)) return e } cleanup = func() { must(t, os.Remove(f.p.path(f.auth.Ticket.Dispatch))) } case "query": must(t, os.Rename(f.remote, f.remote+"-hidden")) case "final_authority": calls := 0 f.p.cfg.Revalidate = func(context.Context, Authorization) error { calls++ if calls > 1 { return team.ErrFence } return nil } case "final_validate": original := f.p.localRemote f.p.localRemote = func(s string) string { f.p.cfg.Clock = func() time.Time { return f.auth.Ticket.ExpiresAt } return original(s) } case "final_objects": original := f.p.localRemote f.p.localRemote = func(s string) string { dirs, e := filepath.Glob(filepath.Join(f.p.cfg.Root, "objects-*", "git", "objects")) must(t, e) for _, dir := range dirs { must(t, os.RemoveAll(dir)) } return original(s) } case "description": f.desc.fail = true case "cancel_description": f.desc.before = func() { f.cancelled = true } case "push_marker": f.desc.before = func() { path := f.p.path(f.auth.Ticket.Dispatch) must(t, os.Rename(path, path+"-saved")) must(t, os.Mkdir(path, 0700)) } cleanup = func() { path := f.p.path(f.auth.Ticket.Dispatch) must(t, os.Remove(path)) must(t, os.Rename(path+"-saved", path)) } } callCtx := ctx if name == "context" { var cancel context.CancelFunc callCtx, cancel = context.WithCancel(ctx) cancel() } _, err := f.p.Execute(callCtx, f.auth.Ticket.Dispatch) cleanup() if name == "description" || name == "cancel_description" || name == "push_marker" { if !errors.Is(err, team.ErrUncertain) { t.Fatal("post-description failure claimed no effect", err) } if _, e := f.p.Reconcile(ctx, f.auth.Ticket.Dispatch); !errors.Is(e, team.ErrUncertain) { t.Fatal("description uncertainty lost", e) } return } if name == "expired_export" { if _, e := os.Stat(f.p.path(f.auth.Ticket.Dispatch)); !os.IsNotExist(e) { t.Fatal("expired export wrote a journal") } } assertNoEffect(t, f, err) if name == "final_authority" || name == "final_validate" || name == "final_objects" { if f.desc.calls != 0 { t.Fatal("description changed before final authority and object checks") } } }) } } func TestPublishMissingJournal(t *testing.T) { f := newFixture(t) r, err := f.p.Reconcile(ctx, f.auth.Ticket.Dispatch) if !errors.Is(err, team.ErrNotDispatched) || r.Receipt != nil { t.Fatalf("missing journal is no dispatch: %+v %v", r, err) } } func TestPublishIndependentActionLocks(t *testing.T) { a := newFixture(t) _, err := a.p.Execute(ctx, a.auth.Ticket.Dispatch) must(t, err) entered, release, done := make(chan struct{}), make(chan struct{}), make(chan error, 1) credential := a.p.cfg.Credential a.p.cfg.Credential = func(c context.Context, s string) (Credential, error) { close(entered) <-release return credential(c, s) } go func() { _, e := a.p.Reconcile(ctx, a.auth.Ticket.Dispatch); done <- e }() <-entered b := newFixture(t) b.p.cfg.Root = a.p.cfg.Root b.auth.Ticket.Dispatch.Action++ b.auth.Ticket.Scope.Job++ _, err = b.p.Execute(ctx, b.auth.Ticket.Dispatch) close(release) must(t, <-done) must(t, err) if b.pushes != 1 { t.Fatal("independent action did not push") } } func TestPublishSameActionLockStaysUncertain(t *testing.T) { f := newFixture(t) entered, release, done := make(chan struct{}), make(chan struct{}), make(chan error, 1) original := f.p.cfg.Export f.p.cfg.Export = func(c context.Context, g team.GitCandidate, s string) error { close(entered) <-release return original(c, g, s) } go func() { _, err := f.p.Execute(ctx, f.auth.Ticket.Dispatch); done <- err }() <-entered _, err := f.p.Reconcile(ctx, f.auth.Ticket.Dispatch) close(release) must(t, <-done) if !errors.Is(err, team.ErrUncertain) { t.Fatal("live action was reported absent", err) } } func TestPublishDefaultChangeAndManualMerge(t *testing.T) { for _, name := range []string{"default_change", "manual_merge"} { t.Run(name, func(t *testing.T) { f := newFixture(t) gitTest(t, f.root, "--git-dir="+f.remote, "fetch", f.source, f.auth.Candidate.Commit) gitTest(t, f.root, "--git-dir="+f.remote, "update-ref", "refs/heads/main", f.auth.Candidate.Commit) if name == "default_change" { gitTest(t, f.root, "--git-dir="+f.remote, "symbolic-ref", "HEAD", "refs/heads/new-default") } f.auth.Publication.Ref = "main" f.auth.Ticket.Dispatch.Payload, f.auth.Ticket.Dispatch.Hash, _ = Canonical(marshalPublication(t, f.auth.Publication)) _, err := f.p.Execute(ctx, f.auth.Ticket.Dispatch) assertNoEffect(t, f, err) if f.desc.calls != 0 { t.Fatal("external branch state granted description authority") } }) } } func TestPublishPushMarkerDurable(t *testing.T) { f := newFixture(t) f.p.beforePush = func() { f.pushes++ r, err := f.p.load(f.auth.Ticket.Dispatch) must(t, err) if r.Phase != "push_started" { t.Fatal("push began without durable marker", r.Phase) } } _, err := f.p.Execute(ctx, f.auth.Ticket.Dispatch) must(t, err) } func TestPublishJournalRecoveryClasses(t *testing.T) { for _, name := range []string{"legacy", "unknown_phase", "corrupt", "prepared_with_remote", "already_identical"} { t.Run(name, func(t *testing.T) { f := newFixture(t) d := f.auth.Ticket.Dispatch if name == "already_identical" { gitTest(t, f.root, "--git-dir="+f.remote, "fetch", f.source, f.auth.Candidate.Commit) gitTest(t, f.root, "--git-dir="+f.remote, "update-ref", "refs/heads/heads/main", f.auth.Candidate.Commit) _, err := f.p.Execute(ctx, d) must(t, err) r, err := f.p.Reconcile(ctx, d) must(t, err) if r.Receipt == nil || f.pushes != 0 || f.desc.calls != 1 { t.Fatal("identical head did not reconcile", r) } return } _, err := f.p.Execute(ctx, d) must(t, err) r, err := f.p.load(d) must(t, err) switch name { case "legacy": r.Phase = "" case "unknown_phase": r.Phase = "unrecognized" case "prepared_with_remote": r.Phase = "prepared" } must(t, runnerproto.JournalCredentials(f.p.path(d), r)) if name == "corrupt" { must(t, os.WriteFile(f.p.path(d), []byte("{"), 0600)) } result, err := f.p.Reconcile(ctx, d) switch name { case "legacy": must(t, err) if result.Receipt == nil { t.Fatal("legacy lost receipt") } case "prepared_with_remote": if !errors.Is(err, team.ErrNotDispatched) { t.Fatal("prepared journal inferred dispatch from remote", err) } default: if !errors.Is(err, team.ErrUncertain) { t.Fatal("unreadable history became no effect", err) } } }) } } func TestPublishReconcileTombstone(t *testing.T) { f := newFixture(t) _, err := f.p.Reconcile(ctx, f.auth.Ticket.Dispatch) if !errors.Is(err, team.ErrNotDispatched) { t.Fatal(err) } _, err = f.p.Execute(ctx, f.auth.Ticket.Dispatch) if !errors.Is(err, team.ErrNotDispatched) || f.pushes != 0 { t.Fatal("reconciled absence was dispatched", err, f.pushes) } } func TestPublishDescriptionFailureUncertain(t *testing.T) { f := newFixture(t) f.desc.fail = true _, err := f.p.Execute(ctx, f.auth.Ticket.Dispatch) if !errors.Is(err, team.ErrUncertain) { t.Fatal("description Set may have succeeded", err) } }