| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "strconv" |
| 8 | "sync" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/sheets/internal/sheets/store" |
| 13 | "gorm.io/gorm" |
| 14 | ) |
| 15 | |
| 16 | func TestAgentSheetCreateRangeRetryAndBrowserEdit(t *testing.T) { |
| 17 | f := newSheetAgentFixture(t) |
| 18 | input := initialSheetAgentInput() |
| 19 | created := decodeSheetCreated(t, f.call(t, "sheets_create", input)) |
| 20 | rec := f.call(t, "sheets_get_range", f.rangeInput(created)) |
| 21 | if rec.Code != 200 { |
| 22 | t.Fatal(rec.Code, rec.Body.String()) |
| 23 | } |
| 24 | var snapshot agentRange |
| 25 | if e := json.Unmarshal(rec.Body.Bytes(), &snapshot); e != nil { |
| 26 | t.Fatal(e) |
| 27 | } |
| 28 | if snapshot.Revision != created.Revision || len(snapshot.Cells) != 4 || snapshot.Cells[0].Value != float64(21) || snapshot.Cells[2].Value != float64(42) { |
| 29 | t.Fatal("incoherent formula result", snapshot) |
| 30 | } |
| 31 | var wg sync.WaitGroup |
| 32 | results := make(chan sheetAgentCreated, 4) |
| 33 | for i := 0; i < 4; i++ { |
| 34 | wg.Add(1) |
| 35 | go func() { defer wg.Done(); results <- decodeSheetCreated(t, f.call(t, "sheets_create", input)) }() |
| 36 | } |
| 37 | wg.Wait() |
| 38 | close(results) |
| 39 | for got := range results { |
| 40 | if got != created { |
| 41 | t.Fatal("retry changed receipt", got) |
| 42 | } |
| 43 | } |
| 44 | reversed := input |
| 45 | reversed.Cells = append([]sheetAgentCell(nil), input.Cells...) |
| 46 | reversed.Cells[0], reversed.Cells[2] = reversed.Cells[2], reversed.Cells[0] |
| 47 | if got := decodeSheetCreated(t, f.call(t, "sheets_create", reversed)); got != created { |
| 48 | t.Fatal("cell order changed meaning") |
| 49 | } |
| 50 | changed := input |
| 51 | changed.Title = "Changed title" |
| 52 | if rec = f.call(t, "sheets_create", changed); rec.Code != 409 { |
| 53 | t.Fatal("changed retry", rec.Code) |
| 54 | } |
| 55 | tab, _ := strconv.ParseUint(created.TabID, 10, 64) |
| 56 | h, e := store.Open(f.a.app.db, f.a.app.sealer, tab) |
| 57 | if e != nil { |
| 58 | t.Fatal(e) |
| 59 | } |
| 60 | defer h.Release() |
| 61 | member, e := f.a.member(t.Context(), f.p, true) |
| 62 | if e != nil { |
| 63 | t.Fatal(e) |
| 64 | } |
| 65 | if _, e = h.SetBatch([]store.CellWrite{{Row: 0, Col: 0, Raw: "8"}}, member.ID); e != nil { |
| 66 | t.Fatal(e) |
| 67 | } |
| 68 | retried := decodeSheetCreated(t, f.call(t, "sheets_create", input)) |
| 69 | if retried.SheetID != created.SheetID || retried.Revision == created.Revision || retried.Revision != strconv.FormatUint(h.Version(), 10) { |
| 70 | t.Fatal("retry reset or mislabelled revision", retried) |
| 71 | } |
| 72 | var count int64 |
| 73 | f.a.app.db.Model(&store.Sheet{}).Count(&count) |
| 74 | if count != 1 { |
| 75 | t.Fatal("duplicate sheet", count) |
| 76 | } |
| 77 | // Query actual database paths; plaintext authored data must not reach DB/WAL. |
| 78 | var dbs []struct { |
| 79 | Seq int |
| 80 | Name, File string |
| 81 | } |
| 82 | if e = f.a.app.db.Raw("PRAGMA database_list").Scan(&dbs).Error; e != nil { |
| 83 | t.Fatal(e) |
| 84 | } |
| 85 | for _, db := range dbs { |
| 86 | if db.Name != "main" { |
| 87 | continue |
| 88 | } |
| 89 | for _, suffix := range []string{"", "-wal"} { |
| 90 | raw, e := os.ReadFile(db.File + suffix) |
| 91 | if e != nil && !os.IsNotExist(e) { |
| 92 | t.Fatal(e) |
| 93 | } |
| 94 | for _, canary := range []string{input.Title, input.TabName, "PRIVATE_CELL_CANARY_42"} { |
| 95 | if bytes.Contains(raw, []byte(canary)) { |
| 96 | t.Fatal("plaintext persisted", canary, suffix) |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | if e = f.a.app.db.Model(&store.Sheet{}).Where("id=?", created.SheetID).Update("deleted_at", time.Now()).Error; e != nil { |
| 102 | t.Fatal(e) |
| 103 | } |
| 104 | if rec = f.call(t, "sheets_create", input); rec.Code != 404 { |
| 105 | t.Fatal("deleted replay recreated", rec.Code) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestAgentSheetAtomicRollbackAndKeyAuthority(t *testing.T) { |
| 110 | f := newSheetAgentFixture(t) |
| 111 | input := initialSheetAgentInput() |
| 112 | for _, table := range []string{"grants", "cells", "sheets_agent_attribution", "sheets_agent_receipts"} { |
| 113 | if e := f.a.app.db.Exec("CREATE TRIGGER agent_failure BEFORE INSERT ON " + table + " BEGIN SELECT RAISE(ABORT,'injected failure'); END").Error; e != nil { |
| 114 | t.Fatal(e) |
| 115 | } |
| 116 | if rec := f.call(t, "sheets_create", input); rec.Code != 503 { |
| 117 | t.Error("write failure not surfaced", rec.Code) |
| 118 | } |
| 119 | for _, model := range []any{&store.Sheet{}, &store.Tab{}, &Grant{}, &store.Cell{}, &store.Row{}, &store.Col{}, &sheetAgentReceipt{}, &sheetAgentAttribution{}} { |
| 120 | var count int64 |
| 121 | if e := f.a.app.db.Model(model).Count(&count).Error; e != nil || count != 0 { |
| 122 | t.Fatal("partial sheet persisted", count, e) |
| 123 | } |
| 124 | } |
| 125 | if e := f.a.app.db.Exec("DROP TRIGGER agent_failure").Error; e != nil { |
| 126 | t.Fatal(e) |
| 127 | } |
| 128 | } |
| 129 | decodeSheetCreated(t, f.call(t, "sheets_create", input)) |
| 130 | for _, change := range []func(*sheetAgent){func(a *sheetAgent) { a.key = bytes.Repeat([]byte{19}, 32) }, func(a *sheetAgent) { a.identityIssuer = "https://other-home.example" }, func(a *sheetAgent) { a.issuer = "https://other-connect.example" }, func(a *sheetAgent) { a.team = "green" }} { |
| 131 | copy := *f.a |
| 132 | change(©) |
| 133 | if e := copy.app.db.Transaction(func(tx *gorm.DB) error { return copy.bind(tx) }); e == nil { |
| 134 | t.Fatal("authority/key change accepted") |
| 135 | } |
| 136 | } |
| 137 | if e := f.a.app.db.Transaction(func(tx *gorm.DB) error { return f.a.bind(tx) }); e != nil { |
| 138 | t.Fatal("same authority refused", e) |
| 139 | } |
| 140 | } |
| 141 | |