package cli_test import ( "bytes" "context" "git.a-wan.net/alan/pipelines/internal/cli" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/transfer" "path/filepath" "strings" "testing" ) func TestExportImportAcrossServersCreateOnly(t *testing.T) { ctx := context.Background() sourceRun, source, _ := setup(t) destRun, dest, _ := setup(t) for _, name := range []string{"alpha", "beta"} { if _, err := source.Submit(ctx, name, pipeline.Template([]string{"one"}, true)); err != nil { t.Fatal(err) } } updated := pipeline.Template([]string{"changed", "review"}, true) if _, err := source.Submit(ctx, "alpha", updated); err != nil { t.Fatal(err) } bundle := filepath.Join(t.TempDir(), "pipelines.zip") if _, err := sourceRun("export", "alpha", "beta", "--output", bundle, "--json"); err != nil { t.Fatal(err) } entries, err := transfer.Read(bundle) if err != nil || len(entries) != 2 || entries[0].Version != 2 || entries[0].Source != updated { t.Fatal(entries, err) } text, err := sourceRun("export", "alpha", "--version", "1") if err != nil || text != pipeline.Template([]string{"one"}, true) { t.Fatal("legacy Markdown export broken", text, err) } if _, err = destRun("import", bundle, "--all", "--json"); err == nil { t.Fatal("import bypassed confirmation") } items, _ := dest.List(ctx) if len(items) != 0 { t.Fatal("canceled import changed destination") } output, err := destRun("import", bundle, "--all", "--yes", "--json") if err != nil || !strings.Contains(output, "alpha") { t.Fatal(output, err) } items, _ = dest.List(ctx) if len(items) != 2 { t.Fatal(items) } for _, item := range items { if item.Version != 1 { t.Fatal("source version incorrectly used on destination", item) } if item.Name == "alpha" { detail, _ := dest.Get(ctx, item.ID, 1) if detail.Source != updated { t.Fatal("source altered", detail) } } } if output, err = destRun("import", bundle, "--all", "--yes", "--json"); err != nil || !strings.Contains(output, "identical") { t.Fatal("identical import not skipped", output, err) } items, _ = dest.List(ctx) for _, item := range items { if item.Version != 1 { t.Fatal("duplicate import changed history") } } if _, err = sourceRun("export", "--all", "--output", bundle, "--json"); err == nil { t.Fatal("export overwrote existing file") } } func TestImportConflictPoliciesPreserveHistory(t *testing.T) { for _, policy := range []string{"ask", "keep", "both", "import"} { t.Run(policy, func(t *testing.T) { ctx := context.Background() run, store, _ := setup(t) old := pipeline.Template([]string{"old"}, true) incoming := pipeline.Template([]string{"new"}, true) if _, err := store.Submit(ctx, "work", old); err != nil { t.Fatal(err) } // Keep-both must avoid both destination names and names elsewhere in this bundle. if _, err := store.Submit(ctx, "work (imported)", old); err != nil { t.Fatal(err) } file := filepath.Join(t.TempDir(), "conflict.zip") if err := transfer.Write(file, []transfer.Entry{{Name: "work", Version: 7, Source: incoming}, {Name: "work (imported 2)", Version: 1, Source: incoming}}); err != nil { t.Fatal(err) } output, err := run("import", file, "--all", "--yes", "--json", "--on-conflict", policy) if policy == "ask" { if err == nil || !strings.Contains(err.Error(), "differs") { t.Fatal("implicit update allowed", output, err) } items, _ := store.List(ctx) if len(items) != 2 { t.Fatal("conflict published other entries") } return } if err != nil { t.Fatal(output, err) } items, _ := store.List(ctx) expectedCount := 3 if policy == "both" { expectedCount = 4 } if len(items) != expectedCount { t.Fatal(items) } for _, p := range items { if p.Name == "work" { detail, _ := store.Get(ctx, p.ID, 0) want := old version := 1 if policy == "import" { want = incoming version = 2 } if detail.Source != want || detail.Version != version { t.Fatal("wrong retained choice", detail) } historical, _ := store.Get(ctx, p.ID, 1) if historical.Source != old { t.Fatal("history overwritten") } } if p.Name == "work (imported 3)" { detail, _ := store.Get(ctx, p.ID, 0) if detail.Source != incoming || detail.Version != 1 { t.Fatal(detail) } } } if policy == "both" && !strings.Contains(output, "work (imported 3)") { t.Fatal("copy name not reported", output) } }) } } func TestInteractiveImportConflictDiffAndConfirmation(t *testing.T) { for _, input := range []string{"d\ni\ny\n", "b\ny\n", "k\n", "i\nno\n", "c\n"} { t.Run(strings.ReplaceAll(input, "\n", "-"), func(t *testing.T) { _, store, url := setup(t) ctx := context.Background() old := pipeline.Template([]string{"old"}, true) incoming := pipeline.Template([]string{"new"}, true) store.Submit(ctx, "work", old) file := filepath.Join(t.TempDir(), "conflict.zip") if err := transfer.Write(file, []transfer.Entry{{Name: "work", Version: 4, Source: incoming}}); err != nil { t.Fatal(err) } var out, stderr bytes.Buffer err := (cli.App{Out: &out, Err: &stderr, In: strings.NewReader(input)}).Run(ctx, []string{"--url", url, "import", file, "--all"}) if input == "c\n" || input == "i\nno\n" { if err == nil { t.Fatal("cancellation bypassed") } } else if err != nil { t.Fatal(out.String(), stderr.String(), err) } items, _ := store.List(ctx) for _, p := range items { if p.Name == "work" { detail, _ := store.Get(ctx, p.ID, 0) want := old if strings.HasPrefix(input, "d") { want = incoming } if detail.Source != want { t.Fatal("wrong conflict choice", detail) } } } if strings.HasPrefix(input, "b") && len(items) != 2 { t.Fatal("both did not create a copy", items) } if strings.HasPrefix(input, "d") && (!strings.Contains(out.String(), "-# stage old") || !strings.Contains(out.String(), "+# stage new")) { t.Fatal("conflict diff missing", out.String()) } }) } } func TestImportValidatesEntireSelectionBeforePublishing(t *testing.T) { run, store, _ := setup(t) bundle := filepath.Join(t.TempDir(), "bad.zip") if err := transfer.Write(bundle, []transfer.Entry{{Name: "valid", Version: 1, Source: pipeline.Template([]string{"one"}, true)}, {Name: "invalid", Version: 1, Source: "not a pipeline"}}); err != nil { t.Fatal(err) } if _, err := run("import", bundle, "--all", "--yes", "--json"); err == nil { t.Fatal("invalid import accepted") } items, _ := store.List(context.Background()) if len(items) != 0 { t.Fatal("preflight published partial selection", items) } }