Add Lua plugin host, built-in amazon plugin and dibs check.
Sandboxed gopher-lua with time, memory and call-stack limits from [sandbox], a goquery-backed doc API, price parsing, and the 'check', 'plugin test' and 'fixtures' commands. Sellers are first_party, fulfilled (shipped by the shop) or third_party, and items accept fulfilled by default; used-only offers are not available. Plugin tests run on real pages that 'dibs fixtures' fetches into an untracked directory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f90ef18f90
commit
bb010356a0
28 files changed
+2096
-26
No files matched your search
@@ -0,0 +1,100 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
|
||||
"git.security-command.org/Overlord/dibs/internal/fetch"
|
||||
"git.security-command.org/Overlord/dibs/internal/fixtures"
|
||||
)
|
||||
|
||||
// testdata is the repository's top-level testdata directory.
|
||||
var testdata = filepath.Join("..", "..", "testdata")
|
||||
|
||||
func discardLogger() *slog.Logger { return slog.New(slog.NewTextHandler(io.Discard, nil)) }
|
||||
|
||||
func builtin(t *testing.T, name string) *Plugin {
|
||||
t.Helper()
|
||||
|
||||
code, err := BuiltinSource(name)
|
||||
if err != nil { t.Fatal(err) }
|
||||
|
||||
p, err := Load(name, "built-in", code, DefaultLimits(), discardLogger())
|
||||
if err != nil { t.Fatal(err) }
|
||||
t.Cleanup(p.Close)
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// TestSavedPages re-reads every page `dibs fixtures` saved and checks the
|
||||
// plugin still reports what it reported when the page was fetched.
|
||||
func TestSavedPages(t *testing.T) {
|
||||
suites, err := fixtures.Load(testdata)
|
||||
if err != nil { t.Fatal(err) }
|
||||
|
||||
ran := 0
|
||||
|
||||
for _, suite := range suites {
|
||||
if !slices.Contains(Builtins(), suite.Shop) { continue }
|
||||
|
||||
p := builtin(t, suite.Shop)
|
||||
|
||||
for _, c := range suite.Cases {
|
||||
page, err := os.ReadFile(suite.PagePath(c))
|
||||
if errors.Is(err, fs.ErrNotExist) { continue }
|
||||
if err != nil { t.Fatal(err) }
|
||||
|
||||
ran++
|
||||
|
||||
t.Run(suite.Shop+"/"+c.Name, func(t *testing.T) {
|
||||
want, err := os.ReadFile(suite.ReportPath(c))
|
||||
if err != nil { t.Fatal(err) }
|
||||
|
||||
var saved Report
|
||||
if err := json.Unmarshal(want, &saved); err != nil { t.Fatal(err) }
|
||||
|
||||
report, err := Evaluate(context.Background(), p, saved.URL, fetch.NewResponse(saved.FetchURL, saved.FinalURL, saved.Status, nil, page))
|
||||
if err != nil { t.Fatal(err) }
|
||||
|
||||
got, err := fixtures.EncodeReport(report)
|
||||
if err != nil { t.Fatal(err) }
|
||||
|
||||
if !bytes.Equal(got, want) { t.Errorf("%s (%s) now reads differently.\ngot:\n%s\nsaved:\n%s", c.Name, c.Note, got, want) }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if ran == 0 { t.Skip("no saved pages: run `dibs fixtures` to fetch them") }
|
||||
}
|
||||
|
||||
func TestAmazonNormalize(t *testing.T) {
|
||||
data, err := os.ReadFile(filepath.Join(testdata, "amazon", "urls.toml"))
|
||||
if err != nil { t.Fatal(err) }
|
||||
|
||||
var urls struct {
|
||||
Normalize []struct {
|
||||
In string `toml:"in"`
|
||||
Out string `toml:"out"`
|
||||
} `toml:"normalize"`
|
||||
}
|
||||
if err := toml.Unmarshal(data, &urls); err != nil { t.Fatal(err) }
|
||||
|
||||
p := builtin(t, "amazon")
|
||||
|
||||
for _, u := range urls.Normalize {
|
||||
got, err := p.Normalize(context.Background(), u.In)
|
||||
if err != nil { t.Fatal(err) }
|
||||
if got != u.Out { t.Errorf("normalize(%q) = %q, want %q", u.In, got, u.Out) }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user