100 lines
3.9 KiB
Go
100 lines
3.9 KiB
Go
|
|
package app
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"errors"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||
|
|
"go.maximumdirect.net/commandkit"
|
||
|
|
configkit "go.maximumdirect.net/configkit"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestLockCommandsPreserveConditionalMutationAndIdempotentRemoval(t *testing.T) {
|
||
|
|
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
||
|
|
fake := &storage.FakeBackend{}
|
||
|
|
deps, _ := remoteSessionLoaderDependencies(fake)
|
||
|
|
app, err := newApplication(deps, executePlan)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
invoke := func(operation string, options ...string) (lockCommandReport, commandkit.Outcome) {
|
||
|
|
t.Helper()
|
||
|
|
args := []string{"session", "locks", operation, "2026-05-03"}
|
||
|
|
if operation != "list" {
|
||
|
|
args = append(args, "narratio.transcript.final_trimmed")
|
||
|
|
}
|
||
|
|
args = append(args, "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--format", "json")
|
||
|
|
args = append(args, options...)
|
||
|
|
var out, diagnostic bytes.Buffer
|
||
|
|
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||
|
|
var report lockCommandReport
|
||
|
|
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
|
||
|
|
t.Fatalf("report error=%v output=%s diagnostics=%s", err, out.String(), diagnostic.String())
|
||
|
|
}
|
||
|
|
return report, outcome
|
||
|
|
}
|
||
|
|
report, outcome := invoke("add", "--reason", "synthetic original reason")
|
||
|
|
if outcome.Status != 0 || !report.Complete || report.Result != "locked" {
|
||
|
|
t.Fatalf("add=%#v outcome=%#v", report, outcome)
|
||
|
|
}
|
||
|
|
report, outcome = invoke("add")
|
||
|
|
if outcome.Status != 1 || report.Complete || report.Result != "already_locked" {
|
||
|
|
t.Fatalf("duplicate add=%#v outcome=%#v", report, outcome)
|
||
|
|
}
|
||
|
|
report, outcome = invoke("add", "--force", "--reason", "synthetic revised reason")
|
||
|
|
if outcome.Status != 0 || report.Result != "locked" {
|
||
|
|
t.Fatalf("forced add=%#v outcome=%#v", report, outcome)
|
||
|
|
}
|
||
|
|
report, outcome = invoke("list")
|
||
|
|
if outcome.Status != 0 || len(report.Locks) != 1 || report.Locks[0].Reason != "synthetic revised reason" || !report.Locks[0].ConfiguredForPublish {
|
||
|
|
t.Fatalf("list=%#v outcome=%#v", report, outcome)
|
||
|
|
}
|
||
|
|
fake.UploadHook = func(storage.FakeUploadCall) error { return storage.ErrConditionNotMet }
|
||
|
|
report, outcome = invoke("add", "--force")
|
||
|
|
if outcome.Status != 1 || report.Complete || !errors.Is(outcome.Cause, storage.ErrConditionNotMet) {
|
||
|
|
t.Fatalf("conflict=%#v outcome=%#v", report, outcome)
|
||
|
|
}
|
||
|
|
fake.UploadHook = nil
|
||
|
|
for _, want := range []string{"unlocked", "not_locked"} {
|
||
|
|
report, outcome = invoke("remove")
|
||
|
|
if outcome.Status != 0 || report.Result != want || !report.Complete {
|
||
|
|
t.Fatalf("remove=%#v outcome=%#v", report, outcome)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
report, outcome = invoke("list")
|
||
|
|
if outcome.Status != 0 || report.Locks == nil || len(report.Locks) != 0 {
|
||
|
|
t.Fatalf("empty list=%#v outcome=%#v", report, outcome)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestLockCommandsCheckOperandsBeforeAcquisitionAndKeepGroupLazy(t *testing.T) {
|
||
|
|
app, err := newApplication(commandDependencies{
|
||
|
|
loadSecretsDir: func(string) (*configkit.SecretsReport, error) {
|
||
|
|
t.Fatal("help or invalid operands bootstrapped secrets")
|
||
|
|
return nil, nil
|
||
|
|
},
|
||
|
|
loadPipelineConfig: func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) {
|
||
|
|
t.Fatal("help or invalid operands loaded config")
|
||
|
|
return nil, nil
|
||
|
|
},
|
||
|
|
}, executePlan)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
for _, args := range [][]string{{"session", "locks"}, {"session", "locks", "--help"}, {"session", "locks", "add"}, {"session", "locks", "add", "session"}, {"session", "locks", "remove", "session"}, {"session", "locks", "list"}} {
|
||
|
|
var out, diagnostic bytes.Buffer
|
||
|
|
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||
|
|
if len(args) == 2 || args[2] == "--help" {
|
||
|
|
if outcome.Status != 0 {
|
||
|
|
t.Fatalf("help=%#v", outcome)
|
||
|
|
}
|
||
|
|
} else if outcome.Status != 2 {
|
||
|
|
t.Fatalf("%v outcome=%#v diagnostics=%s", args, outcome, diagnostic.String())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|