| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "encoding/json" |
| 8 | "flag" |
| 9 | "fmt" |
| 10 | "github.com/carlosframework/docs/internal/core" |
| 11 | "github.com/carlosframework/docs/internal/docs" |
| 12 | "io" |
| 13 | "log/slog" |
| 14 | "os" |
| 15 | ) |
| 16 | |
| 17 | func must(e error) { |
| 18 | if e != nil { |
| 19 | panic(e) |
| 20 | } |
| 21 | } |
| 22 | func main() { |
| 23 | path := flag.String("db", "", "disposable database") |
| 24 | address := flag.String("address", "", "synthetic local account") |
| 25 | phase := flag.String("phase", "", "phase") |
| 26 | flag.Parse() |
| 27 | ctx := context.Background() |
| 28 | app, e := docs.New(docs.Config{DBPath: *path, Env: "development", Origin: "https://compat.example", SampleDocuments: "false", Logger: slog.New(slog.NewTextHandler(io.Discard, nil))}) |
| 29 | must(e) |
| 30 | defer app.Close() |
| 31 | d, e := openProbeDB(*path) |
| 32 | must(e) |
| 33 | defer d.Close() |
| 34 | id, e := app.Store().EnsurePerson(ctx, *address) |
| 35 | must(e) |
| 36 | principal := principalStep(ctx, d, *phase) |
| 37 | if principal > 0 { |
| 38 | var n int |
| 39 | must(d.Writer().QueryRow("SELECT COUNT(*) FROM documents WHERE owner_id=?", principal).Scan(&n)) |
| 40 | if n == 0 { |
| 41 | _, e := app.Store().Create(ctx, principal, "home-owned") |
| 42 | must(e) |
| 43 | } |
| 44 | } |
| 45 | var sqid string |
| 46 | e = d.Writer().QueryRow(`SELECT sqid FROM documents WHERE title='kept-content'`).Scan(&sqid) |
| 47 | if e != nil { |
| 48 | doc, e := app.Store().Create(ctx, id, "kept-content") |
| 49 | must(e) |
| 50 | sqid = doc.Sqid |
| 51 | update, e := os.ReadFile("fixtures/vectors/fragment/hello.update.bin") |
| 52 | must(e) |
| 53 | _, e = app.Store().ApplyUpdate(ctx, doc, fixtureActor(id, *address), update) |
| 54 | must(e) |
| 55 | must(app.Store().Grant(ctx, id, sqid, "reader@example.invalid", docs.RoleWriter)) |
| 56 | _, e = d.Writer().Exec(`INSERT INTO doc_snapshots(doc_id,seq,state,abi_version,created_at)VALUES(?,1,?,?,CURRENT_TIMESTAMP)`, doc.ID, update, core.ABIVersion) |
| 57 | must(e) |
| 58 | _, e = d.Writer().Exec(`INSERT INTO documents(sqid,title,guid,client_id,created_at,updated_at,owner_id)VALUES('orphan','orphan','orphan',1,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,0)`) |
| 59 | must(e) |
| 60 | _, e = d.Writer().Exec(`INSERT INTO grants(doc_id,subject,role,created_by)VALUES(0,'sentinel@example.invalid','reader',0)`) |
| 61 | must(e) |
| 62 | } |
| 63 | var owner int64 |
| 64 | must(d.Writer().QueryRow(`SELECT owner_id FROM documents WHERE sqid=?`, sqid).Scan(&owner)) |
| 65 | doc, e := app.Store().Get(ctx, owner, sqid) |
| 66 | must(e) |
| 67 | text, e := app.Store().Text(ctx, doc) |
| 68 | must(e) |
| 69 | if text != "hello world" { |
| 70 | panic(text) |
| 71 | } |
| 72 | if _, e = d.Writer().Exec(`UPDATE documents SET owner_id=owner_id`); e != nil { |
| 73 | panic(e) |
| 74 | } |
| 75 | if _, e = d.Writer().Exec(`UPDATE grants SET created_by=created_by`); e != nil { |
| 76 | panic(e) |
| 77 | } |
| 78 | rows, e := d.Writer().Query(`SELECT update_bin,content_encoding FROM doc_updates WHERE doc_id=(SELECT id FROM documents WHERE title='kept-content') UNION ALL SELECT state,content_encoding FROM doc_snapshots WHERE doc_id=(SELECT id FROM documents WHERE title='kept-content')`) |
| 79 | must(e) |
| 80 | h := sha256.New() |
| 81 | encodings := []int{} |
| 82 | for rows.Next() { |
| 83 | var b []byte |
| 84 | var encoding int |
| 85 | must(rows.Scan(&b, &encoding)) |
| 86 | h.Write(b) |
| 87 | encodings = append(encodings, encoding) |
| 88 | } |
| 89 | must(rows.Err()) |
| 90 | rows.Close() |
| 91 | var author int64 |
| 92 | must(d.Writer().QueryRow(`SELECT created_by FROM grants WHERE subject='reader@example.invalid'`).Scan(&author)) |
| 93 | var homeCount int |
| 94 | must(d.Writer().QueryRow(`SELECT COUNT(*) FROM documents WHERE owner_id>=4503599627370496`).Scan(&homeCount)) |
| 95 | var ledger int |
| 96 | d.Writer().QueryRow(`SELECT COUNT(*) FROM docs_principals`).Scan(&ledger) |
| 97 | out := map[string]any{"phase": *phase, "local_id": id, "home_id": principal, "owner": owner, "grant_author": author, "sqid": sqid, "content_hash": hex.EncodeToString(h.Sum(nil)), "encodings": encodings, "text": text, "principal_count": ledger, "home_owned": homeCount} |
| 98 | b, e := json.Marshal(out) |
| 99 | must(e) |
| 100 | fmt.Println(string(b)) |
| 101 | } |
| 102 | |