|
1 | 1 | --!nocheck |
2 | 2 | -- Offline test of the notifications' pure parts (plans/29) under Lune: the signed launch token (the contract's shared |
3 | 3 | -- vector, sign -> verify, wrong user, too old, previous key within / after 7 days, bad sig, format), the key hash, the |
4 | | --- intent checks, the queue (rev order, batching at 200, the body JSON, requeue after a failure, a swap's handover) and |
5 | | --- the client's opt-in rows. Run after `bun run build`: |
| 4 | +-- intent checks, the queue (rev order, batching at 200, the body JSON, requeue after a failure, a swap's handover), |
| 5 | +-- the prefs item (setOff) and the client's opt-in rows. Run after `bun run build`: |
6 | 6 | -- cd ../kernel && lune run ../framework/scripts/test-notifications.luau ../framework/ |
7 | 7 | local fs = require("@lune/fs") |
8 | 8 | local luau = require("@lune/luau") |
|
307 | 307 | check("opened state: junk", P.readOpenedState({ state = "x" }) == nil and P.readOpenedState(nil) == nil) |
308 | 308 | end |
309 | 309 |
|
| 310 | +-- Prefs (setOff): the user's switched-off kinds ------------------------------------------------------------------------------- |
| 311 | +do |
| 312 | + local prefs = P.cleanPrefs(42, { "streak", "gift_new", "streak", "daily" }, 11) |
| 313 | + check("prefs: item, unique and sorted", type(prefs) == "table" and prefs.op == "prefs" and prefs.u == 42 and table.concat(prefs.off, ",") == "daily,gift_new,streak" and prefs.rev == 11, prefs) |
| 314 | + check("prefs: empty = every kind on", type(P.cleanPrefs(42, {}, 1)) == "table" and #P.cleanPrefs(42, {}, 1).off == 0) |
| 315 | + check("prefs: bad user", type(P.cleanPrefs(0, {}, 1)) == "string" and type(P.cleanPrefs(1.5, {}, 1)) == "string") |
| 316 | + check("prefs: bad kind", type(P.cleanPrefs(1, { "Gift" }, 1)) == "string" and type(P.cleanPrefs(1, { string.rep("a", 25) }, 1)) == "string" and type(P.cleanPrefs(1, { 5 }, 1)) == "string") |
| 317 | + check("prefs: not a list", type(P.cleanPrefs(1, "gift", 1)) == "string" and type(P.cleanPrefs(1, { a = "gift" }, 1)) == "string" and type(P.cleanPrefs(1, nil, 1)) == "string") |
| 318 | + local many = {} |
| 319 | + for index = 1, P.OFF_MAX + 1 do |
| 320 | + table.insert(many, "k" .. index) |
| 321 | + end |
| 322 | + check("prefs: over the cap", type(P.cleanPrefs(1, many, 1)) == "string") |
| 323 | + local capped = table.clone(many) |
| 324 | + capped[P.OFF_MAX + 1] = "k1" |
| 325 | + check("prefs: duplicates don't count toward the cap", type(P.cleanPrefs(1, capped, 1)) == "table" and #P.cleanPrefs(1, capped, 1).off == P.OFF_MAX) |
| 326 | + check("encode prefs", P.encodeItem(prefs, quote) == '{"op":"prefs","u":42,"off":["daily","gift_new","streak"],"rev":11}', P.encodeItem(prefs, quote)) |
| 327 | + check("encode prefs: empty list", P.encodeItem(P.cleanPrefs(7, {}, 1762500000123), quote) == '{"op":"prefs","u":7,"off":[],"rev":1762500000123}') |
| 328 | + check("encode prefs: valid JSON", serde.decode("json", P.encodeItem(prefs, quote)).off[2] == "gift_new") |
| 329 | + |
| 330 | + -- The queue keeps one prefs item per user (the newest), behind the other items; batching and requeue as any item. |
| 331 | + local state = P.newQueueState() |
| 332 | + P.pushItem(state, P.cleanWant(42, "daily", nil, P.nextRev(state, 1762500000))) |
| 333 | + P.pushPrefs(state, P.cleanPrefs(42, { "daily" }, P.nextRev(state, 1762500000))) |
| 334 | + P.pushPrefs(state, P.cleanPrefs(43, { "streak" }, P.nextRev(state, 1762500000))) |
| 335 | + P.pushPrefs(state, P.cleanPrefs(42, {}, P.nextRev(state, 1762500000))) |
| 336 | + check("prefs queue: one per user, the newest last", #state.items == 3 and state.items[1].op == "want" and state.items[2].u == 43 and state.items[3].u == 42 and #state.items[3].off == 0) |
| 337 | + check("prefs queue: revs keep the call order", state.items[3].rev > state.items[2].rev and state.items[2].rev > state.items[1].rev) |
| 338 | + local batch, body = unpackBatch(P.takeBatch(state, quote)) |
| 339 | + local parsed = serde.decode("json", body) |
| 340 | + check("prefs batch: body", #batch == 3 and parsed.items[3].op == "prefs" and #parsed.items[3].off == 0 and parsed.items[2].off[1] == "streak") |
| 341 | + local handed = serde.decode("json", serde.encode("json", { items = batch })) |
| 342 | + check("prefs: plain data across a swap", handed.items[2].off[1] == "streak") |
| 343 | +end |
| 344 | + |
310 | 345 | -- Opt-in rows from a client ---------------------------------------------------------------------------------------------------- |
311 | 346 | do |
312 | 347 | local check_ = P.cleanOptInRow({ name = "optin_check", where = "streak_2", can = true, extra = "dropped" }) |
|
0 commit comments