| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | |
| 8 | "gorm.io/gorm" |
| 9 | ) |
| 10 | |
| 11 | // I19's fence half (Notion import spec §14.2): bookkeeping may touch |
| 12 | // only notion_* tables. A bookkeeping function that writes pages, |
| 13 | // revisions, aliases, title prints or events is refused and changes |
| 14 | // nothing; one that writes the import's own tables is not. |
| 15 | // |
| 16 | // Mutation (make mutate, first red 2026-11-17): the fence returns |
| 17 | // without checking the table. |
| 18 | func TestI19BookkeepingIsFenced(t *testing.T) { |
| 19 | store := testStore(t) |
| 20 | ctx := context.Background() |
| 21 | if _, err := store.CreateTeamPage(ctx, "ana@example.com", "Acme", |
| 22 | "Body."); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | for _, table := range []string{"pages", "revisions", "aliases", |
| 26 | "title_prints", "events", "images"} { |
| 27 | err := store.bookkeep(ctx, runLease{}, func(db *gorm.DB) error { |
| 28 | return db.Table(table).Where("1 = 1"). |
| 29 | Update("id", gorm.Expr("id")).Error |
| 30 | }) |
| 31 | if !errors.Is(err, errOutsideBookkeeping) { |
| 32 | t.Errorf("bookkeeping wrote %s: %v", table, err) |
| 33 | } |
| 34 | } |
| 35 | err := store.bookkeep(ctx, runLease{}, func(db *gorm.DB) error { |
| 36 | return db.Create(&NotionRun{State: "done", Lease: 1}).Error |
| 37 | }) |
| 38 | if err != nil { |
| 39 | t.Errorf("bookkeeping could not write its own table: %v", err) |
| 40 | } |
| 41 | } |
| 42 | |