oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "context"
5 "log/slog"
6 "path/filepath"
7 "strings"
8 "sync"
9 "testing"
10 "time"
11
12 "amadan.net/rastrillo/rastrillo/db"
13
14 "amadan.net/oficina/notion.git/notion"
15 "amadan.net/oficina/notion.git/notiontest"
16)
17
18// The Notion import's harness: a Mapa over a fresh database, connected
19// to the module's fake Notion. Nothing here dials anything else.
20
21// fakeNotion is NotionAccess over a notiontest server.
22type fakeNotion struct {
23 server *notiontest.Server
24 // unpinned builds clients without the method pin (gate I16).
25 unpinned bool
26}
27
28func (fake fakeNotion) Verify(ctx context.Context,
29 token string) (notion.Workspace, error) {
30 client, err := fake.Client(token)
31 if err != nil {
32 return notion.Workspace{}, err
33 }
34 return client.Me(ctx)
35}
36
37func (fake fakeNotion) Client(token string) (*notion.Client, error) {
38 if fake.unpinned {
39 return notiontest.UnpinnedClient(token, fake.server.URL)
40 }
41 return notiontest.Client(token, fake.server.URL)
42}
43
44func (fake fakeNotion) Files() *notion.Files {
45 return notiontest.Files(fake.server)
46}
47
48// importWorld is one Mapa and one fake Notion.
49type importWorld struct {
50 t *testing.T
51 app *app
52 store *Store
53 server *notiontest.Server
54 logs *lockedBuffer
55}
56
57func newImportWorld(t *testing.T, ws *notiontest.Workspace) *importWorld {
58 t.Helper()
59 logs := &lockedBuffer{}
60 logger := slog.New(slog.NewTextHandler(logs, nil))
61 database, err := db.Open(filepath.Join(t.TempDir(), "mapa.db"), logger)
62 if err != nil {
63 t.Fatal(err)
64 }
65 t.Cleanup(func() { database.Close() })
66 config := Config{Sealer: testSealer(t), TimeZone: time.UTC,
67 Owners: []string{ana, bo}, Importers: []string{ana},
68 Origin: "http://127.0.0.1:8834"}
69 built, err := App(database, config, logger)
70 if err != nil {
71 t.Fatal(err)
72 }
73 server := notiontest.Start(ws, notion.APIVersion)
74 t.Cleanup(server.Close)
75 built.app.notion = fakeNotion{server: server}
76 world := &importWorld{t: t, app: built.app, store: built.Store,
77 server: server, logs: logs}
78 t.Cleanup(world.app.running.wait.Wait)
79 return world
80}
81
82// connect saves the fake's token as who.
83func (world *importWorld) connect(who string) {
84 world.t.Helper()
85 workspace, err := world.app.notion.Verify(ctx(), "secret_test")
86 if err != nil {
87 world.t.Fatal(err)
88 }
89 if err := world.store.Connect(ctx(), who, "secret_test",
90 workspace); err != nil {
91 world.t.Fatal(err)
92 }
93}
94
95// importAs runs a whole import as who and waits for it.
96func (world *importWorld) importAs(who string) NotionRun {
97 world.t.Helper()
98 lease, err := world.store.StartRun(ctx(), who, false)
99 if err != nil {
100 world.t.Fatalf("StartRun: %v", err)
101 }
102 return world.finish(lease, who)
103}
104
105// finish launches a run's goroutine and waits for it to end.
106func (world *importWorld) finish(lease runLease, who string) NotionRun {
107 world.t.Helper()
108 if err := world.app.launch(lease, who); err != nil {
109 world.t.Fatalf("launch: %v", err)
110 }
111 world.app.running.wait.Wait()
112 return world.run(lease.run)
113}
114
115func (world *importWorld) run(id int64) NotionRun {
116 world.t.Helper()
117 var run NotionRun
118 if err := world.store.database.Where("id = ?", id).
119 First(&run).Error; err != nil {
120 world.t.Fatal(err)
121 }
122 return run
123}
124
125// count is a table's rows.
126func (world *importWorld) count(table string, where ...any) int64 {
127 world.t.Helper()
128 var rows int64
129 query := world.store.database.Table(table)
130 if len(where) > 0 {
131 query = query.Where(where[0], where[1:]...)
132 }
133 if err := query.Count(&rows).Error; err != nil {
134 world.t.Fatal(err)
135 }
136 return rows
137}
138
139// pageTitled is the live team page with title, opened.
140func (world *importWorld) pageTitled(title string) (Page, Record, bool) {
141 world.t.Helper()
142 var pages []Page
143 world.store.database.Where("kind = ? AND gone = ?", KindPage, false).
144 Find(&pages)
145 for _, page := range pages {
146 record, err := world.store.OpenPage(page)
147 if err != nil {
148 world.t.Fatal(err)
149 }
150 if record.Title == title {
151 return page, record, true
152 }
153 }
154 return Page{}, Record{}, false
155}
156
157// lockedBuffer is a log sink a run's goroutine and a test share.
158type lockedBuffer struct {
159 mutex sync.Mutex
160 text strings.Builder
161}
162
163func (buffer *lockedBuffer) Write(data []byte) (int, error) {
164 buffer.mutex.Lock()
165 defer buffer.mutex.Unlock()
166 return buffer.text.Write(data)
167}
168
169func (buffer *lockedBuffer) String() string {
170 buffer.mutex.Lock()
171 defer buffer.mutex.Unlock()
172 return buffer.text.String()
173}
174