Fix round 1: prove THIS write is unflushed, not merely that the WAL is non-empty
TestSealedWriteIsUnflushedBeforeExplicitCheckpoint now reads back the just-written event and asserts its unique sealed payload bytes (random AES-GCM nonce per call) appear in the uncheckpointed WAL, rather than only checking the WAL file's size. A non-empty WAL was never evidence that this specific insert was the thing sitting unflushed.
1 file changed,
+29
−7
internal/sheets/store/wal_gate_test.go+29 −7
Intent
diff --git a/internal/sheets/store/wal_gate_test.go b/internal/sheets/store/wal_gate_test.go| index 42d2ca2..e55b95d 100644 |
| --- a/internal/sheets/store/wal_gate_test.go |
| +++ b/internal/sheets/store/wal_gate_test.go |
| @@ -141,13 +141,28 @@ func TestSealedWriteIsUnflushedBeforeExplicitCheckpoint(t *testing.T) { |
| t.Fatal(err) |
| } |
| - // Before any explicit checkpoint call: the WAL file must already exist |
| - // and be non-empty. If it is missing or empty here, the insert above |
| - // was already folded into the main file by something other than an |
| - // explicit checkpoint — a connection-close checkpoint, pool churn, |
| - // AutoMigrate side effects, or journal_mode defaulting to something |
| - // other than WAL — and the sealed-path test's checkpoint() call would |
| - // be a no-op. |
| + // Read back exactly the row just written, so the check below can prove |
| + // THIS write's bytes are unflushed — not merely that the WAL file is |
| + // non-empty, which a completely unrelated pending write could also |
| + // satisfy and would say nothing about this insert specifically. |
| + var written store.Event |
| + if err := db.Where("kind = ?", store.KindInput).Order("id DESC"). |
| + First(&written).Error; err != nil { |
| + t.Fatalf("read back the just-written event: %v", err) |
| + } |
| + if len(written.PayloadSealed) == 0 { |
| + t.Fatal("just-written event has no sealed payload to trace into the WAL") |
| + } |
| + |
| + // Before any explicit checkpoint call: the WAL file must already exist, |
| + // be non-empty, and contain THIS row's sealed payload bytes verbatim |
| + // (the AES-GCM ciphertext is unique per call, courtesy of its random |
| + // nonce, so this is not a coincidental match). If any of that is not |
| + // so, the insert above was already folded into the main file by |
| + // something other than an explicit checkpoint — a connection-close |
| + // checkpoint, pool churn, AutoMigrate side effects, or journal_mode |
| + // defaulting to something other than WAL — and the sealed-path test's |
| + // checkpoint() call would be a no-op. |
| info, err := os.Stat(dbPath + "-wal") |
| if err != nil { |
| t.Fatalf("stat WAL before any explicit checkpoint: %v", err) |
| @@ -155,6 +170,13 @@ func TestSealedWriteIsUnflushedBeforeExplicitCheckpoint(t *testing.T) { |
| if info.Size() == 0 { |
| t.Fatal("WAL file is empty before the explicit checkpoint call; the write was already flushed by something implicit, which would make checkpoint() a no-op") |
| } |
| + walBytes, err := os.ReadFile(dbPath + "-wal") |
| + if err != nil { |
| + t.Fatalf("read WAL before any explicit checkpoint: %v", err) |
| + } |
| + if !bytes.Contains(walBytes, written.PayloadSealed) { |
| + t.Fatal("this write's sealed payload is not present in the uncheckpointed WAL bytes; a non-empty WAL file is not evidence that THIS write specifically is still unflushed") |
| + } |
| var autoVacuum int |
| if err := db.Raw("PRAGMA auto_vacuum").Scan(&autoVacuum).Error; err != nil { |