sl-map-web 0.6.0

Web UI and JSON API for the SL map renderer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! DB-level invariants for the `themes` table (migration `0013_themes.sql`):
//! the Personal-or-Group XOR CHECK, per-user and per-group isolation, the
//! `ON DELETE CASCADE` from both owner columns, and the `created_by`
//! `ON DELETE SET NULL` that keeps a group theme alive after its creator
//! deletes their account. Colour-format validation is enforced at the API
//! surface (see the `ThemeSettings::validate` unit tests in
//! `src/routes/themes.rs`), not by the schema, so it is not exercised here.

#![allow(
    clippy::expect_used,
    reason = "test code panics on assert failure for clearer failure output"
)]
#![allow(
    clippy::missing_docs_in_private_items,
    reason = "test helpers do not need doc comments"
)]
#![allow(
    clippy::tests_outside_test_module,
    reason = "integration tests live in tests/, not in a #[cfg(test)] module"
)]

use std::str::FromStr as _;

use pretty_assertions::assert_eq;
use sqlx::SqlitePool;
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};

const USER_A: [u8; 16] = [1; 16];
const USER_B: [u8; 16] = [2; 16];
const GROUP_G: [u8; 16] = [9; 16];
const THEME_1: [u8; 16] = [11; 16];
const THEME_2: [u8; 16] = [12; 16];

const SETTINGS: &str = r#"{"version":1,"draw_region_names":true}"#;

async fn open_in_memory() -> SqlitePool {
    let options = SqliteConnectOptions::from_str("sqlite::memory:")
        .expect("static in-memory URL parses")
        .foreign_keys(true);
    let pool = SqlitePoolOptions::new()
        .max_connections(1)
        .connect_with(options)
        .await
        .expect("open in-memory pool");
    sl_map_web::db::MIGRATIONS
        .run(&pool)
        .await
        .expect("apply migrations");
    pool
}

async fn insert_user(pool: &SqlitePool, user_id: &[u8], username: &str) {
    sqlx::query(
        "INSERT INTO users (user_id, legacy_name, username, created_at, updated_at) \
         VALUES (?1, ?2, ?3, ?4, ?4)",
    )
    .bind(user_id)
    .bind(username)
    .bind(username)
    .bind("2026-01-01")
    .execute(pool)
    .await
    .expect("insert user");
}

async fn insert_group(pool: &SqlitePool, group_id: &[u8], created_by: &[u8], name: &str) {
    sqlx::query(
        "INSERT INTO \"groups\" (group_id, name, created_by, created_at, updated_at) \
         VALUES (?1, ?2, ?3, ?4, ?4)",
    )
    .bind(group_id)
    .bind(name)
    .bind(created_by)
    .bind("2026-01-01")
    .execute(pool)
    .await
    .expect("insert group");
}

async fn insert_membership(pool: &SqlitePool, group_id: &[u8], user_id: &[u8], role: &str) {
    sqlx::query(
        "INSERT INTO group_memberships (group_id, user_id, role, created_at) \
         VALUES (?1, ?2, ?3, ?4)",
    )
    .bind(group_id)
    .bind(user_id)
    .bind(role)
    .bind("2026-01-01")
    .execute(pool)
    .await
    .expect("insert membership");
}

/// Insert a theme directly. Exactly one of `owner_user_id` / `owner_group_id`
/// should be `Some` for a valid row; the tests pass other combinations on
/// purpose to exercise the CHECK constraint.
async fn insert_theme(
    pool: &SqlitePool,
    theme_id: &[u8],
    owner_user_id: Option<&[u8]>,
    owner_group_id: Option<&[u8]>,
    created_by: &[u8],
    name: &str,
) -> Result<(), sqlx::Error> {
    sqlx::query(
        "INSERT INTO themes \
            (theme_id, owner_user_id, owner_group_id, created_by, name, \
             settings_json, created_at, updated_at) \
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?7)",
    )
    .bind(theme_id)
    .bind(owner_user_id)
    .bind(owner_group_id)
    .bind(created_by)
    .bind(name)
    .bind(SETTINGS)
    .bind("2026-01-01")
    .execute(pool)
    .await
    .map(|_| ())
}

async fn personal_theme_names(pool: &SqlitePool, user_id: &[u8]) -> Vec<String> {
    sqlx::query_scalar("SELECT name FROM themes WHERE owner_user_id = ?1 ORDER BY created_at, name")
        .bind(user_id)
        .fetch_all(pool)
        .await
        .expect("list personal themes")
}

async fn group_theme_names(pool: &SqlitePool, group_id: &[u8]) -> Vec<String> {
    sqlx::query_scalar(
        "SELECT name FROM themes WHERE owner_group_id = ?1 ORDER BY created_at, name",
    )
    .bind(group_id)
    .fetch_all(pool)
    .await
    .expect("list group themes")
}

#[tokio::test]
async fn xor_check_rejects_both_and_neither_owner() {
    let pool = open_in_memory().await;
    insert_user(&pool, USER_A.as_slice(), "alice.one").await;
    insert_group(&pool, GROUP_G.as_slice(), USER_A.as_slice(), "g").await;

    let both = insert_theme(
        &pool,
        THEME_1.as_slice(),
        Some(USER_A.as_slice()),
        Some(GROUP_G.as_slice()),
        USER_A.as_slice(),
        "both",
    )
    .await;
    assert!(
        both.is_err(),
        "a theme cannot be owned by a user AND a group"
    );

    let neither = insert_theme(
        &pool,
        THEME_2.as_slice(),
        None,
        None,
        USER_A.as_slice(),
        "neither",
    )
    .await;
    assert!(neither.is_err(), "a theme must have exactly one owner");
}

#[tokio::test]
async fn theme_names_are_unique_within_a_scope_but_not_across_scopes() {
    let pool = open_in_memory().await;
    insert_user(&pool, USER_A.as_slice(), "alice.one").await;
    insert_user(&pool, USER_B.as_slice(), "bob.two").await;
    insert_group(&pool, GROUP_G.as_slice(), USER_A.as_slice(), "g").await;

    insert_theme(
        &pool,
        THEME_1.as_slice(),
        Some(USER_A.as_slice()),
        None,
        USER_A.as_slice(),
        "dusk",
    )
    .await
    .expect("first personal theme inserts");

    // Same name, same personal scope → rejected.
    let dup = insert_theme(
        &pool,
        THEME_2.as_slice(),
        Some(USER_A.as_slice()),
        None,
        USER_A.as_slice(),
        "dusk",
    )
    .await;
    assert!(dup.is_err(), "a user cannot have two themes named 'dusk'");

    // Same name in a *different* scope (another user, and a group) → allowed.
    insert_theme(
        &pool,
        THEME_2.as_slice(),
        Some(USER_B.as_slice()),
        None,
        USER_B.as_slice(),
        "dusk",
    )
    .await
    .expect("another user may reuse the name");
    insert_theme(
        &pool,
        [13; 16].as_slice(),
        None,
        Some(GROUP_G.as_slice()),
        USER_A.as_slice(),
        "dusk",
    )
    .await
    .expect("a group may reuse a name a member uses personally");
}

#[tokio::test]
async fn personal_themes_are_per_user() {
    let pool = open_in_memory().await;
    insert_user(&pool, USER_A.as_slice(), "alice.one").await;
    insert_user(&pool, USER_B.as_slice(), "bob.two").await;

    insert_theme(
        &pool,
        THEME_1.as_slice(),
        Some(USER_A.as_slice()),
        None,
        USER_A.as_slice(),
        "a-theme",
    )
    .await
    .expect("insert A's theme");

    assert_eq!(
        personal_theme_names(&pool, USER_A.as_slice()).await,
        vec!["a-theme".to_owned()],
        "user A sees their own theme",
    );
    assert!(
        personal_theme_names(&pool, USER_B.as_slice())
            .await
            .is_empty(),
        "user B sees none of user A's personal themes",
    );
}

#[tokio::test]
async fn group_themes_belong_to_their_group() {
    let pool = open_in_memory().await;
    insert_user(&pool, USER_A.as_slice(), "alice.one").await;
    insert_group(&pool, GROUP_G.as_slice(), USER_A.as_slice(), "g").await;
    insert_membership(&pool, GROUP_G.as_slice(), USER_A.as_slice(), "owner").await;

    insert_theme(
        &pool,
        THEME_1.as_slice(),
        None,
        Some(GROUP_G.as_slice()),
        USER_A.as_slice(),
        "shared",
    )
    .await
    .expect("insert group theme");

    assert_eq!(
        group_theme_names(&pool, GROUP_G.as_slice()).await,
        vec!["shared".to_owned()],
        "the group lists its theme",
    );
    assert!(
        personal_theme_names(&pool, USER_A.as_slice())
            .await
            .is_empty(),
        "a group theme is not a personal theme of its creator",
    );
}

#[tokio::test]
async fn deleting_a_user_cascades_their_personal_themes() {
    let pool = open_in_memory().await;
    insert_user(&pool, USER_A.as_slice(), "alice.one").await;
    insert_user(&pool, USER_B.as_slice(), "bob.two").await;
    insert_theme(
        &pool,
        THEME_1.as_slice(),
        Some(USER_A.as_slice()),
        None,
        USER_A.as_slice(),
        "a-theme",
    )
    .await
    .expect("insert A's theme");
    insert_theme(
        &pool,
        THEME_2.as_slice(),
        Some(USER_B.as_slice()),
        None,
        USER_B.as_slice(),
        "b-theme",
    )
    .await
    .expect("insert B's theme");

    sqlx::query("DELETE FROM users WHERE user_id = ?1")
        .bind(USER_A.as_slice())
        .execute(&pool)
        .await
        .expect("delete user A");

    assert!(
        personal_theme_names(&pool, USER_A.as_slice())
            .await
            .is_empty(),
        "user A's personal themes are cascaded away",
    );
    assert_eq!(
        personal_theme_names(&pool, USER_B.as_slice()).await,
        vec!["b-theme".to_owned()],
        "user B's themes are untouched",
    );
}

#[tokio::test]
async fn deleting_a_group_cascades_its_themes() {
    let pool = open_in_memory().await;
    insert_user(&pool, USER_A.as_slice(), "alice.one").await;
    insert_group(&pool, GROUP_G.as_slice(), USER_A.as_slice(), "g").await;
    insert_membership(&pool, GROUP_G.as_slice(), USER_A.as_slice(), "owner").await;
    insert_theme(
        &pool,
        THEME_1.as_slice(),
        None,
        Some(GROUP_G.as_slice()),
        USER_A.as_slice(),
        "shared",
    )
    .await
    .expect("insert group theme");

    sqlx::query("DELETE FROM \"groups\" WHERE group_id = ?1")
        .bind(GROUP_G.as_slice())
        .execute(&pool)
        .await
        .expect("delete group");

    assert!(
        group_theme_names(&pool, GROUP_G.as_slice())
            .await
            .is_empty(),
        "the group's themes are cascaded away",
    );
}

#[tokio::test]
async fn deleting_creator_keeps_group_theme_and_nulls_created_by() {
    let pool = open_in_memory().await;
    // The group is created by A so that deleting B (the theme creator and a
    // group owner) is not blocked by groups.created_by ON DELETE RESTRICT.
    insert_user(&pool, USER_A.as_slice(), "alice.one").await;
    insert_user(&pool, USER_B.as_slice(), "bob.two").await;
    insert_group(&pool, GROUP_G.as_slice(), USER_A.as_slice(), "g").await;
    insert_membership(&pool, GROUP_G.as_slice(), USER_A.as_slice(), "owner").await;
    insert_membership(&pool, GROUP_G.as_slice(), USER_B.as_slice(), "owner").await;
    insert_theme(
        &pool,
        THEME_1.as_slice(),
        None,
        Some(GROUP_G.as_slice()),
        USER_B.as_slice(),
        "shared",
    )
    .await
    .expect("insert group theme created by B");

    sqlx::query("DELETE FROM users WHERE user_id = ?1")
        .bind(USER_B.as_slice())
        .execute(&pool)
        .await
        .expect("delete user B");

    // The group theme survives, but its created_by link is severed.
    assert_eq!(
        group_theme_names(&pool, GROUP_G.as_slice()).await,
        vec!["shared".to_owned()],
        "the group theme outlives its creator's account",
    );
    let created_by: Option<Vec<u8>> =
        sqlx::query_scalar("SELECT created_by FROM themes WHERE theme_id = ?1")
            .bind(THEME_1.as_slice())
            .fetch_one(&pool)
            .await
            .expect("fetch created_by");
    assert!(
        created_by.is_none(),
        "created_by is nulled by ON DELETE SET NULL",
    );
}