Repository navigation
Expand file tree
/
Copy pathservice_notion_sync_test.go
More file actions
276 lines (262 loc) · 13.1 KB
/
Copy pathservice_notion_sync_test.go
File metadata and controls
276 lines (262 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package kata
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.kenn.io/kata/internal/config"
"go.kenn.io/kata/internal/daemon"
"go.kenn.io/kata/internal/db"
"go.kenn.io/kata/internal/githubsync"
"go.kenn.io/kata/internal/notionsync"
)
func TestServiceNotionScheduledProgress(t *testing.T) {
ctx := context.Background()
fetcher := newRuntimeNotionFetcher()
var resolved config.NotionSyncConfig
service, err := newService(ctx, Config{DSN: filepath.Join(t.TempDir(), "service.db"), Auth: AuthConfig{TrustCallerAuthentication: true}, NotionSync: NotionSyncConfig{TokenEnv: " EXAMPLE_NOTION_TOKEN "}}, serviceDeps{notionSyncFetcherFactory: func(cfg config.NotionSyncConfig) notionsync.Fetcher { resolved = cfg; return fetcher }})
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, service.Close()) })
require.Equal(t, "EXAMPLE_NOTION_TOKEN", resolved.TokenEnv)
// Enable and scheduled polling must share the configured fetcher.
close(fetcher.releaseSource)
project, err := service.store.CreateProject(ctx, "example-project")
require.NoError(t, err)
enable := httptest.NewRecorder()
service.Handler().ServeHTTP(enable, newRuntimeNotionPost(fmt.Sprintf("/api/v1/projects/%d/issue-sync/notion/enable", project.ID), strings.NewReader(`{"config":{"data_source_id":"11111111-1111-1111-1111-111111111111","done_statuses":["Delivered"]}}`)))
require.Equal(t, http.StatusOK, enable.Code, enable.Body.String())
binding, err := service.store.IssueSyncBindingByProject(ctx, project.ID)
require.NoError(t, err)
require.Equal(t, 300, binding.IntervalSeconds)
sub := service.broadcaster.Subscribe(daemon.SubFilter{ProjectID: project.ID})
defer sub.Unsub()
recording := &runtimeNotionRecordingStore{Storage: service.store, recorded: make(chan db.IssueSyncStatus, 1)}
service.store = recording
runCtx, cancel := context.WithCancel(ctx)
done := make(chan error, 1)
go func() { done <- service.Run(runCtx) }()
t.Cleanup(func() {
cancel()
select {
case err := <-done:
require.NoError(t, err)
case <-time.After(5 * time.Second):
require.Fail(t, "Run did not stop")
}
})
waitRuntimeNotion(t, fetcher.contentStarted)
status := readRuntimeNotionStatus(t, service.Handler(), project.ID)
require.NotNil(t, status.Status.Progress)
require.Equal(t, "content", status.Status.Progress.Phase)
require.Zero(t, status.Status.Progress.Completed)
require.Equal(t, 1, status.Status.Progress.Total)
durable, err := service.store.IssueSyncStatusByProject(ctx, project.ID)
require.NoError(t, err)
require.NotNil(t, durable.SyncStartedAt)
require.Equal(t, *durable.SyncStartedAt, status.Status.Progress.StartedAt)
require.Equal(t, "content", service.notionSyncProgress.Snapshot(binding.ID, *durable.SyncStartedAt).Phase)
once := httptest.NewRecorder()
service.Handler().ServeHTTP(once, newRuntimeNotionPost(fmt.Sprintf("/api/v1/projects/%d/issue-sync/notion/once", project.ID), strings.NewReader(`{}`)))
require.Equal(t, http.StatusConflict, once.Code, once.Body.String())
require.Equal(t, status.Status.Progress, readRuntimeNotionStatus(t, service.Handler(), project.ID).Status.Progress)
close(fetcher.releaseContent)
committed := waitRuntimeNotionRecord(t, recording.recorded)
require.NotNil(t, committed.LastSuccessAt)
status = readRuntimeNotionStatus(t, service.Handler(), project.ID)
require.Nil(t, status.Status.Progress)
require.Equal(t, 1, status.Status.LastCreated)
select {
case msg := <-sub.Ch:
require.NotNil(t, msg.Event)
require.Equal(t, "issue.created", msg.Event.Type)
case <-time.After(5 * time.Second):
require.FailNow(t, "missing committed issue event")
}
}
func TestServiceNotionInitialSourceAndGitHubRepositoryProgress(t *testing.T) {
ctx := context.Background()
fetcher := newRuntimeNotionFetcher()
github := &runtimeGitHubSourceFetcher{started: make(chan struct{})}
service, err := newService(ctx, Config{DSN: filepath.Join(t.TempDir(), "service.db"), Auth: AuthConfig{TrustCallerAuthentication: true}}, serviceDeps{notionSyncFetcher: fetcher, gitHubSyncFetcher: github})
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, service.Close()) })
p, err := service.store.CreateProject(ctx, "example-project")
require.NoError(t, err)
binding := runtimeNotionBinding(t, service.store, p.ID)
g, err := service.store.CreateProject(ctx, "other-project")
require.NoError(t, err)
raw, err := githubsync.EncodeConfig(githubsync.Config{Host: "github.com", Owner: "example-owner", Repo: "example-repo", RepoID: 12345})
require.NoError(t, err)
_, err = service.store.UpsertIssueSyncBinding(ctx, db.UpsertIssueSyncBindingParams{ProjectID: g.ID, Provider: "github", SourceKey: "github:R_exampleNode", RemoteID: "R_exampleNode", DisplayName: "example-owner/example-repo", Config: raw, IntervalSeconds: 300})
require.NoError(t, err)
runCtx, cancel := context.WithCancel(ctx)
done := make(chan error, 1)
go func() { done <- service.Run(runCtx) }()
t.Cleanup(func() { cancel(); require.NoError(t, <-done) })
waitRuntimeNotion(t, fetcher.sourceStarted)
waitRuntimeNotion(t, github.started)
status := readRuntimeNotionStatus(t, service.Handler(), p.ID)
require.NotNil(t, status.Status.Progress)
require.Equal(t, "source", status.Status.Progress.Phase)
durable, err := service.store.IssueSyncStatusByProject(ctx, p.ID)
require.NoError(t, err)
require.Equal(t, "source", service.notionSyncProgress.Snapshot(binding.ID, *durable.SyncStartedAt).Phase)
response := httptest.NewRecorder()
service.Handler().ServeHTTP(response, httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/projects/%d/issue-sync/github/status", g.ID), nil))
require.Equal(t, 200, response.Code)
var out runtimeNotionStatus
require.NoError(t, json.Unmarshal(response.Body.Bytes(), &out))
require.NotNil(t, out.Status.Progress)
require.Equal(t, "repository", out.Status.Progress.Phase)
}
type runtimeGitHubSourceFetcher struct {
serviceGitHubFetcher
started chan struct{}
}
func (f *runtimeGitHubSourceFetcher) Repository(ctx context.Context, _, _, _ string) (githubsync.Repository, error) {
close(f.started)
<-ctx.Done()
return githubsync.Repository{}, ctx.Err()
}
func TestServiceNotionConfigRejectsInvalidSelector(t *testing.T) {
_, err := New(context.Background(), Config{DSN: filepath.Join(t.TempDir(), "service.db"), Auth: AuthConfig{TrustCallerAuthentication: true}, NotionSync: NotionSyncConfig{TokenEnv: "INVALID=NAME"}})
require.ErrorContains(t, err, "notion_sync.token_env")
}
const runtimeNotionSourceID = "11111111-1111-1111-1111-111111111111"
const runtimeNotionDatabaseID = "22222222-2222-2222-2222-222222222222"
type runtimeNotionFetcher struct {
sourceStarted, releaseSource, contentStarted, releaseContent chan struct{}
sourceOnce, contentOnce sync.Once
}
func newRuntimeNotionFetcher() *runtimeNotionFetcher {
return &runtimeNotionFetcher{sourceStarted: make(chan struct{}), releaseSource: make(chan struct{}), contentStarted: make(chan struct{}), releaseContent: make(chan struct{})}
}
func (f *runtimeNotionFetcher) ForRun(context.Context) (notionsync.Session, error) { return f, nil }
func (f *runtimeNotionFetcher) DataSource(ctx context.Context, id string) (notionsync.DataSource, error) {
f.sourceOnce.Do(func() { close(f.sourceStarted) })
select {
case <-f.releaseSource:
case <-ctx.Done():
return notionsync.DataSource{}, ctx.Err()
}
return notionsync.DataSource{ID: id, DatabaseID: runtimeNotionDatabaseID, Name: "Tasks", Properties: []notionsync.Property{
{ID: "title", Name: "Task", Type: "title"}, {ID: "status", Name: "State", Type: "status", Options: []notionsync.Option{{ID: "done", Name: "Delivered"}}}, {ID: "people", Name: "Owner", Type: "people"},
}}, nil
}
func (*runtimeNotionFetcher) Database(context.Context, string) (notionsync.Database, error) {
return notionsync.Database{ID: runtimeNotionDatabaseID, DataSources: []notionsync.Option{{ID: runtimeNotionSourceID, Name: "Tasks"}}}, nil
}
func (*runtimeNotionFetcher) Pages(context.Context, notionsync.Config, *time.Time) ([]notionsync.Page, error) {
return []notionsync.Page{{ID: "33333333-3333-3333-3333-333333333333", URL: "https://www.notion.so/33333333333333333333333333333333", DataSourceID: runtimeNotionSourceID, CreatedAt: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC), UpdatedAt: time.Date(2026, 1, 2, 0, 0, 0, 0, time.UTC)}}, nil
}
func (f *runtimeNotionFetcher) Content(ctx context.Context, _ notionsync.Config, p notionsync.Page) (notionsync.PageContent, error) {
f.contentOnce.Do(func() { close(f.contentStarted) })
select {
case <-f.releaseContent:
case <-ctx.Done():
return notionsync.PageContent{}, ctx.Err()
}
return notionsync.PageContent{Page: p, Title: "Scheduled task", Markdown: "Body"}, nil
}
func runtimeNotionBinding(t *testing.T, store db.Storage, projectID int64) db.IssueSyncBinding {
t.Helper()
raw, err := notionsync.EncodeConfig(notionsync.Config{DataSourceID: runtimeNotionSourceID, DatabaseID: runtimeNotionDatabaseID, TitlePropertyID: "title", StatusPropertyID: "status", AssigneePropertyID: "people", DoneStatusIDs: []string{"done"}})
require.NoError(t, err)
binding, err := store.UpsertIssueSyncBinding(context.Background(), db.UpsertIssueSyncBindingParams{ProjectID: projectID, Provider: "notion", SourceKey: "notion:" + runtimeNotionSourceID, RemoteID: runtimeNotionSourceID, DisplayName: "Tasks", Config: raw, IntervalSeconds: 300})
require.NoError(t, err)
return binding
}
type runtimeNotionStatus struct {
Status struct {
State string `json:"state"`
LastCreated int `json:"last_created"`
Progress *struct {
Phase string `json:"phase"`
Completed, Total int
StartedAt time.Time `json:"started_at"`
} `json:"progress"`
} `json:"status"`
}
func readRuntimeNotionStatus(t *testing.T, handler http.Handler, projectID int64) runtimeNotionStatus {
t.Helper()
response := httptest.NewRecorder()
handler.ServeHTTP(response, httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/projects/%d/issue-sync/notion/status", projectID), nil))
require.Equal(t, http.StatusOK, response.Code, response.Body.String())
var status runtimeNotionStatus
require.NoError(t, json.Unmarshal(response.Body.Bytes(), &status))
return status
}
func waitRuntimeNotion(t *testing.T, started <-chan struct{}) {
t.Helper()
select {
case <-started:
case <-time.After(5 * time.Second):
require.FailNow(t, "scheduled Notion worker did not reach upstream read")
}
}
func newRuntimeNotionPost(path string, body io.Reader) *http.Request {
request := httptest.NewRequest(http.MethodPost, path, body)
request.Header.Set("Content-Type", "application/json")
return request
}
func TestServiceNotionExplicitCredentialDoesNotFallBack(t *testing.T) {
t.Setenv("KATA_NOTION_TOKEN", "default-example-token")
t.Setenv("EXAMPLE_NOTION_TOKEN", "")
service, err := New(context.Background(), Config{DSN: filepath.Join(t.TempDir(), "service.db"), Auth: AuthConfig{TrustCallerAuthentication: true}, NotionSync: NotionSyncConfig{TokenEnv: " EXAMPLE_NOTION_TOKEN "}})
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, service.Close()) })
project, err := service.store.CreateProject(context.Background(), "example-project")
require.NoError(t, err)
enable := httptest.NewRecorder()
service.Handler().ServeHTTP(enable, newRuntimeNotionPost(fmt.Sprintf("/api/v1/projects/%d/issue-sync/notion/enable", project.ID), strings.NewReader(`{"config":{"data_source_id":"11111111-1111-1111-1111-111111111111","done_statuses":["Delivered"]}}`)))
require.Equal(t, http.StatusBadRequest, enable.Code, enable.Body.String())
require.Contains(t, enable.Body.String(), "credentials")
runtimeNotionBinding(t, service.store, project.ID)
recording := &runtimeNotionRecordingStore{Storage: service.store, recorded: make(chan db.IssueSyncStatus, 1)}
service.store = recording
ctx, cancel := context.WithCancel(context.Background())
done := make(chan error, 1)
go func() { done <- service.Run(ctx) }()
defer func() { cancel(); require.NoError(t, <-done) }()
status := waitRuntimeNotionRecord(t, recording.recorded)
require.NotNil(t, status.LastErrorAt)
require.Nil(t, status.LastSuccessAt)
}
// runtimeNotionRecordingStore exposes the real durable completion to tests.
// It delegates all product storage behavior before signaling the observer.
type runtimeNotionRecordingStore struct {
db.Storage
recorded chan db.IssueSyncStatus
}
func (s *runtimeNotionRecordingStore) RecordIssueSyncSuccess(ctx context.Context, params db.IssueSyncSuccessParams) (db.IssueSyncStatus, error) {
status, err := s.Storage.RecordIssueSyncSuccess(ctx, params)
if err == nil {
s.recorded <- status
}
return status, err
}
func (s *runtimeNotionRecordingStore) RecordIssueSyncError(ctx context.Context, params db.IssueSyncErrorParams) (db.IssueSyncStatus, error) {
status, err := s.Storage.RecordIssueSyncError(ctx, params)
if err == nil {
s.recorded <- status
}
return status, err
}
func waitRuntimeNotionRecord(t *testing.T, recorded <-chan db.IssueSyncStatus) db.IssueSyncStatus {
t.Helper()
select {
case status := <-recorded:
return status
case <-time.After(5 * time.Second):
require.FailNow(t, "scheduled Notion worker did not record durable completion")
}
return db.IssueSyncStatus{}
}