Support independent native configuration comparisons and overrides
This commit is contained in:
@@ -255,14 +255,9 @@ func TestAssembledSplitBundleCommandsUseOneEffectiveFamilyConfiguration(t *testi
|
||||
for _, operation := range []string{"validate", "show", "sources"} {
|
||||
nativeInspectionForTest(t, []string{"config", operation, "--config", pipelinePath, "--overlay", "testing"})
|
||||
}
|
||||
var diff bytes.Buffer
|
||||
if err := ConfigDiff(context.Background(), []string{
|
||||
"default", "testing", "--config", pipelinePath, "--campaign-file", campaignPath,
|
||||
}, &diff); err != nil {
|
||||
t.Fatalf("compare profiles: %v", err)
|
||||
}
|
||||
if !strings.Contains(diff.String(), "/audita/proposal_profile") || !strings.Contains(diff.String(), "/character_items_arannis/profile_id") {
|
||||
t.Fatalf("profile diff = %q, want proposal profile and expanded family changes", diff.String())
|
||||
diff := nativeInspectionForTest(t, []string{"config", "diff", "--details", "--config", pipelinePath, "--against-config", pipelinePath, "--against-overlay", "testing", "--format", "json"})
|
||||
if !strings.Contains(diff, "/audita/proposal_profile") || !strings.Contains(diff, "/scriptorium/artifact_families/character_items/profile_id") {
|
||||
t.Fatalf("profile diff = %q, want authored profile changes", diff)
|
||||
}
|
||||
|
||||
common := []string{
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func TestExecuteConfigUsesInvocationPipelineLoader(t *testing.T) {
|
||||
@@ -45,186 +44,6 @@ func TestExecuteConfigUsesInvocationPipelineLoader(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigDiffReportsSemanticOverlayChanges(t *testing.T) {
|
||||
workspaceRoot := t.TempDir()
|
||||
pipelinePath, _, _ := writeValidConfigFiles(t, workspaceRoot)
|
||||
writeInspectionOverlays(t, pipelinePath, `workspace:
|
||||
cleanup_after_publish: true
|
||||
publish:
|
||||
upload_run: true
|
||||
whisperx:
|
||||
language: en
|
||||
audita:
|
||||
modules: [glossary, grammar]
|
||||
scriptorium:
|
||||
artifacts:
|
||||
production_only:
|
||||
enabled: false
|
||||
output_path: artifacts/production.md
|
||||
`, `workspace:
|
||||
cleanup_after_publish: false
|
||||
whisperx:
|
||||
language: fr
|
||||
audita:
|
||||
modules: []
|
||||
scriptorium:
|
||||
artifacts:
|
||||
testing_only:
|
||||
enabled: false
|
||||
output_path: artifacts/testing.md
|
||||
`)
|
||||
|
||||
args := []string{"production", "testing", "--config", pipelinePath}
|
||||
var first, second bytes.Buffer
|
||||
if err := ConfigDiff(context.Background(), args, &first); err != nil {
|
||||
t.Fatalf("ConfigDiff() error = %v", err)
|
||||
}
|
||||
if err := ConfigDiff(context.Background(), args, &second); err != nil {
|
||||
t.Fatalf("second ConfigDiff() error = %v", err)
|
||||
}
|
||||
if first.String() != second.String() {
|
||||
t.Fatalf("diff output is not deterministic:\nfirst=%q\nsecond=%q", first.String(), second.String())
|
||||
}
|
||||
output := first.String()
|
||||
for _, want := range []string{
|
||||
"changed\t/audita/modules\t[\"glossary\",\"grammar\"]\t[]\n",
|
||||
"changed\t/workspace/cleanup_after_publish\ttrue\tfalse\n",
|
||||
"changed\t/whisperx/language\t\"en\"\t\"fr\"\n",
|
||||
"removed\t/scriptorium/artifacts/production_only\t{",
|
||||
"added\t/scriptorium/artifacts/testing_only\t{",
|
||||
} {
|
||||
if !strings.Contains(output, want) {
|
||||
t.Fatalf("diff output missing %q:\n%s", want, output)
|
||||
}
|
||||
}
|
||||
if strings.Index(output, "/audita/modules") > strings.Index(output, "/workspace/cleanup_after_publish") {
|
||||
t.Fatalf("diff records are not sorted by path:\n%s", output)
|
||||
}
|
||||
var reversed bytes.Buffer
|
||||
if err := ConfigDiff(context.Background(), []string{"testing", "production", "--config", pipelinePath}, &reversed); err != nil {
|
||||
t.Fatalf("reversed ConfigDiff() error = %v", err)
|
||||
}
|
||||
if !strings.Contains(reversed.String(), "changed\t/whisperx/language\t\"fr\"\t\"en\"\n") {
|
||||
t.Fatalf("reversed diff did not independently resolve overlays:\n%s", reversed.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigDiffComparesExpandedFamiliesAndPublishRules(t *testing.T) {
|
||||
pipelinePath, campaignPath := writeInspectionFamilyConfig(t)
|
||||
writeInspectionOverlays(t, pipelinePath, `scriptorium:
|
||||
artifact_families:
|
||||
character_note:
|
||||
enabled: false
|
||||
member_vars:
|
||||
character_name: character.name
|
||||
publish:
|
||||
enabled: true
|
||||
required: false
|
||||
`, `scriptorium:
|
||||
artifact_families:
|
||||
character_note:
|
||||
enabled: true
|
||||
prompt_id: dnd.character_note
|
||||
member_vars:
|
||||
character_name: character.class_summary
|
||||
publish:
|
||||
enabled: true
|
||||
required: true
|
||||
`)
|
||||
|
||||
var out bytes.Buffer
|
||||
if err := ConfigDiff(context.Background(), []string{
|
||||
"production", "testing", "--config", pipelinePath, "--campaign-file", campaignPath,
|
||||
}, &out); err != nil {
|
||||
t.Fatalf("ConfigDiff() error = %v", err)
|
||||
}
|
||||
output := out.String()
|
||||
for _, want := range []string{
|
||||
"changed\t/scriptorium/artifacts/character_note_arannis/enabled\tfalse\ttrue\n",
|
||||
"changed\t/scriptorium/artifacts/character_note_arannis/vars/character_name\t\"Arannis\"\t\"wizard\"\n",
|
||||
"changed\t/publish/outputs\t",
|
||||
} {
|
||||
if !strings.Contains(output, want) {
|
||||
t.Fatalf("family diff output missing %q:\n%s", want, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigDiffReportsEqualityAndRejectsInvalidInput(t *testing.T) {
|
||||
workspaceRoot := t.TempDir()
|
||||
pipelinePath, campaignPath, _ := writeValidConfigFiles(t, workspaceRoot)
|
||||
writeInspectionOverlays(t, pipelinePath, "whisperx:\n language: en\n", "whisperx:\n language: en\n")
|
||||
|
||||
var equal bytes.Buffer
|
||||
if err := ConfigDiff(context.Background(), []string{"production", "testing", "--config", pipelinePath}, &equal); err != nil {
|
||||
t.Fatalf("ConfigDiff() equal overlays error = %v", err)
|
||||
}
|
||||
if got := equal.String(); got != "no differences\n" {
|
||||
t.Fatalf("equal diff output = %q", got)
|
||||
}
|
||||
|
||||
for _, args := range [][]string{
|
||||
{"production", "testing", "extra", "--config", pipelinePath},
|
||||
{"production", "--config", pipelinePath},
|
||||
{"production", "production", "--config", pipelinePath},
|
||||
{"unknown", "testing", "--config", pipelinePath},
|
||||
{"production", "testing", "--config", pipelinePath, "--overlay", "production"},
|
||||
{"production", "testing", "--config", pipelinePath, "--config", pipelinePath},
|
||||
{"production", "testing", "--config", pipelinePath, "--campaign", "sample-campaign", "--campaign-file", campaignPath},
|
||||
} {
|
||||
if err := ConfigDiff(context.Background(), args, io.Discard); err == nil {
|
||||
t.Fatalf("ConfigDiff(%q) succeeded, want error", args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigDiffBootstrapsSecretsOnceForBothOverlays(t *testing.T) {
|
||||
workspaceRoot := t.TempDir()
|
||||
pipelinePath, _, _ := writeValidConfigFiles(t, workspaceRoot)
|
||||
writeInspectionOverlays(t, pipelinePath, "whisperx:\n language: en\n", "whisperx:\n language: fr\n")
|
||||
secretsDir := t.TempDir()
|
||||
const secretName = "NARRATIO_DIFF_BOOTSTRAP_REPORT"
|
||||
restoreEnvAfterTest(t, secretName)
|
||||
if err := os.Unsetenv(secretName); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mustWriteSecretFile(t, filepath.Join(secretsDir, secretName), "diff-secret-must-not-appear\n")
|
||||
bootstrapCalls := 0
|
||||
var loadedPaths []string
|
||||
var diagnostics bytes.Buffer
|
||||
deps := commandDependencies{loadSecretsDir: func(got string) (*configkit.SecretsReport, error) {
|
||||
bootstrapCalls++
|
||||
if got != secretsDir {
|
||||
t.Fatalf("secrets directory = %q, want %q", got, secretsDir)
|
||||
}
|
||||
return configkit.LoadSecretsIntoEnvironment(got)
|
||||
}, diagnostics: &diagnostics}
|
||||
search := configkit.SearchPaths(pipelinePath)
|
||||
deps.pipelineSearch = &search
|
||||
deps.loadPipelineConfig = func(path string, opts config.PipelineLoadOptions) (*config.PipelineConfig, error) {
|
||||
loadedPaths = append(loadedPaths, path)
|
||||
return config.LoadPipelineWithOptions(path, opts)
|
||||
}
|
||||
|
||||
if err := configDiffWithDependencies(context.Background(), []string{
|
||||
"production", "testing", "--secrets-dir", secretsDir,
|
||||
}, io.Discard, deps); err != nil {
|
||||
t.Fatalf("config diff: %v", err)
|
||||
}
|
||||
if bootstrapCalls != 1 {
|
||||
t.Fatalf("bootstrap calls = %d, want 1", bootstrapCalls)
|
||||
}
|
||||
if len(loadedPaths) != 2 || loadedPaths[0] != "" || loadedPaths[1] != pipelinePath {
|
||||
t.Fatalf("pipeline load paths = %q, want discovery then selected root %q", loadedPaths, pipelinePath)
|
||||
}
|
||||
if got := diagnostics.String(); strings.Count(got, `name="`+secretName+`"`) != 1 {
|
||||
t.Fatalf("bootstrap report count = %d, want 1; diagnostics = %q", strings.Count(got, `name="`+secretName+`"`), got)
|
||||
}
|
||||
if strings.Contains(diagnostics.String(), "diff-secret-must-not-appear") {
|
||||
t.Fatalf("bootstrap diagnostics exposed secret value: %q", diagnostics.String())
|
||||
}
|
||||
}
|
||||
|
||||
func writeInspectionOverlays(t *testing.T, pipelinePath, production, testing string) {
|
||||
t.Helper()
|
||||
overlaysDir := filepath.Join(filepath.Dir(pipelinePath), "overlays")
|
||||
@@ -335,13 +154,9 @@ func TestNativeBundleInspectionAndExecutionAgree(t *testing.T) {
|
||||
if !strings.Contains(catalog.String(), "character_meta_arannis") {
|
||||
t.Fatal("domain catalog omitted concrete family member")
|
||||
}
|
||||
var diff bytes.Buffer
|
||||
args := append([]string{"default", "testing"}, common...)
|
||||
if err := ConfigDiff(context.Background(), args, &diff); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(diff.String(), "/audita/proposal_profile") || strings.Contains(diff.String(), secretValue) {
|
||||
t.Fatalf("unexpected diff: %s", diff.String())
|
||||
diff := nativeInspectionForTest(t, []string{"config", "diff", "--details", "--against-overlay", "testing", "--secrets-dir", secrets, "--format", "json"})
|
||||
if !strings.Contains(diff, "/audita/proposal_profile") || strings.Contains(diff, secretValue) {
|
||||
t.Fatalf("unexpected diff: %s", diff)
|
||||
}
|
||||
workspace := filepath.Join(root, "conf.d", "workspace")
|
||||
if _, err := os.Stat(workspace); !os.IsNotExist(err) {
|
||||
|
||||
@@ -1,271 +0,0 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type configDiffFlags struct {
|
||||
pipelinePath singletonStringFlag
|
||||
campaignPath singletonStringFlag
|
||||
campaignFilePath singletonStringFlag
|
||||
secretsDir singletonStringFlag
|
||||
}
|
||||
|
||||
type configDiffRequest struct {
|
||||
leftOverlay string
|
||||
rightOverlay string
|
||||
flags configDiffFlags
|
||||
}
|
||||
|
||||
type configDiffResolved struct {
|
||||
left *inspectionConfig
|
||||
right *inspectionConfig
|
||||
}
|
||||
|
||||
// ConfigDiff compares two explicitly selected overlays through the same
|
||||
// read-only configuration resolution boundary used by config validate, show,
|
||||
// and sources. Differences describe normalized effective values rather than
|
||||
// the layout of root, import, or overlay files.
|
||||
func ConfigDiff(ctx context.Context, args []string, out io.Writer) error {
|
||||
return configDiffWithDependencies(ctx, args, out, commandDependencies{})
|
||||
}
|
||||
|
||||
func configDiffWithDependencies(_ context.Context, args []string, out io.Writer, deps commandDependencies) error {
|
||||
request, err := parseConfigDiffRequest(args, out)
|
||||
if err != nil {
|
||||
if errors.Is(err, flag.ErrHelp) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
resolved, err := resolveConfigDiff(request, deps)
|
||||
if err != nil {
|
||||
return fmt.Errorf("config diff: %w", err)
|
||||
}
|
||||
differences, err := config.DiffEffectivePipelines(resolved.left.Pipeline, resolved.right.Pipeline)
|
||||
if err != nil {
|
||||
return fmt.Errorf("config diff: %w", err)
|
||||
}
|
||||
if len(differences) == 0 {
|
||||
_, err := fmt.Fprintln(out, "no differences")
|
||||
return err
|
||||
}
|
||||
for _, difference := range differences {
|
||||
kind := "changed"
|
||||
if !difference.Left.Present {
|
||||
kind = "added"
|
||||
}
|
||||
if !difference.Right.Present {
|
||||
kind = "removed"
|
||||
}
|
||||
fields := []string{kind, difference.Path.String()}
|
||||
if difference.Left.Present {
|
||||
value, err := compactSnapshot(difference.Left)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fields = append(fields, value)
|
||||
}
|
||||
if difference.Right.Present {
|
||||
value, err := compactSnapshot(difference.Right)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fields = append(fields, value)
|
||||
}
|
||||
if difference.HiddenChanged {
|
||||
fields = append(fields, "hidden_changed=true")
|
||||
}
|
||||
if _, err := fmt.Fprintln(out, strings.Join(fields, "\t")); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseConfigDiffRequest(args []string, out io.Writer) (configDiffRequest, error) {
|
||||
overlays, flagArgs := splitConfigDiffArguments(args)
|
||||
fs := flag.NewFlagSet("config diff", flag.ContinueOnError)
|
||||
fs.SetOutput(out)
|
||||
var request configDiffRequest
|
||||
request.flags.pipelinePath.name = "config"
|
||||
request.flags.campaignPath.name = "campaign"
|
||||
request.flags.campaignFilePath.name = "campaign-file"
|
||||
request.flags.secretsDir.name = "secrets-dir"
|
||||
fs.Var(&request.flags.pipelinePath, "config", "path to pipeline.yml (optional; defaults searched)")
|
||||
fs.Var(&request.flags.campaignPath, "campaign", "campaign ID")
|
||||
fs.Var(&request.flags.campaignFilePath, "campaign-file", "path to campaign.yml")
|
||||
fs.Var(&request.flags.secretsDir, "secrets-dir", "secrets directory (optional; defaults to /usr/local/etc/narratio/secrets)")
|
||||
fs.Usage = func() {
|
||||
fmt.Fprintln(out, "Usage: narratio config diff <left-overlay> <right-overlay> [--config <pipeline.yml>] [--campaign <id> | --campaign-file <campaign.yml>] [--secrets-dir <path>]")
|
||||
fmt.Fprintln(out)
|
||||
fmt.Fprintln(out, "Compares fully resolved overlays without a session, workspace, manifest, adapters, or external services.")
|
||||
fmt.Fprintln(out)
|
||||
fmt.Fprintln(out, "Flags:")
|
||||
fs.PrintDefaults()
|
||||
}
|
||||
if err := fs.Parse(flagArgs); err != nil {
|
||||
return configDiffRequest{}, fmt.Errorf("config diff: invalid flags: %w", err)
|
||||
}
|
||||
overlays = append(overlays, fs.Args()...)
|
||||
if len(overlays) != 2 || strings.TrimSpace(overlays[0]) == "" || strings.TrimSpace(overlays[1]) == "" {
|
||||
return configDiffRequest{}, fmt.Errorf("config diff: exactly two non-empty overlay names are required")
|
||||
}
|
||||
if overlays[0] == overlays[1] {
|
||||
return configDiffRequest{}, fmt.Errorf("config diff: overlay names must differ")
|
||||
}
|
||||
if request.flags.campaignPath.set && request.flags.campaignFilePath.set {
|
||||
return configDiffRequest{}, fmt.Errorf("config diff: --campaign and --campaign-file are mutually exclusive")
|
||||
}
|
||||
request.leftOverlay = overlays[0]
|
||||
request.rightOverlay = overlays[1]
|
||||
return request, nil
|
||||
}
|
||||
|
||||
// splitConfigDiffArguments accepts the documented positional-first syntax and
|
||||
// also permits flags before or between overlay names. Values belonging to the
|
||||
// supported string flags stay with their flag so they are not mistaken for
|
||||
// overlay names.
|
||||
func splitConfigDiffArguments(args []string) (overlays, flagArgs []string) {
|
||||
for index := 0; index < len(args); index++ {
|
||||
argument := args[index]
|
||||
if !strings.HasPrefix(argument, "-") || argument == "-" {
|
||||
overlays = append(overlays, argument)
|
||||
continue
|
||||
}
|
||||
flagArgs = append(flagArgs, argument)
|
||||
name, hasValue := strings.CutPrefix(argument, "--")
|
||||
if !hasValue {
|
||||
continue
|
||||
}
|
||||
name, _, hasInlineValue := strings.Cut(name, "=")
|
||||
if hasInlineValue || !configDiffStringFlag(name) || index+1 >= len(args) {
|
||||
continue
|
||||
}
|
||||
index++
|
||||
flagArgs = append(flagArgs, args[index])
|
||||
}
|
||||
return overlays, flagArgs
|
||||
}
|
||||
|
||||
func configDiffStringFlag(name string) bool {
|
||||
switch name {
|
||||
case "config", "campaign", "campaign-file", "secrets-dir":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func resolveConfigDiff(request configDiffRequest, deps commandDependencies) (*configDiffResolved, error) {
|
||||
deps = normalizeCommandDependencies(deps)
|
||||
pipelinePath, leftPipeline, err := loadPipelineConfigWithDependencies(request.flags.pipelinePath.value, config.PipelineLoadOptions{Overlay: &request.leftOverlay}, request.flags.secretsDir.pointer(), deps)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load left pipeline overlay: %w", err)
|
||||
}
|
||||
_, rightPipeline, err := loadPipelineConfigWithDependencies(pipelinePath, config.PipelineLoadOptions{Overlay: &request.rightOverlay}, request.flags.secretsDir.pointer(), deps)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load right pipeline overlay: %w", err)
|
||||
}
|
||||
needsCampaign := pipelineHasArtifactFamilies(leftPipeline) || pipelineHasArtifactFamilies(rightPipeline)
|
||||
hasCampaignSelection := request.flags.campaignPath.set || request.flags.campaignFilePath.set
|
||||
if !needsCampaign && !hasCampaignSelection {
|
||||
if err := validateInspectionPipeline(pipelinePath, leftPipeline); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateInspectionPipeline(pipelinePath, rightPipeline); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &configDiffResolved{
|
||||
left: &inspectionConfig{PipelinePath: pipelinePath, Pipeline: leftPipeline},
|
||||
right: &inspectionConfig{PipelinePath: pipelinePath, Pipeline: rightPipeline},
|
||||
}, nil
|
||||
}
|
||||
|
||||
campaignPath, err := resolveSharedDiffCampaignPath(leftPipeline, rightPipeline, request.flags)
|
||||
if err != nil {
|
||||
if needsCampaign {
|
||||
return nil, fmt.Errorf("pipeline.scriptorium.artifact_families requires one shared campaign: %w", err)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
campaign, err := config.LoadCampaign(campaignPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if request.flags.campaignPath.set && !request.flags.campaignFilePath.set {
|
||||
if got := config.CampaignID(campaign); got != request.flags.campaignPath.value {
|
||||
return nil, fmt.Errorf("campaign config %q invalid: campaign_id %q does not match selected campaign %q", campaignPath, got, request.flags.campaignPath.value)
|
||||
}
|
||||
}
|
||||
leftLoaded, err := config.LoadPipelineCampaign(pipelinePath, leftPipeline, campaignPath, campaign)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rightLoaded, err := config.LoadPipelineCampaignWithParty(pipelinePath, rightPipeline, campaignPath, campaign, leftLoaded.Party)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateInspectionPipeline(leftLoaded.PipelinePath, leftLoaded.Pipeline); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateInspectionPipeline(rightLoaded.PipelinePath, rightLoaded.Pipeline); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateInspectionCampaign(campaignPath, campaign); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &configDiffResolved{
|
||||
left: &inspectionConfig{
|
||||
PipelinePath: leftLoaded.PipelinePath,
|
||||
Pipeline: leftLoaded.Pipeline,
|
||||
CampaignPath: leftLoaded.CampaignPath,
|
||||
Campaign: campaign,
|
||||
Party: leftLoaded.Party,
|
||||
},
|
||||
right: &inspectionConfig{
|
||||
PipelinePath: rightLoaded.PipelinePath,
|
||||
Pipeline: rightLoaded.Pipeline,
|
||||
CampaignPath: rightLoaded.CampaignPath,
|
||||
Campaign: campaign,
|
||||
Party: rightLoaded.Party,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func resolveSharedDiffCampaignPath(left, right *config.PipelineConfig, flags configDiffFlags) (string, error) {
|
||||
leftPath, leftErr := resolveCampaignConfigPath(left, flags.campaignPath.value, flags.campaignFilePath.value)
|
||||
if leftErr != nil {
|
||||
return "", leftErr
|
||||
}
|
||||
rightPath, rightErr := resolveCampaignConfigPath(right, flags.campaignPath.value, flags.campaignFilePath.value)
|
||||
if rightErr != nil {
|
||||
return "", rightErr
|
||||
}
|
||||
if leftPath != rightPath {
|
||||
return "", fmt.Errorf("overlay selections resolve different campaign files; specify --campaign-file to compare one campaign")
|
||||
}
|
||||
return leftPath, nil
|
||||
}
|
||||
|
||||
func compactSnapshot(snapshot configkit.ValueSnapshot) (string, error) {
|
||||
var value any
|
||||
if err := yaml.Unmarshal([]byte(snapshot.YAML), &value); err != nil {
|
||||
return "", fmt.Errorf("decode safe diff value: %w", err)
|
||||
}
|
||||
encoded, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("encode safe diff value: %w", err)
|
||||
}
|
||||
return string(encoded), nil
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfigDiffUsesEscapedPointersAndAtomicLists(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
root := filepath.Join(dir, "pipeline.yml")
|
||||
mustWriteTestFile(t, root, "whisperx: {transcribe_url: 'https://example.test/transcribe'}\nscriptorium:\n artifacts:\n recap:\n enabled: false\n output_path: artifacts/recap.md\n vars: {'a/b~c': before}\naudita: {modules: [glossary, grammar]}\n")
|
||||
mustWriteTestFile(t, filepath.Join(dir, "overlays", "changed.yml"), "scriptorium:\n artifacts:\n recap:\n vars: {'a/b~c': after}\naudita: {modules: [glossary, homophones]}\n")
|
||||
var output bytes.Buffer
|
||||
if err := ConfigDiff(context.Background(), []string{"default", "changed", "--config", root}, &output); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, want := range []string{"changed\t/audita/modules\t[\"glossary\",\"grammar\"]\t[\"glossary\",\"homophones\"]", "changed\t/scriptorium/artifacts/recap/vars/a~1b~0c\t\"before\"\t\"after\""} {
|
||||
if !strings.Contains(output.String(), want) {
|
||||
t.Fatalf("diff lacks %q: %s", want, output.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
// configurationAcquisition belongs to one parsed selection, including one side
|
||||
@@ -24,6 +25,7 @@ type configurationAcquisition struct {
|
||||
session *acquiredSession
|
||||
previousSessionID string
|
||||
domain *config.Config
|
||||
pipelineOverrides []configkit.Override
|
||||
}
|
||||
|
||||
func newConfigurationAcquisition(selection configurationSelection, deps commandDependencies) *configurationAcquisition {
|
||||
@@ -77,7 +79,7 @@ func (a *configurationAcquisition) Pipeline(ctx context.Context) (*config.Pipeli
|
||||
return a.pipeline, nil
|
||||
}
|
||||
path, cfg, err := loadPipelineConfigWithDependencies(a.selection.Config.Value,
|
||||
config.PipelineLoadOptions{Overlay: selectedString(a.selection.Overlay)},
|
||||
config.PipelineLoadOptions{Overlay: selectedString(a.selection.Overlay), Overrides: a.pipelineOverrides},
|
||||
selectedString(a.selection.SecretsDir), a.deps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -12,11 +12,17 @@ import (
|
||||
|
||||
type configurationInspectionState struct {
|
||||
deps commandDependencies
|
||||
independent bool
|
||||
acquisitions map[configurationSelection]*configurationAcquisition
|
||||
}
|
||||
|
||||
func (s *configurationInspectionState) acquisition(selection ckconfig.Selection, input configurationSelection) *configurationAcquisition {
|
||||
input.Config, input.Overlay = selection.Config, selection.Overlay
|
||||
if s.independent || len(selection.Overrides) != 0 {
|
||||
acquired := newConfigurationAcquisition(input, s.deps)
|
||||
acquired.pipelineOverrides = append([]configkit.Override(nil), selection.Overrides...)
|
||||
return acquired
|
||||
}
|
||||
acquired := s.acquisitions[input]
|
||||
if acquired == nil {
|
||||
acquired = newConfigurationAcquisition(input, s.deps)
|
||||
@@ -35,6 +41,7 @@ func configurationCommand(deps commandDependencies) commandkit.Command {
|
||||
}
|
||||
return cfg.ConfigKitResult(), nil
|
||||
})
|
||||
app.Features.Overrides = true
|
||||
campaign := ckconfig.FromConfigKit("campaign", "Campaign configuration.", func(ctx context.Context, selection ckconfig.Selection, input configurationSelection, state *configurationInspectionState) (*configkit.Result[config.CampaignConfig], error) {
|
||||
acquired := state.acquisition(selection, input)
|
||||
cfg, err := acquired.Campaign(ctx)
|
||||
@@ -60,6 +67,16 @@ func configurationCommand(deps commandDependencies) commandkit.Command {
|
||||
}
|
||||
return cfg.Canonical.ConfigKitResult(), nil
|
||||
})
|
||||
scopes := []ckconfig.Scope[configurationSelection, *configurationInspectionState]{app, campaign, session, party}
|
||||
// Comparison sides share bootstrap services, but always load their own results.
|
||||
for index := range scopes {
|
||||
compare := scopes[index].Compare
|
||||
scopes[index].Compare = func(ctx context.Context, request ckconfig.ComparisonRequest[configurationSelection], state *configurationInspectionState) (commandkit.Acquired[[]configkit.Difference], error) {
|
||||
separate := *state
|
||||
separate.independent = true
|
||||
return compare(ctx, request, &separate)
|
||||
}
|
||||
}
|
||||
return ckconfig.Command(ckconfig.Suite[configurationSelection, *configurationInspectionState]{
|
||||
NewState: func() *configurationInspectionState {
|
||||
invocationDeps := normalizeCommandDependencies(deps)
|
||||
@@ -71,13 +88,13 @@ func configurationCommand(deps commandDependencies) commandkit.Command {
|
||||
return checkConfigurationSelection(input)
|
||||
},
|
||||
Selectors: []ckconfig.Selector[configurationSelection]{
|
||||
{Binding: flags.Campaign, Scopes: []string{"campaign", "session", "party"}},
|
||||
{Binding: flags.CampaignFile, Scopes: []string{"campaign", "session", "party"}},
|
||||
{Binding: flags.Session, Scopes: []string{"session"}},
|
||||
{Binding: flags.SessionFile, Scopes: []string{"session"}},
|
||||
{Binding: flags.PartyFile, Scopes: []string{"party"}},
|
||||
{Binding: flags.Campaign, AgainstName: "against-campaign", Scopes: []string{"campaign", "session", "party"}},
|
||||
{Binding: flags.CampaignFile, AgainstName: "against-campaign-file", Scopes: []string{"campaign", "session", "party"}},
|
||||
{Binding: flags.Session, AgainstName: "against-session", Scopes: []string{"session"}},
|
||||
{Binding: flags.SessionFile, AgainstName: "against-session-file", Scopes: []string{"session"}},
|
||||
{Binding: flags.PartyFile, AgainstName: "against-party-file", Scopes: []string{"party"}},
|
||||
{Binding: flags.SecretsDir, Scopes: []string{"app", "campaign", "session", "party"}},
|
||||
},
|
||||
Scopes: []ckconfig.Scope[configurationSelection, *configurationInspectionState]{app, campaign, session, party},
|
||||
Scopes: scopes,
|
||||
})
|
||||
}
|
||||
|
||||
142
internal/app/configuration_comparison_test.go
Normal file
142
internal/app/configuration_comparison_test.go
Normal file
@@ -0,0 +1,142 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func TestConfigurationComparisonUsesIndependentRootDefaults(t *testing.T) {
|
||||
left, _, _ := writeValidConfigFiles(t, t.TempDir())
|
||||
right, _, _ := writeValidConfigFiles(t, t.TempDir())
|
||||
search := configkit.SearchPaths(right)
|
||||
loads, bootstrap := 0, 0
|
||||
app, err := newApplication(commandDependencies{
|
||||
pipelineSearch: &search,
|
||||
loadPipelineConfig: func(path string, options config.PipelineLoadOptions) (*config.PipelineConfig, error) {
|
||||
loads++
|
||||
return config.LoadPipelineWithOptions(path, options)
|
||||
},
|
||||
loadSecretsDir: func(string) (*configkit.SecretsReport, error) { bootstrap++; return nil, nil },
|
||||
}, executePlan)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out, diagnostic bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), []string{"config", "diff", "--details", "--config", left, "--check", "--format", "json", "--secrets-dir", t.TempDir()}, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||||
if outcome.Status != 3 || loads != 2 || bootstrap != 1 || !strings.Contains(out.String(), "/workspace/root") {
|
||||
t.Fatalf("status=%d loads=%d bootstrap=%d output=%s diagnostics=%s", outcome.Status, loads, bootstrap, out.String(), diagnostic.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigurationComparisonAndShowApplyOnlyAppOverrides(t *testing.T) {
|
||||
pipeline, campaign, _ := writeValidConfigFiles(t, t.TempDir())
|
||||
before, err := os.ReadFile(pipeline)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out, diagnostic bytes.Buffer
|
||||
args := []string{"config", "diff", "--details", "--config", pipeline, "--against-config", pipeline, "--set", `/whisperx/language="fr"`, "--against-set", `/whisperx/language="de"`, "--check", "--format", "json"}
|
||||
outcome := Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||||
if outcome.Status != 3 || !strings.Contains(out.String(), "/whisperx/language") || !strings.Contains(out.String(), "fr") || !strings.Contains(out.String(), "de") {
|
||||
t.Fatalf("status=%d output=%s diagnostics=%s", outcome.Status, out.String(), diagnostic.String())
|
||||
}
|
||||
shown := nativeInspectionForTest(t, []string{"config", "show", "--config", pipeline, "--set", `/whisperx/language="fr"`, "--path", "/whisperx/language", "--format", "json"})
|
||||
if !strings.Contains(shown, "fr") {
|
||||
t.Fatalf("override not applied: %s", shown)
|
||||
}
|
||||
shown = nativeInspectionForTest(t, []string{"config", "show", "--config", pipeline, "--path", "/whisperx/language", "--format", "json"})
|
||||
if strings.Contains(shown, "fr") {
|
||||
t.Fatalf("override persisted into later invocation: %s", shown)
|
||||
}
|
||||
after, err := os.ReadFile(pipeline)
|
||||
if err != nil || !bytes.Equal(before, after) {
|
||||
t.Fatal("inspection modified source file")
|
||||
}
|
||||
for _, scope := range []string{"campaign", "session", "party", "all"} {
|
||||
out.Reset()
|
||||
diagnostic.Reset()
|
||||
outcome := Execute(context.Background(), []string{"config", "show", "--scope", scope, "--config", pipeline, "--campaign-file", campaign, "--set", `/whisperx/language="fr"`}, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||||
if outcome.Status != 2 {
|
||||
t.Fatalf("scope %s accepted unsupported overrides: status=%d diagnostics=%s", scope, outcome.Status, diagnostic.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigurationComparisonSeparatesCampaignSessionAndRoster(t *testing.T) {
|
||||
_, leftCampaign, leftSession := writeValidConfigFiles(t, t.TempDir())
|
||||
_, rightCampaign, rightSession := writeValidConfigFiles(t, t.TempDir())
|
||||
rightBytes, err := os.ReadFile(rightCampaign)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mustWriteTestFile(t, rightCampaign, strings.ReplaceAll(string(rightBytes), "sample-campaign", "other-campaign"))
|
||||
mustWriteTestFile(t, rightSession, "session_id: 2026-05-03\ntitle: Independent right session\ninputs: {audio_dir: ./audio}\n")
|
||||
campaignCfg, err := config.LoadCampaign(rightCampaign)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
party, err := config.ResolveParty(rightCampaign, campaignCfg, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mustWriteTestFile(t, party.Source.Path, "schema_version: narratio.party.v1\ncharacters:\n other_character:\n player: {name: Synthetic Player}\n character: {name: Synthetic Character, classes: [{name: ranger}]}\n")
|
||||
leftCfg, err := config.LoadCampaign(leftCampaign)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
leftParty, err := config.ResolveParty(leftCampaign, leftCfg, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cases := []struct {
|
||||
scope, path string
|
||||
selectors []string
|
||||
}{
|
||||
{"campaign", "/campaign_id", []string{"--campaign-file", leftCampaign, "--against-campaign-file", rightCampaign}},
|
||||
{"session", "/title", []string{"--campaign-file", leftCampaign, "--session", "2026-05-03", "--session-file", leftSession, "--against-campaign-file", rightCampaign, "--against-session", "2026-05-03", "--against-session-file", rightSession}},
|
||||
{"party", "/characters", []string{"--party-file", leftParty.Source.Path, "--against-campaign-file", rightCampaign}},
|
||||
{"party", "/characters", []string{"--party-file", leftParty.Source.Path, "--against-party-file", party.Source.Path}},
|
||||
}
|
||||
for _, test := range cases {
|
||||
var out, diagnostic bytes.Buffer
|
||||
args := append([]string{"config", "diff", "--details", "--scope", test.scope, "--check", "--format", "json"}, test.selectors...)
|
||||
outcome := Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||||
if outcome.Status != 3 || !strings.Contains(out.String(), test.path) {
|
||||
t.Fatalf("scope=%s status=%d output=%s diagnostics=%s", test.scope, outcome.Status, out.String(), diagnostic.String())
|
||||
}
|
||||
}
|
||||
// An omitted right-side party selector uses normal campaign discovery, never
|
||||
// the left side's exact party file.
|
||||
defaultRoot := filepath.Join(t.TempDir(), "pipeline.yml")
|
||||
mustWriteTestFile(t, defaultRoot, "whisperx: {transcribe_url: https://transcription.example.test/transcribe}\ncampaigns:\n root: "+filepath.Dir(filepath.Dir(rightCampaign))+"\n default_campaign_id: sample-campaign\n")
|
||||
nativeInspectionForTest(t, []string{"config", "diff", "--details", "--scope", "party", "--party-file", leftParty.Source.Path, "--against-config", defaultRoot, "--format", "json"})
|
||||
}
|
||||
|
||||
func TestConfigurationComparisonKeepsCredentialValuesHidden(t *testing.T) {
|
||||
const leftSecret, rightSecret = "synthetic-left-comparison-secret", "synthetic-right-comparison-secret"
|
||||
t.Setenv("NARRATIO_COMPARE_LEFT", leftSecret)
|
||||
t.Setenv("NARRATIO_COMPARE_RIGHT", rightSecret)
|
||||
left := filepath.Join(t.TempDir(), "left.yml")
|
||||
right := filepath.Join(t.TempDir(), "right.yml")
|
||||
base := "whisperx: {transcribe_url: https://transcription.example.test/transcribe}\n"
|
||||
mustWriteTestFile(t, left, base+"audita: {credentials: [NARRATIO_COMPARE_LEFT]}\n")
|
||||
mustWriteTestFile(t, right, base+"audita: {credentials: [NARRATIO_COMPARE_RIGHT]}\n")
|
||||
var out, diagnostic bytes.Buffer
|
||||
outcome := Execute(context.Background(), []string{"config", "diff", "--config", left, "--against-config", right, "--check", "--details", "--format", "json"}, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||||
if outcome.Status != 3 {
|
||||
t.Fatalf("status=%d output=%s diagnostics=%s", outcome.Status, out.String(), diagnostic.String())
|
||||
}
|
||||
for _, secret := range []string{leftSecret, rightSecret} {
|
||||
if strings.Contains(out.String()+diagnostic.String(), secret) {
|
||||
t.Fatal("comparison exposed resolved credentials")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user