@bongos/core 1.19.251 → 1.19.252

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,150 @@
1
+ // tests/government_board_membership_db.mjs — the board franchise against a REAL
2
+ // Postgres (task 1003094).
3
+ //
4
+ // WHY THIS EXISTS BESIDE THE DB-FREE government_board_membership.mjs. That file
5
+ // covers the pure parser and the resolvers against a stubbed db, which is where
6
+ // the grammar lives. It cannot cover the half this task actually changed: the
7
+ // SQL. `WHERE rank_key = $1` became `WHERE rank_key = ANY($1)` plus a DISTINCT,
8
+ // and a FRANCHISE bug is precisely the kind a stub agrees with and the database
9
+ // does not — the stub returns whatever the test author imagined the query means.
10
+ //
11
+ // What only real SQL can answer:
12
+ // * does `rank:metic+` genuinely seat the Archon (the regression that cost an
13
+ // owner their seat on their own constitution);
14
+ // * do the two STRUCTURAL filters survive the rewrite — a deactivated builder
15
+ // and a machine principal must still never govern, because a widened
16
+ // franchise that also admits a bot is a worse bug than the one being fixed;
17
+ // * does DISTINCT actually hold when a builder matches through two keys — a
18
+ // double-counted member silently raises the bar a consent vote must clear.
19
+ //
20
+ // It SELF-SKIPS (exit 0) with no reachable Postgres, and is listed in
21
+ // run-unit-tests.js's INTEGRATION set so the DB-free lane never runs it.
22
+ //
23
+ // Run: DATABASE_URL=postgres://... node tests/government_board_membership_db.mjs
24
+ //
25
+ // It creates its own throwaway `builders` / `government_builder_ranks` rows at
26
+ // ids far above any real row, and deletes them at the end.
27
+
28
+ import pg from 'pg';
29
+ import { createRequire } from 'node:module';
30
+ const require = createRequire(import.meta.url);
31
+
32
+ const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL || undefined });
33
+
34
+ try {
35
+ await pool.query('SELECT 1');
36
+ } catch (e) {
37
+ console.log(`government_board_membership_db.mjs SKIPPED — no reachable Postgres (${e.code || e.message}). Set DATABASE_URL to run.`);
38
+ await pool.end().catch(() => {});
39
+ process.exit(0);
40
+ }
41
+ let passed = 0, failed = 0;
42
+ async function t(name, fn) {
43
+ try { await fn(); passed++; console.log(` ok ${name}`); }
44
+ catch (e) { failed++; console.log(` FAIL ${name}\n ${e.message}`); }
45
+ }
46
+ const assert = (await import('node:assert')).strict;
47
+
48
+ // Minimal stand-ins ONLY when the real tables are absent (throwaway container);
49
+ // against a migrated database these are no-ops and the real tables are used.
50
+ await pool.query(`
51
+ CREATE TABLE IF NOT EXISTS builders (
52
+ id bigserial PRIMARY KEY, github_login text, display_name text,
53
+ status text DEFAULT 'active', system_role text);
54
+ CREATE TABLE IF NOT EXISTS government_builder_ranks (
55
+ builder_id bigint, rank_key text);
56
+ `);
57
+
58
+ // Ids far above any real row, and every login prefixed, so cleanup is exact and
59
+ // this can never touch a real builder or a real rank assignment.
60
+ const B = 990000000;
61
+ const ID = { xenos: B + 1, thetes: B + 2, metic: B + 3, archon: B + 4, gone: B + 5, bot: B + 6, ops: B + 7 };
62
+ const cleanup = async () => {
63
+ await pool.query('DELETE FROM government_builder_ranks WHERE builder_id >= $1', [B]);
64
+ await pool.query('DELETE FROM builders WHERE id >= $1', [B]);
65
+ };
66
+ await cleanup();
67
+ await pool.query(`INSERT INTO builders (id, github_login, display_name, status, system_role) VALUES
68
+ ($1,'gbmdb-x','Xenos','active',NULL),($2,'gbmdb-t','Thetes','active',NULL),
69
+ ($3,'gbmdb-m','Metic','active',NULL),($4,'gbmdb-a','Archon','active',NULL),
70
+ ($5,'gbmdb-gone','Deactivated Archon','inactive',NULL),
71
+ ($6,'gbmdb-bot','A Machine','active','bfg'),
72
+ ($7,'gbmdb-ops','Custom Ops','active',NULL)`,
73
+ [ID.xenos, ID.thetes, ID.metic, ID.archon, ID.gone, ID.bot, ID.ops]);
74
+ await pool.query(`INSERT INTO government_builder_ranks (builder_id, rank_key) VALUES
75
+ ($1,'xenos'),($2,'thetes'),($3,'metic'),($4,'archon'),($5,'archon'),($6,'archon'),($7,'gbmdb-ops-rank')`,
76
+ [ID.xenos, ID.thetes, ID.metic, ID.archon, ID.gone, ID.bot, ID.ops]);
77
+
78
+ const api = require(process.cwd() + '/src/module-api.js');
79
+ api.pool.query = (...a) => pool.query(...a);
80
+ const { resolveBoardMembers, isBoardMember } = require(process.cwd() + '/modules/government/board-membership.js');
81
+
82
+ const board = (membership) => ({ membership, pass_rule: 'consent', window_minutes: null, close_early_on_full_turnout: true });
83
+ const logins = async (m) => (await resolveBoardMembers(board(m)))
84
+ .filter((r) => Number(r.builder_id) >= B) // ignore any real rows on a migrated db
85
+ .map((r) => r.github_login.replace('gbmdb-', ''));
86
+
87
+ console.log('the predicate against real SQL');
88
+
89
+ await t('bare rank:archon is unchanged — exactly archons', async () => {
90
+ assert.deepEqual(await logins('rank:archon'), ['a']);
91
+ });
92
+
93
+ await t('THE BUG: bare rank:metic seats metics ONLY (still true, deliberately)', async () => {
94
+ assert.deepEqual(await logins('rank:metic'), ['m']);
95
+ });
96
+
97
+ await t('THE FIX: rank:metic+ seats metic AND archon', async () => {
98
+ assert.deepEqual(await logins('rank:metic+'), ['m', 'a']);
99
+ });
100
+
101
+ await t('rank:xenos+ is how you say everyone', async () => {
102
+ assert.deepEqual(await logins('rank:xenos+'), ['x', 't', 'm', 'a']);
103
+ });
104
+
105
+ await t('bare rank:xenos really does seat NEWCOMERS ONLY — the library\'s lie', async () => {
106
+ assert.deepEqual(await logins('rank:xenos'), ['x']);
107
+ });
108
+
109
+ await t('a comma union works, and composes with the ladder', async () => {
110
+ assert.deepEqual(await logins('rank:metic,archon'), ['m', 'a']);
111
+ assert.deepEqual(await logins('rank:metic+,gbmdb-ops-rank'), ['m', 'a', 'ops']);
112
+ });
113
+
114
+ await t('the deactivated and the machine are STILL excluded under the widened form', async () => {
115
+ // The two structural filters must survive the ANY() rewrite — a widened
116
+ // franchise that also admits a bot or a departed builder is a worse bug.
117
+ const l = await logins('rank:xenos+');
118
+ assert.ok(!l.includes('gone'), 'a deactivated builder never governs');
119
+ assert.ok(!l.includes('bot'), 'a machine principal never governs');
120
+ });
121
+
122
+ await t('no duplicate rows when a builder matches through two keys', async () => {
123
+ await pool.query('INSERT INTO government_builder_ranks (builder_id, rank_key) VALUES ($1, $2)', [ID.archon, 'gbmdb-ops-rank']);
124
+ try {
125
+ const l = await logins('rank:archon,gbmdb-ops-rank');
126
+ assert.deepEqual(l.filter((x) => x === 'a').length, 1,
127
+ 'DISTINCT holds — a double-counted member silently raises the consent bar');
128
+ } finally {
129
+ await pool.query("DELETE FROM government_builder_ranks WHERE builder_id=$1 AND rank_key='gbmdb-ops-rank'", [ID.archon]);
130
+ }
131
+ });
132
+
133
+ await t('isBoardMember agrees with resolveBoardMembers on every form', async () => {
134
+ assert.equal(await isBoardMember(ID.archon, board('rank:metic+')), true, 'THE REGRESSION: the Archon keeps their seat');
135
+ assert.equal(await isBoardMember(ID.archon, board('rank:metic')), false, 'and the bare form still excludes them');
136
+ assert.equal(await isBoardMember(ID.metic, board('rank:metic+')), true);
137
+ assert.equal(await isBoardMember(ID.xenos, board('rank:xenos+')), true);
138
+ assert.equal(await isBoardMember(ID.gone, board('rank:xenos+')), false, 'deactivated');
139
+ assert.equal(await isBoardMember(ID.bot, board('rank:xenos+')), false, 'machine');
140
+ });
141
+
142
+ await t('an unparseable predicate still seats nobody against a real db', async () => {
143
+ assert.deepEqual(await logins('rank:ops+'), [], '+ on a non-ladder rank fails closed');
144
+ assert.deepEqual(await logins('nonsense'), []);
145
+ });
146
+
147
+ await cleanup();
148
+ console.log(`\n${passed} passed, ${failed} failed`);
149
+ await pool.end();
150
+ process.exit(failed ? 1 : 0);
@@ -84,8 +84,10 @@ test('the happy path: a member yes is recorded fresh, with its +1 karma', async
84
84
  let insertArgs = null;
85
85
  await withDb({
86
86
  getBoardItemForVote: async () => openItem(),
87
- builderHoldsRankActive: async (builderId, rankKey) =>
88
- builderId === '42' && rankKey === 'archon', // the REAL membership parse chose 'archon' from the snapshot
87
+ builderHoldsRankActive: async (builderId, rankKeys) =>
88
+ // The db read takes a SET since task 1003094; the REAL membership parse
89
+ // resolved 'rank:archon' from the snapshot to the one-element set ['archon'].
90
+ builderId === '42' && rankKeys.includes('archon'),
89
91
  getBoardItemAuthor: async () => '7', // someone else authored the idea
90
92
  insertBoardVote: async (args) => { insertArgs = args; return { id: '1', ...args }; },
91
93
  insertVoteKarma: async (args) => { karma.push(args); },
@@ -105,7 +107,7 @@ test('membership is the SNAPSHOT\'s rule: a rank the snapshot does not name is r
105
107
  // they hold A rank — just not the snapshotted one.
106
108
  await withDb({
107
109
  getBoardItemForVote: async () => openItem(),
108
- builderHoldsRankActive: async (builderId, rankKey) => rankKey === 'metic', // holds metic, not archon
110
+ builderHoldsRankActive: async (builderId, rankKeys) => rankKeys.includes('metic'), // holds metic, not archon
109
111
  getBoardItemAuthor: async () => { throw new Error('must not be reached'); },
110
112
  insertBoardVote: async () => { throw new Error('must not be reached'); },
111
113
  }, async () => {
@@ -86,3 +86,56 @@ test('the read rides the vote atom, and the client renders the SERVER value (sou
86
86
  assert.ok(!/'first_ratifier'[^]*hardcod/i.test(client), 'sanity');
87
87
  assert.match(client, /loadConstitution\(\)/, 'the panel loads for everyone the page admits');
88
88
  });
89
+
90
+ // ── the hall's membership prose mirrors the server parser (task 1003094) ─────
91
+ //
92
+ // Browser code cannot import the server module (the established pattern here —
93
+ // the five Full Idea section labels are mirrored the same way), so the hall
94
+ // re-implements the predicate grammar to render it in plain language. Two copies
95
+ // of a grammar is exactly the drift that produced the bug this task fixes, so
96
+ // they are held equal here over every form.
97
+ //
98
+ // This matters more than a normal mirror: the Constitution panel is where an
99
+ // owner READS who governs before deciding whether to change it. A panel that
100
+ // misdescribes the franchise is the same failure as a resolver that mis-seats
101
+ // it, one layer up.
102
+ test('the hall renders every predicate form the server actually resolves', () => {
103
+ const src = fs.readFileSync(path.join(ROOT, 'modules/hall-ui/public/government.js'), 'utf8');
104
+
105
+ // Lift the browser mirror out of the IIFE and run it.
106
+ const ladder = /const STANDARD_LADDER = (\[[^\]]*\]);/.exec(src);
107
+ assert.ok(ladder, 'the hall must declare the standard ladder it expands `+` against');
108
+ const fn = /function membershipRankKeys\(predicate\) \{[\s\S]*?\n \}/.exec(src);
109
+ assert.ok(fn, 'membershipRankKeys must be findable — this test lifts it');
110
+ // eslint-disable-next-line no-new-func
111
+ const browserKeys = new Function(`const STANDARD_LADDER = ${ladder[1]}; ${fn[0]}; return membershipRankKeys;`)();
112
+
113
+ const { parseMembershipPredicate } = require(path.join(ROOT, 'modules/government/membership-predicate.js'));
114
+
115
+ const forms = [
116
+ 'rank:archon', 'rank:metic', 'rank:xenos', 'rank:custom-rank',
117
+ 'rank:metic+', 'rank:xenos+', 'rank:archon+',
118
+ 'rank:metic,archon', 'rank:metic+,ops', 'rank:archon,archon',
119
+ 'rank:custom+', 'rank:', 'rank:metic++', 'rank:+metic', 'nonsense', '',
120
+ 'rank:archon,', 'rank:,archon', 'rank:archon,,metic',
121
+ ];
122
+ for (const f of forms) {
123
+ const server = parseMembershipPredicate(f);
124
+ const browser = browserKeys(f);
125
+ assert.deepEqual(
126
+ browser === null ? null : browser,
127
+ server === null ? null : [...server.rankKeys],
128
+ `the hall and the server disagree about ${JSON.stringify(f)}`,
129
+ );
130
+ }
131
+ });
132
+
133
+ test('the ladder the hall expands against IS the government ladder', () => {
134
+ const src = fs.readFileSync(path.join(ROOT, 'modules/hall-ui/public/government.js'), 'utf8');
135
+ const { RANK_ORDER } = require(path.join(ROOT, 'modules/government/catalog.js'));
136
+ const m = /const STANDARD_LADDER = \[([^\]]*)\]/.exec(src);
137
+ assert.ok(m);
138
+ const hall = m[1].split(',').map((x) => x.trim().replace(/['"]/g, '')).filter(Boolean);
139
+ assert.deepEqual(hall, [...RANK_ORDER],
140
+ 'a hall ladder that drifts from catalog.RANK_ORDER renders a franchise nobody has');
141
+ });
@@ -107,6 +107,15 @@ const STATIC_LADDER_FILES = [
107
107
  'modules/hall-ui/public/ranks.js',
108
108
  'modules/hall-ui/public/profile.js',
109
109
  'modules/hall-ui/public/watch.js',
110
+ // task 1003094: the Constitution panel expands the `rank:<key>+` membership
111
+ // form ("that rank and above") to render who governs in plain language, so it
112
+ // needs the ladder to expand against. Browser code cannot require the server
113
+ // module — the established mirror pattern here — and listing it in this array
114
+ // is what makes the copy provable rather than merely allowed. A second,
115
+ // government-specific pin lives in tests/government_constitution_view.mjs,
116
+ // which holds it against catalog.RANK_ORDER and holds the whole browser parser
117
+ // equal to the server one.
118
+ 'modules/hall-ui/public/government.js',
110
119
  ];
111
120
  test('static ladder copies carry the exact canonical ladder', () => {
112
121
  for (const rel of STATIC_LADDER_FILES) {