package inventory import ( "context" "errors" "fmt" "path/filepath" "strings" "testing" "supermarket/internal/db" ) func TestParseYAMLWebsiteOnly(t *testing.T) { doc := "- url: https://a.example\n relationship: competitor\n- website: b.example\n- https://c.example\n" + "- name: D\n url: https://d.example\n websites: [https://d.example, https://blog.d.example]\n" entries, err := ParseYAML([]byte(doc), Partner) if err != nil || len(entries) != 4 { t.Fatalf("%+v %v", entries, err) } var got []string for _, e := range entries { got = append(got, fmt.Sprintf("%s|%s|%s", e.Name, e.Relationship, strings.Join(e.Websites, ","))) } want := "|competitor|https://a.example |partner|b.example |partner|https://c.example D|partner|https://d.example,https://blog.d.example" if strings.Join(got, " ") != want { t.Errorf("got %s\nwant %s", strings.Join(got, " "), want) } // Exported entries don't grow a url:. out, _ := ExportYAML(entries[:1]) if strings.Contains(string(out), "url:") { t.Errorf("export:\n%s", out) } } func testDB(t *testing.T) *db.DB { t.Helper() d, err := db.Open(filepath.Join(t.TempDir(), "app.db")) if err != nil { t.Fatal(err) } t.Cleanup(func() { d.Close() }) return d } func TestFillImport(t *testing.T) { d := testDB(t) srv := rivalSite(t) if err := Save(d, &Entry{Name: "Old", Relationship: Partner, Websites: []string{"https://www.old.invalid/"}}); err != nil { t.Fatal(err) } fake := &fakeAI{reply: map[string]any{ "name": "Rival Inc", "description": "Hosted widgets.", "talking_points": "", "keywords": []string{}, "hashtags": []string{}, "subreddits": []map[string]string{}, }} in := []Entry{ {Websites: []string{srv.URL}, Relationship: Competitor}, {Websites: []string{srv.URL + "/"}, Relationship: Partner}, // the same site {Websites: []string{"old.invalid"}, Relationship: Partner}, // already there: never read {Name: "Plain", Relationship: Watch}, // nothing to fill in from {Websites: []string{"ftp://nope"}}, {}, } var told int lines := FillImport(context.Background(), d, in, FillOptions{Client: fake, Workers: 1, Done: func(FillLine) { told++ }}) var got []string for _, l := range lines { got = append(got, l.Name+":"+l.Outcome) } // Those known without reading a site first, then the ones filled in. if sum := strings.Join(got, " "); sum != ":skipped :skipped Plain:added :failed :failed Rival Inc:added" { t.Errorf("lines = %q", sum) } if told != len(in) || fake.calls != 1 { t.Errorf("told %d, model called %d times", told, fake.calls) } if lines[0].Note != "listed twice" || lines[1].Note != "already in the Market" || lines[1].ID == 0 { t.Errorf("notes: %+v %+v", lines[0], lines[1]) } e, _ := Get(d, lines[len(lines)-1].ID) if e == nil || e.Relationship != Competitor || e.Description != "Hosted widgets." || e.TwitterHandle != "rivalhq" || e.Websites[0] != srv.URL || e.MentionPolicy != MentionCategory { t.Errorf("saved: %+v", e) } // Again: already there, by website, before reading it. lines = FillImport(context.Background(), d, in[:1], FillOptions{Client: fake}) if len(lines) != 1 || lines[0].Outcome != FillSkipped || fake.calls != 1 { t.Errorf("again: %+v, %d calls", lines, fake.calls) } // Replacing it reads it again, and keeps its id. id := e.ID lines = FillImport(context.Background(), d, in[:1], FillOptions{Client: fake, Overwrite: true}) if len(lines) != 1 || lines[0].Outcome != FillUpdated || lines[0].ID != id || fake.calls != 2 { t.Errorf("overwrite: %+v, %d calls", lines, fake.calls) } } func TestFillImportLimitsAndFailures(t *testing.T) { d := testDB(t) srv := rivalSite(t) fake := &fakeAI{reply: map[string]any{"name": "", "description": "", "talking_points": "", "keywords": []string{}, "hashtags": []string{}, "subreddits": []map[string]string{}}} full := errors.New("the plan is full") lines := FillImport(context.Background(), d, []Entry{{Name: "Named", Websites: []string{srv.URL}, Relationship: Partner}}, FillOptions{Client: fake, Room: func(int) error { return full }}) if len(lines) != 1 || lines[0].Outcome != FillFailed || lines[0].Note != full.Error() { t.Errorf("limit: %+v", lines) } // No name typed and none found: not saved. lines = FillImport(context.Background(), d, []Entry{{Websites: []string{srv.URL}, Relationship: Partner}}, FillOptions{Client: fake}) if len(lines) != 1 || lines[0].Outcome != FillFailed || !strings.Contains(lines[0].Note, "name wasn't found") { t.Errorf("no name: %+v", lines) } // A site that can't be read, with a name typed: saved, with what went wrong. lines = FillImport(context.Background(), d, []Entry{{Name: "Gone", Websites: []string{srv.URL + "/missing"}, Relationship: Watch}}, FillOptions{Client: fake}) if len(lines) != 1 || lines[0].Outcome != FillAdded || !strings.Contains(lines[0].Note, "only partly") { t.Errorf("unreadable: %+v", lines) } }