Files
pages/internal/calendar/calendar_test.go
T
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

248 lines
7.4 KiB
Go

package calendar
import (
"context"
"database/sql"
"errors"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/google/uuid"
_ "modernc.org/sqlite"
)
func fixture(t *testing.T) (*Repository, string, string, Calendar) {
t.Helper()
db, e := sql.Open("sqlite", filepath.Join(t.TempDir(), "cache.db"))
if e != nil {
t.Fatal(e)
}
db.SetMaxOpenConns(1)
t.Cleanup(func() { db.Close() })
r, e := New(t.TempDir(), db)
if e != nil {
t.Fatal(e)
}
domain, actor := uuid.NewString(), uuid.NewString()
c, e := r.Create(context.Background(), domain, actor, "予定")
if e != nil {
t.Fatal(e)
}
return r, domain, actor, c
}
func sample(t *testing.T, uid string, start, end string, allDay bool) Event {
t.Helper()
a, e := time.Parse(time.RFC3339, start)
if e != nil {
t.Fatal(e)
}
b, e := time.Parse(time.RFC3339, end)
if e != nil {
t.Fatal(e)
}
v, e := Build("", Event{UID: uid, Summary: "会議, 東京; test\n次行", Start: a, End: b, AllDay: allDay})
if e != nil {
t.Fatal(e)
}
return v
}
func TestCalendarICalendarValidation(t *testing.T) {
v := sample(t, "a@example.org", "2026-09-30T23:00:00Z", "2026-10-01T02:00:00Z", false)
long := v
long.Summary = strings.Repeat("日本語", 100)
long, e := Build(v.Data, long)
if e != nil {
t.Fatal(e)
}
for _, line := range strings.Split(long.Data, "\r\n") {
if len(line) > 75 {
t.Fatal("unfolded long output line")
}
}
if !strings.Contains(v.Data, "\r\n") || !strings.Contains(v.Data, `SUMMARY:会議\, 東京\; test\n次行`) {
t.Fatal(v.Data)
}
for _, data := range []string{
strings.Replace(v.Data, "DTSTART:", "DTSTART;TZID=Asia/Tokyo:", 1),
strings.Replace(v.Data, "20260930T230000Z", "20260930T230000", 1),
strings.Replace(v.Data, "END:VEVENT", "RRULE:FREQ=DAILY\r\nEND:VEVENT", 1),
strings.Replace(v.Data, "END:VEVENT", "UID:duplicate\r\nEND:VEVENT", 1),
strings.Replace(v.Data, "20261001T020000Z", "20260930T220000Z", 1),
strings.Replace(v.Data, "VERSION:2.0", "VERSION:1.0", 1),
v.Data + v.Data,
} {
if _, e := Parse(data); !errors.Is(e, ErrInvalid) {
t.Fatalf("accepted invalid input: %v\n%s", e, data)
}
}
extended := strings.Replace(v.Data, "END:VEVENT", "X-PAGESWIKI-TEST:keep-me\r\nEND:VEVENT", 1)
v, e = Parse(extended)
if e != nil {
t.Fatal(e)
}
v.Summary = "changed"
changed, e := Build(v.Data, v)
if e != nil || !strings.Contains(changed.Data, "X-PAGESWIKI-TEST:keep-me") {
t.Fatal(e, changed.Data)
}
}
func TestCalendarMonthlyCommitIndexAndACL(t *testing.T) {
r, domain, actor, c := fixture(t)
ctx := context.Background()
v := sample(t, "stable", "2026-09-30T23:00:00Z", "2026-10-01T02:00:00Z", false)
if _, e := r.Change(ctx, domain, c.ID, actor, v.UID, "*", &v); e != nil {
t.Fatal(e)
}
lo := time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC)
items, e := r.Query(ctx, domain, c.ID, actor, lo, lo.Add(24*time.Hour))
if e != nil || len(items) != 1 {
t.Fatal(items, e)
}
if _, e = r.Event(ctx, domain, c.ID, "", v.UID); !errors.Is(e, ErrMissing) {
t.Fatal("anonymous read", e)
}
if _, e = r.Change(ctx, domain, c.ID, actor, v.UID, "*", &v); !errors.Is(e, ErrConflict) {
t.Fatal("duplicate overwrite", e)
}
moved := sample(t, v.UID, "2026-11-01T01:00:00Z", "2026-11-01T02:00:00Z", false)
if _, e = r.Change(ctx, domain, c.ID, actor, v.UID, v.ETag, &moved); e != nil {
t.Fatal(e)
}
// Simulated lost/discarded cache must reconstruct only the committed manifest.
if _, e = r.DB.Exec(`DELETE FROM calendar_event_cache;DELETE FROM calendar_month_cache;DELETE FROM calendar_index_state`); e != nil {
t.Fatal(e)
}
got, e := r.Event(ctx, domain, c.ID, actor, v.UID)
if e != nil || !got.Start.Equal(moved.Start) {
t.Fatal(got, e)
}
c, e = r.Get(domain, c.ID, actor)
if e != nil {
t.Fatal(e)
}
if len(c.Months) != 1 || c.Months["2026-11"] == "" {
t.Fatal(c.Months)
}
other := uuid.NewString()
c, e = r.Settings(ctx, domain, c.ID, actor, c.Version, c.Name, true, map[string]string{other: "read"})
if e != nil {
t.Fatal(e)
}
if _, e = r.Event(ctx, domain, c.ID, "", v.UID); e != nil {
t.Fatal(e)
}
if _, e = r.Change(ctx, domain, c.ID, other, v.UID, moved.ETag, nil); !errors.Is(e, ErrForbidden) {
t.Fatal(e)
}
if _, e = r.Settings(ctx, domain, c.ID, other, c.Version, "bad", false, nil); !errors.Is(e, ErrForbidden) {
t.Fatal(e)
}
if _, e = r.Change(ctx, domain, c.ID, actor, v.UID, moved.ETag, nil); e != nil {
t.Fatal(e)
}
if _, e = r.Event(ctx, domain, c.ID, actor, v.UID); !errors.Is(e, ErrMissing) {
t.Fatal(e)
}
}
func TestCalendarConcurrentConditionalWrite(t *testing.T) {
r, domain, actor, c := fixture(t)
ctx := context.Background()
v := sample(t, "race", "2026-09-20T00:00:00Z", "2026-09-21T00:00:00Z", true)
if _, e := r.Change(ctx, domain, c.ID, actor, v.UID, "*", &v); e != nil {
t.Fatal(e)
}
var wg sync.WaitGroup
results := make(chan error, 2)
for _, title := range []string{"one", "two"} {
wg.Add(1)
go func(title string) {
defer wg.Done()
n := v
n.Summary = title
n, e := Build(v.Data, n)
if e == nil {
_, e = r.Change(ctx, domain, c.ID, actor, v.UID, v.ETag, &n)
}
results <- e
}(title)
}
wg.Wait()
close(results)
ok, conflict := 0, 0
for e := range results {
if e == nil {
ok++
} else if errors.Is(e, ErrConflict) {
conflict++
} else {
t.Fatal(e)
}
}
if ok != 1 || conflict != 1 {
t.Fatal(ok, conflict)
}
}
func TestCalendarDeleteRemovesCanonicalFilesCacheAndDefaults(t *testing.T) {
r, domain, actor, created := fixture(t)
ctx := context.Background()
event := sample(t, "delete-me", "2026-09-22T09:00:00Z", "2026-09-22T10:00:00Z", false)
if _, err := r.Change(ctx, domain, created.ID, actor, event.UID, "*", &event); err != nil {
t.Fatal(err)
}
if err := r.SaveDefaults(ctx, domain, actor, []string{created.ID}); err != nil {
t.Fatal(err)
}
fresh, err := r.Get(domain, created.ID, actor)
if err != nil {
t.Fatal(err)
}
if err = r.Delete(ctx, domain, created.ID, uuid.NewString(), fresh.Version); !errors.Is(err, ErrForbidden) {
t.Fatal("non-owner delete", err)
}
if err = r.Delete(ctx, domain, created.ID, actor, created.Version); !errors.Is(err, ErrConflict) {
t.Fatal("stale delete", err)
}
dir, _ := r.dir(domain, created.ID)
if err = r.Delete(ctx, domain, created.ID, actor, fresh.Version); err != nil {
t.Fatal(err)
}
if _, err = os.Stat(dir); !os.IsNotExist(err) {
t.Fatal("canonical calendar remains", err)
}
if _, err = r.Get(domain, created.ID, actor); !errors.Is(err, ErrMissing) {
t.Fatal("deleted calendar readable", err)
}
defaults, err := r.Defaults(domain, actor)
if err != nil || len(defaults) != 0 {
t.Fatal("deleted default retained", defaults, err)
}
for _, table := range []string{"calendar_event_cache", "calendar_month_cache", "calendar_index_state"} {
var count int
if err = r.DB.QueryRow(`SELECT COUNT(*) FROM `+table+` WHERE domain=? AND calendar=?`, domain, created.ID).Scan(&count); err != nil || count != 0 {
t.Fatal(table, count, err)
}
}
}
func TestCalendarSnapshotsVerified(t *testing.T) {
r, d, a, c := fixture(t)
v := sample(t, "broken", "2026-09-01T00:00:00Z", "2026-09-02T00:00:00Z", true)
if _, e := r.Change(context.Background(), d, c.ID, a, v.UID, "*", &v); e != nil {
t.Fatal(e)
}
c, _ = r.Get(d, c.ID, a)
dir, _ := r.dir(d, c.ID)
for m, h := range c.Months {
if e := os.WriteFile(filepath.Join(dir, m+"."+h+".ics"), []byte("corrupt"), 0600); e != nil {
t.Fatal(e)
}
}
if _, e := r.Event(context.Background(), d, c.ID, a, v.UID); e == nil {
t.Fatal("corrupt snapshot accepted")
}
}