package cli import ( "bytes" "context" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/transfer" "path/filepath" "strings" "testing" "unicode/utf8" ) func TestImportUpdateRejectsConcurrentPublication(t *testing.T) { ctx := context.Background() s, err := pipeline.Open(filepath.Join(t.TempDir(), "race.db")) if err != nil { t.Fatal(err) } defer s.Close() old := pipeline.Template([]string{"old"}, true) incoming := pipeline.Template([]string{"incoming"}, true) concurrent := pipeline.Template([]string{"concurrent"}, true) s.Submit(ctx, "work", old) bundle := filepath.Join(t.TempDir(), "import.zip") if err = transfer.Write(bundle, []transfer.Entry{{Name: "work", Version: 1, Source: incoming}}); err != nil { t.Fatal(err) } call := func(req protocol.Message) (protocol.Message, error) { switch req.Type { case protocol.List: items, err := s.List(ctx) return protocol.Message{Pipelines: items}, err case protocol.GetPipeline: detail, err := s.Get(ctx, req.PipelineID, req.Version) return protocol.Message{Detail: detail}, err case protocol.Validate: g, err := pipeline.Parse(req.Source) return protocol.Message{Type: protocol.Validated, Graph: &g}, err case protocol.Submit: if req.ExpectedVersion != 1 || req.CreateOnly { t.Fatal("unguarded update", req) } if _, err := s.Submit(ctx, "work", concurrent); err != nil { t.Fatal(err) } _, err := s.SubmitChecked(ctx, req.Name, req.Source, req.CreateOnly, req.ExpectedVersion) return protocol.Message{}, err } t.Fatal("unexpected request", req) return protocol.Message{}, nil } var output bytes.Buffer app := App{Out: &output, Err: &output, In: strings.NewReader("")} err = app.importBundle(ctx, &options{all: true, yes: true, json: true, conflict: "import"}, []string{bundle}, call) if err == nil || !strings.Contains(err.Error(), "draft conflict") { t.Fatal("concurrent update overwritten", err) } items, _ := s.List(ctx) detail, _ := s.Get(ctx, items[0].ID, 0) if detail.Source != concurrent || detail.Version != 2 { t.Fatal(detail) } } func TestImportCopyNameFitsUnicodeLimit(t *testing.T) { name := strings.Repeat("界", 66) reserved := map[string]bool{} a := importCopyName(name, reserved) reserved[a] = true b := importCopyName(name, reserved) if a == b || len(a) > 200 || len(b) > 200 || !utf8.ValidString(a) || !utf8.ValidString(b) { t.Fatal(a, b) } }