Replace the old Go TUI with a web app (port 8470) that runs in a container as a CoreDesktop sibling: - cmd/supermarket server with htmx web UI: dashboard, company profile, inventory (partners/competitors), AI and credential settings, and a Core & Hermes page - SQLite storage and a secrets store under ~/.local/share/supermarket - AI providers (Anthropic, OpenAI-compatible) behind one interface - MCP server and a Hermes bridge to the Federated Core MCP hub - Import of the old Coremarketing TUI's state - install.sh / update.sh / lib.sh, Dockerfile, compose.yaml, the bin/supermarket launcher and hermes/setup.sh Posting is not wired back up yet; that comes in Phase 1. See PLAN.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
149 lines
4.2 KiB
Go
149 lines
4.2 KiB
Go
package legacy
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"supermarket/internal/ai"
|
|
"supermarket/internal/db"
|
|
"supermarket/internal/inventory"
|
|
"supermarket/internal/profile"
|
|
"supermarket/internal/secrets"
|
|
)
|
|
|
|
const oldConfigYAML = `applications:
|
|
- name: "Nextcloud"
|
|
description: "Files"
|
|
twitter_handle: "Nextclouders"
|
|
youtube_channel_id: "UCx"
|
|
instagram_handle: ""
|
|
hashtags: ["#Nextcloud", "SelfHosted"]
|
|
- name: "Jitsi"
|
|
description: "Video"
|
|
twitter_handle: "jitsinews"
|
|
credentials:
|
|
twitter:
|
|
bearer_token: "bt"
|
|
client_id: "cid"
|
|
oauth2_access_token: "at"
|
|
oauth2_refresh_token: "rt"
|
|
linkedin:
|
|
access_token: ""
|
|
claude:
|
|
api_key: "sk-old"
|
|
model: "claude-sonnet-4-6"
|
|
posting:
|
|
twitter_handle: "federatedcomp"
|
|
max_length: 280
|
|
settings:
|
|
max_results: 15
|
|
`
|
|
|
|
func write(t *testing.T, dir, name, body string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestImport(t *testing.T) {
|
|
dir := t.TempDir()
|
|
importDir := filepath.Join(dir, "import")
|
|
os.Mkdir(importDir, 0700)
|
|
write(t, importDir, "config.yaml", oldConfigYAML)
|
|
write(t, importDir, "state.json", `{"last_checked":{"Nextcloud":"2026-11-01T10:00:00Z"}}`)
|
|
write(t, importDir, "posts.json", `{"posts":[{"id":"1","app_name":"Jitsi","content":"Hi","source_url":"https://x.com/1","generated_at":"2026-11-02T10:00:00Z","posted_to":["x"]}]}`)
|
|
write(t, importDir, "dismissed.json", `{"ids":{"a":true,"b":true,"c":false}}`)
|
|
|
|
d, err := db.Open(filepath.Join(dir, "app.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer d.Close()
|
|
sec, _ := secrets.Open(filepath.Join(dir, "secrets.json"))
|
|
// A key entered in the new app must survive the import.
|
|
sec.Set(map[string]string{secrets.AnthropicAPIKey: "sk-new"})
|
|
|
|
st, err := Import(d, sec, importDir)
|
|
if err != nil || st == nil {
|
|
t.Fatalf("import: %v %v", st, err)
|
|
}
|
|
|
|
entries, _ := inventory.List(d, inventory.Partner)
|
|
if len(entries) != 2 || entries[1].Name != "Nextcloud" || entries[1].Hashtags[1] != "#SelfHosted" {
|
|
t.Errorf("inventory: %+v", entries)
|
|
}
|
|
if sec.Get(secrets.XAccessToken) != "at" || sec.Get(secrets.XRefreshToken) != "rt" || sec.Get(secrets.XBearerToken) != "bt" {
|
|
t.Error("X credentials not imported")
|
|
}
|
|
if sec.Get(secrets.AnthropicAPIKey) != "sk-new" {
|
|
t.Error("import overwrote a newer key")
|
|
}
|
|
if sec.Has(secrets.LinkedInAccessToken) {
|
|
t.Error("empty values must not be imported")
|
|
}
|
|
cfg, _ := ai.LoadConfig(d)
|
|
if cfg.Model != "claude-sonnet-4-6" {
|
|
t.Errorf("model = %q", cfg.Model)
|
|
}
|
|
p, _ := profile.Load(d)
|
|
if p.Accounts.X != "federatedcomp" {
|
|
t.Errorf("x account = %q", p.Accounts.X)
|
|
}
|
|
var cs ContentSettings
|
|
d.GetJSON(ContentSettingsKey, &cs)
|
|
if cs.MaxLength != 280 || cs.MaxResults != 15 || cs.LookbackDays != 7 {
|
|
t.Errorf("content settings = %+v", cs)
|
|
}
|
|
count := func(q string) int {
|
|
var n int
|
|
d.QueryRow(q).Scan(&n)
|
|
return n
|
|
}
|
|
if count(`SELECT count(*) FROM posts WHERE posted_to = '["x"]'`) != 1 {
|
|
t.Error("posts not imported")
|
|
}
|
|
if count(`SELECT count(*) FROM dismissed`) != 2 {
|
|
t.Error("dismissed not imported")
|
|
}
|
|
if count(`SELECT count(*) FROM fetch_state WHERE key = 'inventory:Nextcloud'`) != 1 {
|
|
t.Error("state not imported")
|
|
}
|
|
|
|
// Runs once.
|
|
if st, err := Import(d, sec, importDir); st != nil || err != nil {
|
|
t.Errorf("second import ran: %v %v", st, err)
|
|
}
|
|
}
|
|
|
|
func TestImportNothingToDo(t *testing.T) {
|
|
dir := t.TempDir()
|
|
d, _ := db.Open(filepath.Join(dir, "app.db"))
|
|
defer d.Close()
|
|
sec, _ := secrets.Open(filepath.Join(dir, "secrets.json"))
|
|
if st, err := Import(d, sec, filepath.Join(dir, "import")); st != nil || err != nil {
|
|
t.Errorf("got %v %v", st, err)
|
|
}
|
|
}
|
|
|
|
func TestImportBadFileIsRetried(t *testing.T) {
|
|
dir := t.TempDir()
|
|
write(t, dir, "config.yaml", oldConfigYAML)
|
|
write(t, dir, "posts.json", `{not json`)
|
|
d, _ := db.Open(filepath.Join(dir, "app.db"))
|
|
defer d.Close()
|
|
sec, _ := secrets.Open(filepath.Join(dir, "secrets.json"))
|
|
if _, err := Import(d, sec, dir); err == nil {
|
|
t.Fatal("expected an error for posts.json")
|
|
}
|
|
write(t, dir, "posts.json", `{"posts":[]}`)
|
|
if st, err := Import(d, sec, dir); err != nil || st == nil {
|
|
t.Fatalf("retry: %v %v", st, err)
|
|
}
|
|
entries, _ := inventory.List(d, "")
|
|
if len(entries) != 2 {
|
|
t.Errorf("retry duplicated or lost entries: %d", len(entries))
|
|
}
|
|
}
|