package app import ( "bytes" "context" "io" "reflect" "strings" "testing" ) func TestRegenerateArtifactsForwardsExactCanonicalRunArguments(t *testing.T) { original := runCommandFn t.Cleanup(func() { runCommandFn = original }) var captured []string runCommandFn = func(_ context.Context, args []string, _ io.Writer) error { captured = append([]string(nil), args...) return nil } code := Execute([]string{ "regenerate-artifacts", "2026-05-03", "--artifacts", "session_recap,player_handout", "--artifacts=player_handout", "--config", "pipeline.yml", "--campaign", "sample-campaign", "--profile", "testing", }, io.Discard, io.Discard) if code != 0 { t.Fatalf("Execute() code = %d, want 0", code) } want := []string{ "2026-05-03", "--force", "--from", "extract", "--through", "analyze", "--artifacts", "session_recap,player_handout", "--artifacts=player_handout", "--config", "pipeline.yml", "--campaign", "sample-campaign", "--profile", "testing", } if !reflect.DeepEqual(captured, want) { t.Fatalf("forwarded args = %#v, want %#v", captured, want) } } func TestRegenerateArtifactsForwardsSessionIDCompatibilityFlag(t *testing.T) { original := runCommandFn t.Cleanup(func() { runCommandFn = original }) var captured []string runCommandFn = func(_ context.Context, args []string, _ io.Writer) error { captured = append([]string(nil), args...) return nil } code := Execute([]string{ "regenerate-artifacts", "--config", "pipeline.yml", "--session-id", "2026-05-03", "--artifacts", "session_recap", }, io.Discard, io.Discard) if code != 0 { t.Fatalf("Execute() code = %d, want 0", code) } want := []string{ "--force", "--from", "extract", "--through", "analyze", "--config", "pipeline.yml", "--session-id", "2026-05-03", "--artifacts", "session_recap", } if !reflect.DeepEqual(captured, want) { t.Fatalf("forwarded args = %#v, want %#v", captured, want) } } func TestRegenerateArtifactsHelpDoesNotInvokeRun(t *testing.T) { original := runCommandFn t.Cleanup(func() { runCommandFn = original }) called := false runCommandFn = func(_ context.Context, _ []string, _ io.Writer) error { called = true return nil } var stdout bytes.Buffer var stderr bytes.Buffer if code := Execute([]string{"regenerate-artifacts", "--help"}, &stdout, &stderr); code != 0 { t.Fatalf("Execute() code = %d, stderr = %q", code, stderr.String()) } if called { t.Fatal("help invoked canonical run handler") } for _, detail := range []string{ "narratio run --force --from extract --through analyze", "Extraction always runs", "Publish and notify never run", } { if !strings.Contains(stdout.String(), detail) { t.Fatalf("help = %q, want %q", stdout.String(), detail) } } if stderr.Len() != 0 { t.Fatalf("stderr = %q, want empty", stderr.String()) } } func TestRegenerateArtifactsOwnedOptionsFailThroughRunParser(t *testing.T) { for _, test := range []struct { name string args []string owned string }{ {name: "force", args: []string{"--force"}, owned: "force"}, {name: "from", args: []string{"--from=render"}, owned: "from"}, {name: "through", args: []string{"--through", "publish"}, owned: "through"}, } { t.Run(test.name, func(t *testing.T) { args := []string{"regenerate-artifacts", "2026-05-03"} args = append(args, test.args...) var stderr bytes.Buffer if code := Execute(args, io.Discard, &stderr); code == 0 { t.Fatalf("Execute(%#v) code = 0", args) } if !strings.Contains(stderr.String(), "--"+test.owned+" may be specified only once") { t.Fatalf("stderr = %q, want shared duplicate %s error", stderr.String(), test.owned) } }) } } func TestRegenerateArtifactsRejectsUnknownOptionsThroughRunParser(t *testing.T) { var stderr bytes.Buffer if code := Execute([]string{"regenerate-artifacts", "2026-05-03", "--regenerate-only"}, io.Discard, &stderr); code == 0 { t.Fatal("Execute() code = 0") } if !strings.Contains(stderr.String(), "flag provided but not defined") || !strings.Contains(stderr.String(), "regenerate-only") { t.Fatalf("stderr = %q, want canonical parser unknown-option error", stderr.String()) } }