oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package plan3test
2
3import (
4 "io"
5 "log/slog"
6 "path/filepath"
7 "testing"
8
9 "amadan.net/oficina/equipo/internal/equipo"
10 "amadan.net/rastrillo/rastrillo/db"
11 "amadan.net/rastrillo/rastrillo/migrate"
12)
13
14func TestPersonAPIMigrationUpgrade(t *testing.T) {
15 path := filepath.Join(t.TempDir(), "person-api.db")
16 logger := slog.New(slog.NewTextHandler(io.Discard, nil))
17 d, err := db.Open(path, logger)
18 must(t, err)
19 old := &migrate.Set{}
20 for _, m := range equipo.BootSchema.All() {
21 if m.ID != "equipo/0023_person_api_requests" {
22 old.Add(m)
23 }
24 }
25 _, err = migrate.Apply(ctx, d, old)
26 must(t, err)
27 s := equipo.NewStore(d)
28 must(t, s.Bootstrap(ctx, []string{owner.Address}))
29 msg, err := s.SendMessage(ctx, owner, "", "historical message", equipo.Mutation{RequestID: "historical"})
30 must(t, err)
31 result, err := migrate.Apply(ctx, d, equipo.BootSchema)
32 must(t, err)
33 if len(result.Applied) != 1 || result.Applied[0] != "equipo/0023_person_api_requests" {
34 t.Fatal(result.Applied)
35 }
36 f := newFixture(t)
37 upgraded, err := migrate.Read(ctx, d.Writer())
38 must(t, err)
39 fresh, err := migrate.Read(ctx, f.DB.Writer())
40 must(t, err)
41 if !upgraded.Equal(fresh) {
42 t.Fatal(upgraded.Diff(fresh))
43 }
44 must(t, d.Close())
45 d, err = db.Open(path, logger)
46 must(t, err)
47 t.Cleanup(func() { must(t, d.Close()) })
48 result, err = migrate.Apply(ctx, d, equipo.BootSchema)
49 must(t, err)
50 if len(result.Applied) != 0 {
51 t.Fatal("reapplied", result.Applied)
52 }
53 page, err := equipo.NewStore(d).Conversation(ctx, owner, "", 50)
54 must(t, err)
55 if len(page.Messages) != 2 || page.Messages[0].ID != msg.ID {
56 t.Fatal("historical message lost")
57 }
58}
59
60func TestPersonAPIRequestMetadataConstraintsAndExpiry(t *testing.T) {
61 f := newFixture(t)
62 if f.count(t, "SELECT count(*) FROM pragma_table_info('person_api_requests') WHERE \"notnull\"=0") != 0 {
63 t.Fatal("nullable required metadata")
64 }
65 if f.count(t, "SELECT count(*) FROM pragma_index_info('person_api_requests_expiry') WHERE name='expires_at'") != 1 {
66 t.Fatal("missing expiry index")
67 }
68 expectHTTP(t, apiRequest(t, f, owner, "POST", "/api/v1/conversation/messages", `{"body":"old"}`, "old", ""), 200)
69 f.exec(t, "UPDATE person_api_requests SET expires_at='2000-01-01T00:00:00Z' WHERE request_id='old'")
70 expectHTTP(t, apiRequest(t, f, owner, "POST", "/api/v1/conversation/messages", `{"body":"new"}`, "new", ""), 200)
71 if f.count(t, "SELECT count(*) FROM person_api_requests WHERE request_id='old'") != 0 {
72 t.Fatal("expired replay metadata retained")
73 }
74}
75
76func TestPersonAPIDeviceMigrationUpgrade(t *testing.T) {
77 path := filepath.Join(t.TempDir(), "person-devices.db")
78 logger := slog.New(slog.NewTextHandler(io.Discard, nil))
79 d, err := db.Open(path, logger)
80 must(t, err)
81 old := &migrate.Set{}
82 for _, m := range equipo.BootSchema.All() {
83 if m.ID != "equipo/0098_native_device" && m.ID != "equipo_identity/0002_native_lifecycle" && m.ID != "equipo/0030_person_devices" && m.ID != "equipo/0030_person_devices_usage" && m.ID != "equipo/0031_notifications" && m.ID != "equipo/0033_plan_notifications" {
84 old.Add(m)
85 }
86 }
87 _, err = migrate.Apply(ctx, d, old)
88 must(t, err)
89 s := equipo.NewStore(d)
90 must(t, s.Bootstrap(ctx, []string{owner.Address}))
91 msg, err := s.SendMessage(ctx, owner, "", "historical message", equipo.Mutation{RequestID: "historical"})
92 must(t, err)
93 result, err := migrate.Apply(ctx, d, equipo.BootSchema)
94 must(t, err)
95 if len(result.Applied) != 6 || result.Applied[0] != "equipo/0030_person_devices" || result.Applied[1] != "equipo/0030_person_devices_usage" || result.Applied[2] != "equipo/0031_notifications" {
96 t.Fatal(result.Applied)
97 }
98 f := newFixture(t)
99 upgraded, err := migrate.Read(ctx, d.Writer())
100 must(t, err)
101 fresh, err := migrate.Read(ctx, f.DB.Writer())
102 must(t, err)
103 if !upgraded.Equal(fresh) {
104 t.Fatal(upgraded.Diff(fresh))
105 }
106 must(t, d.Close())
107 d, err = db.Open(path, logger)
108 must(t, err)
109 t.Cleanup(func() { must(t, d.Close()) })
110 result, err = migrate.Apply(ctx, d, equipo.BootSchema)
111 must(t, err)
112 if len(result.Applied) != 0 {
113 t.Fatal("reapplied", result.Applied)
114 }
115 page, err := equipo.NewStore(d).Conversation(ctx, owner, "", 50)
116 must(t, err)
117 if len(page.Messages) != 2 || page.Messages[0].ID != msg.ID {
118 t.Fatal("historical message lost")
119 }
120}
121
122func TestPersonAPIDeviceUsageMigration(t *testing.T) {
123 path := filepath.Join(t.TempDir(), "device-usage.db")
124 d, err := db.Open(path, slog.New(slog.NewTextHandler(io.Discard, nil)))
125 must(t, err)
126 t.Cleanup(func() { must(t, d.Close()) })
127 old := &migrate.Set{}
128 for _, m := range equipo.BootSchema.All() {
129 if m.ID != "equipo/0030_person_devices_usage" {
130 old.Add(m)
131 }
132 }
133 _, err = migrate.Apply(ctx, d, old)
134 must(t, err)
135 _, err = d.Writer().Exec(`INSERT INTO person_devices(id,request_hash,person,token_hash,revision) VALUES('history','request','person','hash',3)`)
136 must(t, err)
137 result, err := migrate.Apply(ctx, d, equipo.BootSchema)
138 must(t, err)
139 if len(result.Applied) != 1 || result.Applied[0] != "equipo/0030_person_devices_usage" {
140 t.Fatal(result.Applied)
141 }
142 var count int
143 must(t, d.Writer().QueryRow(`SELECT count(*) FROM person_devices WHERE id='history' AND token_hash='hash' AND revision=3 AND last_used_at IS NULL`).Scan(&count))
144 if count != 1 {
145 t.Fatal("usage migration lost device history")
146 }
147}
148