Files
pages/internal/store/seheweit_history_test.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts.

Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation.

Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
2026-11-01 15:23:31 +09:00

125 lines
4.1 KiB
Go

package store
import (
"context"
"errors"
"fmt"
"testing"
"time"
"pageswiki/internal/seheweit"
)
func resourceTestStore(t *testing.T) *Store {
t.Helper()
s := newSchemaTestStore(t)
if err := s.EnsureResourceHistorySchema(); err != nil {
t.Fatal(err)
}
return s
}
func TestSeheweitHistoryBucketCompatibility(t *testing.T) {
s := resourceTestStore(t)
now := time.Date(2026, 11, 1, 12, 0, 0, 0, time.UTC)
// Boundary-inclusive query. Later insertion, not later timestamp, wins a
// bucket, matching the old MAX(id) query even with out-of-order samples.
for i, tc := range []struct {
offset time.Duration
payload string
}{
{-time.Hour - time.Millisecond, `{"label":"too-old"}`},
{-time.Hour, `{"label":"start"}`},
{-10 * time.Second, `{"label":"later-time"}`},
{-20 * time.Second, `{"label":"last-insert"}`},
{0, `{"label":"end"}`},
{time.Millisecond, `{"label":"future"}`},
} {
if err := s.SaveResourceSample(fmt.Sprint(i), now.Add(tc.offset).UnixMilli(), []byte(tc.payload)); err != nil {
t.Fatal(err)
}
}
got, err := s.ResourceHistory(t.Context(), 1, now)
if err != nil {
t.Fatal(err)
}
want := []string{`{"label":"start"}`, `{"label":"last-insert"}`, `{"label":"end"}`}
if len(got) != len(want) {
t.Fatalf("samples %s", got)
}
for i := range want {
if string(got[i]) != want[i] {
t.Fatalf("sample %d: %s", i, got[i])
}
}
}
func TestSeheweitHistoryRetentionAndIdempotence(t *testing.T) {
s := resourceTestStore(t)
at := time.Date(2026, 11, 1, 12, 0, 0, 0, time.UTC).UnixMilli()
cutoff := seheweit.RetentionCutoff(at)
for _, tc := range []struct {
job string
at int64
}{{"expired", cutoff - 1}, {"boundary", cutoff}, {"current", at}} {
if err := s.SaveResourceSample(tc.job, tc.at, []byte(`{"original":true}`)); err != nil {
t.Fatal(err)
}
}
if err := s.SaveResourceSample("current", at+1, []byte(`{"replacement":true}`)); err != nil {
t.Fatal(err)
}
var payload string
if err := s.DB.QueryRow(`SELECT payload FROM resource_samples WHERE job_uuid='current'`).Scan(&payload); err != nil || payload != `{"original":true}` {
t.Fatalf("retry replaced sample: %s %v", payload, err)
}
// The retry uses its collection time for pruning, so the old cutoff is now
// expired too. A distinct test covers exact retention-boundary preservation.
var count int
if err := s.DB.QueryRow(`SELECT COUNT(*) FROM resource_samples`).Scan(&count); err != nil || count != 1 {
t.Fatalf("retention count=%d %v", count, err)
}
s2 := resourceTestStore(t)
if err := s2.SaveResourceSample("boundary", cutoff, []byte(`{}`)); err != nil {
t.Fatal(err)
}
if err := s2.SaveResourceSample("current", at, []byte(`{}`)); err != nil {
t.Fatal(err)
}
if err := s2.DB.QueryRow(`SELECT COUNT(*) FROM resource_samples`).Scan(&count); err != nil || count != 2 {
t.Fatalf("inclusive cutoff count=%d %v", count, err)
}
}
func TestSeheweitHistoryCancelledReadOnlyAndRollback(t *testing.T) {
s := resourceTestStore(t)
at := time.Now().UnixMilli()
ctx, cancel := context.WithCancel(t.Context())
cancel()
if err := s.SaveResourceSampleContext(ctx, "cancelled", at, []byte(`{}`)); !errors.Is(err, context.Canceled) {
t.Fatalf("cancel: %v", err)
}
if _, err := s.ResourceHistory(ctx, 24, time.Now()); !errors.Is(err, context.Canceled) {
t.Fatalf("read cancel: %v", err)
}
s.ReadOnly = true
if err := s.SaveResourceSample("readonly", at, []byte(`{}`)); !errors.Is(err, ErrReadOnly) {
t.Fatalf("readonly: %v", err)
}
s.ReadOnly = false
if err := s.SaveResourceSample("old", seheweit.RetentionCutoff(at)-1, []byte(`{}`)); err != nil {
t.Fatal(err)
}
// Failure during expiration must roll back the insertion as well.
if _, err := s.DB.Exec(`CREATE TRIGGER prevent_resource_expire BEFORE DELETE ON resource_samples BEGIN SELECT RAISE(ABORT,'synthetic failure'); END`); err != nil {
t.Fatal(err)
}
if err := s.SaveResourceSample("rollback", at, []byte(`{}`)); err == nil {
t.Fatal("expiration failure ignored")
}
var count int
if err := s.DB.QueryRow(`SELECT COUNT(*) FROM resource_samples WHERE job_uuid<>'old'`).Scan(&count); err != nil || count != 0 {
t.Fatalf("partial transaction or cancelled write count=%d %v", count, err)
}
}