| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { mkdtempSync } from "node:fs"; |
| 4 | import { tmpdir } from "node:os"; |
| 5 | import { join } from "node:path"; |
| 6 | import { StateDatabase } from "../src/persistence/stateDb.js"; |
| 7 | |
| 8 | test("first account from auth becomes game admin", () => |
| 9 | { |
| 10 | const dir = mkdtempSync(join(tmpdir(), "questward-auth-")); |
| 11 | const db = new StateDatabase(join(dir, "state.sqlite")); |
| 12 | const first = db.resolveAccountFromAuth({ |
| 13 | authSubject: "auth:1", |
| 14 | email: "first@example.com", |
| 15 | username: "first" |
| 16 | }); |
| 17 | assert.equal(first.is_admin, 1); |
| 18 | const second = db.resolveAccountFromAuth({ |
| 19 | authSubject: "auth:2", |
| 20 | email: "second@example.com", |
| 21 | username: "second" |
| 22 | }); |
| 23 | assert.equal(second.is_admin, 0); |
| 24 | }); |
| 25 | |
| 26 | test("grant admin by username and block removing last admin", () => |
| 27 | { |
| 28 | const dir = mkdtempSync(join(tmpdir(), "questward-auth-")); |
| 29 | const db = new StateDatabase(join(dir, "state.sqlite")); |
| 30 | db.resolveAccountFromAuth({ authSubject: "auth:10", username: "alpha", email: "a@b.c" }); |
| 31 | db.resolveAccountFromAuth({ authSubject: "auth:11", username: "beta", email: "b@c.d" }); |
| 32 | db.grantGameAdminByUsername("beta"); |
| 33 | assert.equal(db.listGameAdmins().length, 2); |
| 34 | db.setGameAdmin("auth:11", false); |
| 35 | assert.equal(db.listGameAdmins().length, 1); |
| 36 | assert.throws( |
| 37 | () => db.setGameAdmin("auth:10", false), |
| 38 | (err) => err.userFacing && /last game admin/i.test(err.message) |
| 39 | ); |
| 40 | }); |
| 41 | |
| 42 | test("grant admin by username requires prior sign-in", () => |
| 43 | { |
| 44 | const dir = mkdtempSync(join(tmpdir(), "questward-auth-")); |
| 45 | const db = new StateDatabase(join(dir, "state.sqlite")); |
| 46 | assert.throws( |
| 47 | () => db.grantGameAdminByUsername("nobody"), |
| 48 | (err) => err.userFacing && /signed in yet/i.test(err.message) |
| 49 | ); |
| 50 | }); |
| 51 | |
| 52 | test("resolveAccountFromAuth works without email and preserves stored email", () => |
| 53 | { |
| 54 | const dir = mkdtempSync(join(tmpdir(), "questward-auth-")); |
| 55 | const db = new StateDatabase(join(dir, "state.sqlite")); |
| 56 | const created = db.resolveAccountFromAuth({ |
| 57 | authSubject: "auth:99", |
| 58 | email: "keep@example.com", |
| 59 | username: "keeper" |
| 60 | }); |
| 61 | assert.equal(created.email, "keep@example.com"); |
| 62 | const again = db.resolveAccountFromAuth({ |
| 63 | authSubject: "auth:99", |
| 64 | email: null, |
| 65 | username: "keeper" |
| 66 | }); |
| 67 | assert.equal(again.account_id, created.account_id); |
| 68 | assert.equal(again.email, "keep@example.com"); |
| 69 | assert.equal(again.username, "keeper"); |
| 70 | }); |
| 71 | |
| 72 | test("new account can be created with only auth subject and username", () => |
| 73 | { |
| 74 | const dir = mkdtempSync(join(tmpdir(), "questward-auth-")); |
| 75 | const db = new StateDatabase(join(dir, "state.sqlite")); |
| 76 | const account = db.resolveAccountFromAuth({ |
| 77 | authSubject: "auth:7", |
| 78 | username: "noemail" |
| 79 | }); |
| 80 | assert.equal(account.auth_subject, "auth:7"); |
| 81 | assert.equal(account.username, "noemail"); |
| 82 | assert.equal(account.email, null); |
| 83 | assert.equal(account.is_admin, 1); |
| 84 | }); |