|
|
@@ -0,0 +1,119 @@
|
|
|
+// Execute the actual migration SQL in an in-memory PostgreSQL-compatible engine.
|
|
|
+// PGLITE_MODULE points to an external, explicitly provisioned test dependency.
|
|
|
+// This harness never installs packages or connects to a real DB.
|
|
|
+// PGLITE_MODULE=/absolute/path/to/pglite/dist/index.js node --test alarm_common_routes.test.mjs
|
|
|
+import { readFile } from 'node:fs/promises'
|
|
|
+import { pathToFileURL } from 'node:url'
|
|
|
+import assert from 'node:assert/strict'
|
|
|
+import { test } from 'node:test'
|
|
|
+
|
|
|
+const { PGlite } = await import(process.env.PGLITE_MODULE
|
|
|
+ ? pathToFileURL(process.env.PGLITE_MODULE).href : '@electric-sql/pglite')
|
|
|
+// The optional baseline path exercises historical SQL without rewriting current implementation.
|
|
|
+const operationsSql = await readFile(process.env.ALARM_ROUTES_OPERATIONS_SQL
|
|
|
+ ? pathToFileURL(process.env.ALARM_ROUTES_OPERATIONS_SQL) : new URL('../migration_alarm_operations.sql', import.meta.url), 'utf8')
|
|
|
+const patchSql = () => readFile(new URL('../migration_alarm_common_routes_20261111.sql', import.meta.url), 'utf8')
|
|
|
+
|
|
|
+const schema = `
|
|
|
+ CREATE SCHEMA app_user;
|
|
|
+ CREATE TABLE app_user.sys_menu (
|
|
|
+ menu_id int8 PRIMARY KEY, menu_name text, parent_id int8, order_num integer,
|
|
|
+ path text, component text, query text, perms text, status text, visible text,
|
|
|
+ update_time timestamp
|
|
|
+ );
|
|
|
+ CREATE TABLE app_user.sys_role_menu (
|
|
|
+ role_id int8, menu_id int8, PRIMARY KEY (role_id, menu_id)
|
|
|
+ );
|
|
|
+`
|
|
|
+const rows = async (db, sql) => (await db.query(sql)).rows
|
|
|
+const menus = db => rows(db, 'SELECT * FROM app_user.sys_menu ORDER BY menu_id')
|
|
|
+const grants = db => rows(db, 'SELECT * FROM app_user.sys_role_menu ORDER BY role_id,menu_id')
|
|
|
+
|
|
|
+async function incrementalFixture() {
|
|
|
+ const db = new PGlite()
|
|
|
+ await db.exec(schema)
|
|
|
+ await db.exec(`
|
|
|
+ INSERT INTO app_user.sys_menu VALUES
|
|
|
+ (2141,'drainage analysis',2100,1,'keep-analysis','subSystem/drainage/alarm/analysis','{"rootTypeId":2,"warning_code":"WARN-ONLY"}','custom:analysis','0','0','2026-01-01'),
|
|
|
+ (2143,'drainage GIS',2100,3,'keep-gis','subSystem/drainage/alarm/alarmGIS','{"rootTypeId":2}','custom:gis','0','1','2026-01-02'),
|
|
|
+ (4301,'water GIS',2000,9,'keep-water','subSystem/waterSupply/alarm/alarmGIS','{"rootTypeId":1}','waterSupply:alarm:list','0','0','2026-01-03'),
|
|
|
+ (4302,'disabled exact legacy',999,7,'keep-disabled','subSystem/drainage/alarm/alarmGIS','{"rootTypeId":3,"warningCode":"WARN-GAS"}','custom:revoked','1','1','2026-01-04'),
|
|
|
+ (2117,'known ID but unknown component',999,4,'do-not-guess','subSystem/unknown/alarm/alarmGIS','{"rootTypeId":4}','custom:other','0','0','2026-01-05'),
|
|
|
+ (9001,'near-match suffix',999,5,'keep-suffix','subSystem/drainage/alarm/alarmGIS/extra','{}','custom:suffix','0','0','2026-01-06'),
|
|
|
+ (9002,'near-match whitespace',999,6,'keep-space',' subSystem/drainage/alarm/analysis','{}','custom:space','0','0','2026-01-07'),
|
|
|
+ (9003,'already canonical GIS',999,7,'keep-common','subSystem/alarm/common/alarmGIS','{"rootTypeId":1}','custom:common','0','0','2026-01-08'),
|
|
|
+ (9004,'already canonical analysis',999,8,'keep-common-analysis','subSystem/alarm/common/analysis','{}','custom:common-analysis','0','0','2026-01-09'),
|
|
|
+ (9005,'null component',999,9,'keep-null',NULL,'{}','custom:null','0','0','2026-01-10'),
|
|
|
+ (9006,'unsupported water analysis',999,10,'keep-water-analysis','subSystem/waterSupply/alarm/analysis','{}','custom:water-analysis','0','0','2026-01-11');
|
|
|
+ INSERT INTO app_user.sys_role_menu VALUES (9,2141),(9,2143),(10,4301),(10,9001);
|
|
|
+ `)
|
|
|
+ return db
|
|
|
+}
|
|
|
+
|
|
|
+test('fresh operations install points only the intended analysis/GIS menu components at common', async () => {
|
|
|
+ const db = new PGlite()
|
|
|
+ try {
|
|
|
+ await db.exec(schema + `
|
|
|
+ CREATE TABLE app_user.alarm_data (id text PRIMARY KEY);
|
|
|
+ CREATE TABLE app_user.work_order_log (log_id int8 PRIMARY KEY);
|
|
|
+ INSERT INTO app_user.sys_menu(menu_id,parent_id,order_num,path,component,query,perms,status)
|
|
|
+ VALUES (2141,2000,3,'old-analysis','old-analysis','{}','old:analysis','0'),
|
|
|
+ (2143,2000,4,'old-gis','old-gis','{}','old:gis','1'),
|
|
|
+ (9999,3000,9,'keep','custom/unrelated','{"rootTypeId":4}','keep:permission','1');
|
|
|
+ INSERT INTO app_user.sys_role_menu VALUES (9,2141),(10,2143);
|
|
|
+ `)
|
|
|
+ const beforeGrants = await grants(db)
|
|
|
+ const [unrelated] = await rows(db, 'SELECT * FROM app_user.sys_menu WHERE menu_id=9999')
|
|
|
+ await db.exec(operationsSql)
|
|
|
+ assert.deepEqual(await rows(db, 'SELECT menu_id,component,path,query,perms,parent_id,order_num,status FROM app_user.sys_menu WHERE menu_id IN (2141,2143) ORDER BY menu_id'), [
|
|
|
+ { menu_id: 2141, component: 'subSystem/alarm/common/analysis', path: 'drainageAlarmAnalysis', query: '{"rootTypeId":2}', perms: 'alarm:operations:list', parent_id: 2000, order_num: 3, status: '0' },
|
|
|
+ { menu_id: 2143, component: 'subSystem/alarm/common/alarmGIS', path: 'drainageAlarmGIS', query: '{"rootTypeId":2}', perms: 'alarm:operations:list', parent_id: 2000, order_num: 4, status: '1' },
|
|
|
+ ])
|
|
|
+ assert.deepEqual(await grants(db), beforeGrants)
|
|
|
+ assert.deepEqual(await rows(db, 'SELECT * FROM app_user.sys_menu WHERE menu_id=9999'), [unrelated])
|
|
|
+ } finally { await db.close() }
|
|
|
+})
|
|
|
+
|
|
|
+test('incremental patch maps exact legacy components and preserves every other menu field and role grant', async () => {
|
|
|
+ const db = await incrementalFixture()
|
|
|
+ try {
|
|
|
+ const before = await menus(db)
|
|
|
+ const beforeGrants = await grants(db)
|
|
|
+ await db.exec(await patchSql())
|
|
|
+ const expectedComponents = new Map([
|
|
|
+ [2141, 'subSystem/alarm/common/analysis'],
|
|
|
+ [2143, 'subSystem/alarm/common/alarmGIS'],
|
|
|
+ [4301, 'subSystem/alarm/common/alarmGIS'],
|
|
|
+ [4302, 'subSystem/alarm/common/alarmGIS'],
|
|
|
+ ])
|
|
|
+ assert.deepEqual(await menus(db), before.map(row => expectedComponents.has(row.menu_id)
|
|
|
+ ? { ...row, component: expectedComponents.get(row.menu_id) } : row))
|
|
|
+ assert.deepEqual(await grants(db), beforeGrants)
|
|
|
+ } finally { await db.close() }
|
|
|
+})
|
|
|
+
|
|
|
+test('repeat deployment is a no-op and never restores deliberately revoked role grants', async () => {
|
|
|
+ const db = await incrementalFixture()
|
|
|
+ try {
|
|
|
+ await db.exec(await patchSql())
|
|
|
+ await db.exec('DELETE FROM app_user.sys_role_menu WHERE role_id=9 AND menu_id=2141')
|
|
|
+ const once = await menus(db)
|
|
|
+ const revokedGrants = await grants(db)
|
|
|
+ await db.exec(await patchSql())
|
|
|
+ assert.deepEqual(await menus(db), once)
|
|
|
+ assert.deepEqual(await grants(db), revokedGrants)
|
|
|
+ } finally { await db.close() }
|
|
|
+})
|
|
|
+
|
|
|
+test('a failed component update rolls back the incremental patch without partial changes', async () => {
|
|
|
+ const db = await incrementalFixture()
|
|
|
+ try {
|
|
|
+ const before = await menus(db)
|
|
|
+ const beforeGrants = await grants(db)
|
|
|
+ await db.exec("ALTER TABLE app_user.sys_menu ADD CONSTRAINT reject_water_conversion CHECK (menu_id <> 4301 OR component <> 'subSystem/alarm/common/alarmGIS')")
|
|
|
+ await assert.rejects(db.exec(await patchSql()))
|
|
|
+ await db.exec('ROLLBACK')
|
|
|
+ assert.deepEqual(await menus(db), before)
|
|
|
+ assert.deepEqual(await grants(db), beforeGrants)
|
|
|
+ } finally { await db.close() }
|
|
|
+})
|