1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
 2//
 3// SPDX-License-Identifier: AGPL-3.0-or-later
 4
 5package goal_test
 6
 7import (
 8	"context"
 9	"errors"
10	"testing"
11	"time"
12
13	"git.secluded.site/np/internal/goal"
14	"git.secluded.site/np/internal/testutil"
15	"git.secluded.site/np/internal/timeutil"
16)
17
18func TestStoreSetAndGet(t *testing.T) {
19	ctx := context.Background()
20	db := testutil.OpenDB(t)
21
22	fixed := time.Date(2025, time.January, 12, 8, 30, 0, 0, time.FixedZone("TEST", 2*3600))
23	store := goal.NewStore(db, timeutil.FixedClock{Time: fixed})
24
25	doc, err := store.Set(ctx, "sid-1", "  My Goal  ", "describe")
26	if err != nil {
27		t.Fatalf("Set: %v", err)
28	}
29
30	if want, got := "My Goal", doc.Title; want != got {
31		t.Fatalf("Title mismatch: want %q got %q", want, got)
32	}
33	if want, got := "describe", doc.Description; want != got {
34		t.Fatalf("Description mismatch: want %q got %q", want, got)
35	}
36	if want, got := fixed.UTC(), doc.UpdatedAt; !want.Equal(got) {
37		t.Fatalf("UpdatedAt mismatch: want %v got %v", want, got)
38	}
39
40	loaded, err := store.Get(ctx, "sid-1")
41	if err != nil {
42		t.Fatalf("Get: %v", err)
43	}
44	if loaded != doc {
45		t.Fatalf("Loaded doc mismatch: want %+v got %+v", doc, loaded)
46	}
47
48	exists, err := store.Exists(ctx, "sid-1")
49	if err != nil {
50		t.Fatalf("Exists: %v", err)
51	}
52	if !exists {
53		t.Fatalf("Exists returned false")
54	}
55
56	exists, err = store.Exists(ctx, "sid-unknown")
57	if err != nil {
58		t.Fatalf("Exists unknown: %v", err)
59	}
60	if exists {
61		t.Fatalf("Exists should be false for unknown session")
62	}
63}
64
65func TestStoreGetMissing(t *testing.T) {
66	ctx := context.Background()
67	db := testutil.OpenDB(t)
68	store := goal.NewStore(db, timeutil.FixedClock{Time: time.Now()})
69
70	_, err := store.Get(ctx, "missing")
71	if !errors.Is(err, goal.ErrNotFound) {
72		t.Fatalf("expected ErrNotFound, got %v", err)
73	}
74}
75
76func TestStoreSetRequiresTitle(t *testing.T) {
77	ctx := context.Background()
78	db := testutil.OpenDB(t)
79	store := goal.NewStore(db, timeutil.FixedClock{Time: time.Now()})
80
81	if _, err := store.Set(ctx, "sid", "   ", "desc"); !errors.Is(err, goal.ErrEmptyTitle) {
82		t.Fatalf("expected ErrEmptyTitle, got %v", err)
83	}
84}