Inventory: competitor prices, as checked

Each Inventory entry can hold its plans' prices: the plan, the price,
what it's per, when it was checked and where. Comparison and alternative
pages, and new sections, state a competitor's price only as entered,
with the month it was checked; a missing one is left out and listed as a
to-do. A price checked more than six months ago is flagged on the entry
and adds a to-do to every page that names the competitor. Prices go out
and come back with the Inventory's YAML.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-24 08:18:31 -07:00
co-authored by Claude Opus 5.5
parent e0e74af86c
commit f5d87164ac
10 changed files with 357 additions and 4 deletions
+4
View File
@@ -50,6 +50,7 @@ type Entry struct {
Keywords []string `json:"keywords,omitempty" yaml:"keywords,omitempty"`
TalkingPoints string `json:"talking_points,omitempty" yaml:"talking_points,omitempty"`
MentionPolicy string `json:"mention_policy,omitempty" yaml:"mention_policy,omitempty"`
Prices []Price `json:"prices,omitempty" yaml:"prices,omitempty"` // their plans' prices, as checked
}
// Normalize tidies an entry and checks it.
@@ -74,6 +75,9 @@ func (e *Entry) Normalize() error {
e.RSS = profile.CleanList(e.RSS)
e.Keywords = profile.CleanList(e.Keywords)
e.TalkingPoints = strings.TrimSpace(e.TalkingPoints)
if err := e.normalizePrices(); err != nil {
return err
}
subs := profile.CleanList(e.Subreddits)
for i, s := range subs {
+119
View File
@@ -0,0 +1,119 @@
package inventory
import (
"errors"
"fmt"
"net/url"
"strings"
"time"
)
// Price is one of an entry's plans, as the user read it on their site:
// what it costs, per what, when it was checked and where. Pages that
// compare with a competitor say only these prices, with the date.
type Price struct {
Plan string `json:"plan,omitempty" yaml:"plan,omitempty"` // "Standard", "Pro"…
Amount string `json:"amount" yaml:"amount"` // as written: "$25", "€19", "Free"
Per string `json:"per,omitempty" yaml:"per,omitempty"` // "per agent per month", "per year"
Checked string `json:"checked" yaml:"checked"` // YYYY-MM-DD
Source string `json:"source,omitempty" yaml:"source,omitempty"` // the pricing page
}
// StaleAfter is how old a price can be before pages built with it say to
// check it again.
const StaleAfter = 180 * 24 * time.Hour
const dateFormat = "2006-01-02"
// normalizePrices drops empty rows and checks the rest.
func (e *Entry) normalizePrices() error {
var keep []Price
for _, p := range e.Prices {
p.Plan, p.Amount, p.Per = strings.TrimSpace(p.Plan), strings.TrimSpace(p.Amount), strings.TrimSpace(p.Per)
p.Checked, p.Source = strings.TrimSpace(p.Checked), strings.TrimSpace(p.Source)
if p.Amount == "" {
if p.Plan != "" || p.Per != "" || p.Source != "" {
return fmt.Errorf("%s: give the price of %q, or clear its row", e.Name, or(p.Plan, "the plan"))
}
continue
}
if p.Checked == "" {
return fmt.Errorf("%s: say when you checked the price %s", e.Name, p.Amount)
}
if _, err := time.Parse(dateFormat, p.Checked); err != nil {
return fmt.Errorf("%s: %q isn't a date (YYYY-MM-DD)", e.Name, p.Checked)
}
if p.Source != "" {
if u, err := url.Parse(p.Source); err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
return errors.New(e.Name + ": the price's source must be a web address (https://…)")
}
}
keep = append(keep, p)
}
e.Prices = keep
return nil
}
// String is a price in words: "Pro: $25 per agent per month".
func (p Price) String() string {
s := p.Amount
if p.Per != "" {
s += " " + p.Per
}
if p.Plan != "" {
s = p.Plan + ": " + s
}
return s
}
// CheckedOn is when the price was checked, as "24 Nov 2026".
func (p Price) CheckedOn() string {
t, err := time.Parse(dateFormat, p.Checked)
if err != nil {
return p.Checked
}
return t.Format("2 Jan 2006")
}
// Stale says whether a price is older than StaleAfter.
func (p Price) Stale(now time.Time) bool {
t, err := time.Parse(dateFormat, p.Checked)
return err != nil || now.Sub(t) > StaleAfter
}
// StalePrices are the entry's prices due for another look.
func (e *Entry) StalePrices(now time.Time) []Price {
var out []Price
for _, p := range e.Prices {
if p.Stale(now) {
out = append(out, p)
}
}
return out
}
// PricesPrompt is the entry's prices for the AI, each with when and where
// it was read; "" without prices.
func (e *Entry) PricesPrompt() string {
if len(e.Prices) == 0 {
return ""
}
var b strings.Builder
fmt.Fprintf(&b, "%s's prices, as the owner read them on %s's site (the only prices you may state for them, each with the month it was checked, e.g. \"as of Nov 2026\"):\n", e.Name, e.Name)
for _, p := range e.Prices {
t, _ := time.Parse(dateFormat, p.Checked)
fmt.Fprintf(&b, "- %s (checked %s", p.String(), t.Format("Jan 2006"))
if p.Source != "" {
fmt.Fprintf(&b, ", from %s", p.Source)
}
b.WriteString(")\n")
}
return b.String()
}
func or(a, b string) string {
if a != "" {
return a
}
return b
}
+62
View File
@@ -0,0 +1,62 @@
package inventory
import (
"path/filepath"
"strings"
"testing"
"time"
"gopkg.in/yaml.v3"
"supermarket/internal/db"
)
func TestPrices(t *testing.T) {
d, err := db.Open(filepath.Join(t.TempDir(), "app.db"))
if err != nil {
t.Fatal(err)
}
defer d.Close()
e := &Entry{Name: "Zendesk", Relationship: Competitor, Prices: []Price{
{Plan: " Suite Team ", Amount: "$55", Per: "per agent per month", Checked: "2026-11-24", Source: "https://www.zendesk.com/pricing/"},
{Plan: "", Amount: "", Per: "", Checked: "2026-11-24"}, // an empty row, with today's date: dropped
{Plan: "Old", Amount: "$49", Per: "per agent per month", Checked: "2026-03-01"},
}}
if err := Save(d, e); err != nil {
t.Fatal(err)
}
got, err := Get(d, e.ID)
if err != nil || len(got.Prices) != 2 || got.Prices[0].Plan != "Suite Team" {
t.Fatalf("saved: %+v %v", got, err)
}
if got.Prices[0].String() != "Suite Team: $55 per agent per month" || got.Prices[0].CheckedOn() != "24 Nov 2026" {
t.Errorf("words: %q %q", got.Prices[0].String(), got.Prices[0].CheckedOn())
}
now := time.Date(2026, 11, 24, 0, 0, 0, 0, time.UTC)
if st := got.StalePrices(now); len(st) != 1 || st[0].Plan != "Old" {
t.Errorf("stale: %+v", st)
}
prompt := got.PricesPrompt()
for _, want := range []string{"the only prices you may state for them", "- Suite Team: $55 per agent per month (checked Nov 2026, from https://www.zendesk.com/pricing/)"} {
if !strings.Contains(prompt, want) {
t.Errorf("prompt lacks %q:\n%s", want, prompt)
}
}
// Exported and imported with the rest of the entry.
raw, _ := yaml.Marshal(got)
var back Entry
if err := yaml.Unmarshal(raw, &back); err != nil || len(back.Prices) != 2 || back.Prices[1].Checked != "2026-03-01" {
t.Errorf("yaml: %v %+v\n%s", err, back.Prices, raw)
}
for _, bad := range []Price{
{Amount: "$5"}, // no date
{Amount: "$5", Checked: "24/11/2026"}, // not a date
{Plan: "Pro", Checked: "2026-11-24", Per: "x"}, // a row without its price
{Amount: "$5", Checked: "2026-11-24", Source: "zendesk.com/pricing"},
} {
if err := Save(d, &Entry{Name: "Bad", Relationship: Competitor, Prices: []Price{bad}}); err == nil {
t.Errorf("accepted %+v", bad)
}
}
}