docs(lighthouse,postcard,signpost): compile README Go examples via src= (quick-261118-qsd)

This commit is contained in:
Jakub Zych
2026-11-18 20:13:14 +01:00
parent a2551d83d7
commit 1e56d750ef
7 changed files with 482 additions and 150 deletions

View File

@@ -1,11 +1,15 @@
package signpost_test
import (
"bytes"
"context"
"fmt"
"log"
"strings"
"testing"
"time"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/pact"
"git.golem15.com/golem15/summercms/modules/signpost"
)
@@ -130,3 +134,46 @@ func ExampleRegistry_MatchPath() {
// 2026-10-01 Ada 2 2
// acme-article 7 true
}
// listenForMoves registers a consumer that maps every saved record to its
// content type and logs the record's URL.
func listenForMoves(app *backpack.App, logger *log.Logger) {
// docs:start listen
app.Events.Listen("acme.audit", func(ctx context.Context, ev *pact.RecordSaved) error {
reg, _ := app.Lookup[*signpost.Registry]()
typ, ok := reg.ContentTypeForModel(ev.Model)
if !ok {
return nil
}
rec, err := reg.Describe(ctx, typ.Key, ev.ID, "")
if err != nil || rec == nil {
return err
}
logger.Printf("%s now lives at %s", ev.ID, rec.Locales[0].URL)
return nil
})
// docs:end listen
}
// TestDocsRecordListener fires record events for an article and for a model
// no content type declares through listenForMoves.
func TestDocsRecordListener(t *testing.T) {
app := exampleApp() // its menu_items.yaml overrides acme-post of BlogPlugin
news := NewsPlugin{articles: []Article{{ID: "7", Slug: "launch", Title: "Launch", Published: true}}}
if err := signpost.Activate(app, []interface{ ID() string }{BlogPlugin{}, news}); err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
listenForMoves(app, log.New(&buf, "", 0))
for _, ev := range []*pact.RecordSaved{
{Model: "acme_news_articles", ID: "7"},
{Model: "acme_shop_orders", ID: "3"},
} {
if err := app.Events.Fire(t.Context(), ev); err != nil {
t.Fatal(err)
}
}
if got, want := buf.String(), "7 now lives at /news/launch\n"; got != want {
t.Fatalf("log = %q, want %q", got, want)
}
}