5be7f7f04b
- Qt 6 desktop client with day/month/year views, AI scheduling, and TOML settings\n- Go sync server with SQLite change stream, WebSocket notifications, and Docker deployment\n- Client outbox/LWW sync, offline retry, and dual-client integration tests\n- Linux fcitx5 setup, validation reports, and Windows MinGW preset
79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/misaki/Schedule/server/internal/model"
|
|
)
|
|
|
|
func newStore(t *testing.T) *SQLiteStore {
|
|
t.Helper()
|
|
st, err := Open(filepath.Join(t.TempDir(), "sync.db"))
|
|
if err != nil {
|
|
t.Fatalf("open store: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = st.Close() })
|
|
return st
|
|
}
|
|
|
|
func TestRegisterAndLookupDevice(t *testing.T) {
|
|
st := newStore(t)
|
|
ctx := context.Background()
|
|
device := model.Device{ID: "device-1", Name: "Laptop", TokenHash: "hash-1"}
|
|
if err := st.RegisterDevice(ctx, device); err != nil {
|
|
t.Fatalf("register: %v", err)
|
|
}
|
|
got, err := st.DeviceByTokenHash(ctx, "hash-1")
|
|
if err != nil {
|
|
t.Fatalf("lookup: %v", err)
|
|
}
|
|
if got.ID != device.ID || got.Name != device.Name {
|
|
t.Fatalf("unexpected device: %+v", got)
|
|
}
|
|
if _, err := st.DeviceByTokenHash(ctx, "missing"); err != ErrNotFound {
|
|
t.Fatalf("expected ErrNotFound, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestAppendAndPullChanges(t *testing.T) {
|
|
st := newStore(t)
|
|
ctx := context.Background()
|
|
payload := json.RawMessage(`{"id":"task-1","title":"写周报"}`)
|
|
inputs := []model.ChangeInput{
|
|
{EntityType: "task", EntityID: "task-1", Op: "upsert", Version: 1, UpdatedAt: "2026-10-26T10:00:00Z", Payload: payload},
|
|
{EntityType: "timeblock", EntityID: "block-1", Op: "upsert", Version: 1, UpdatedAt: "2026-10-26T10:00:00Z", Payload: json.RawMessage(`{"id":"block-1"}`)},
|
|
}
|
|
last, err := st.AppendChanges(ctx, "device-1", inputs)
|
|
if err != nil {
|
|
t.Fatalf("append: %v", err)
|
|
}
|
|
if last != 2 {
|
|
t.Fatalf("last seq = %d, want 2", last)
|
|
}
|
|
changes, latest, hasMore, err := st.ChangesSince(ctx, 0, 1)
|
|
if err != nil {
|
|
t.Fatalf("pull: %v", err)
|
|
}
|
|
if len(changes) != 1 || !hasMore || latest != 2 {
|
|
t.Fatalf("pull result changes=%d hasMore=%v latest=%d", len(changes), hasMore, latest)
|
|
}
|
|
changes, latest, hasMore, err = st.ChangesSince(ctx, 1, 10)
|
|
if err != nil {
|
|
t.Fatalf("pull second: %v", err)
|
|
}
|
|
if len(changes) != 1 || changes[0].EntityID != "block-1" || hasMore || latest != 2 {
|
|
t.Fatalf("unexpected second pull: %+v latest=%d hasMore=%v", changes, latest, hasMore)
|
|
}
|
|
|
|
export, err := st.Export(ctx)
|
|
if err != nil {
|
|
t.Fatalf("export: %v", err)
|
|
}
|
|
if len(export.Changes) != 2 || export.LatestSeq != 2 {
|
|
t.Fatalf("unexpected export: %+v", export)
|
|
}
|
|
}
|