| 123456789101112131415161718 |
- import{test}from'node:test';import assert from'node:assert/strict';import{PGlite}from'@electric-sql/pglite';import{retireImportedAccounts}from'../lib/retire-imported-accounts.mjs';
- test('按对象身份原子注销,释放阅读伪编号,保留课时和业务记录;重复执行无副作用',async()=>{
- const db=new PGlite();const q=async(s,p)=>(await db.query(s,p)).rows;
- const a={any:q,oneOrNone:async(s,p)=>(await q(s,p))[0],none:async(s,p)=>{await q(s,p);}};a.tx=async f=>{await db.exec('BEGIN');try{const r=await f(a);await db.exec('COMMIT');return r;}catch(e){await db.exec('ROLLBACK');throw e;}};
- try{
- await db.exec('CREATE TABLE "_User" ("objectId" text,company text,"isAdmin" boolean,roles text[],"adminRoleKey" text,"legacyUserId" numeric,"sourceKey" text,"updatedAt" timestamptz,"isDeleted" boolean,"isDisabled" boolean,"legacyUserData" jsonb);CREATE TABLE "_Session" ("objectId" text,"user" text);CREATE TABLE "LegacyDirectRun" ("runId" text,company text,status text,"leaseUntil" timestamptz);CREATE TABLE "SysLog" ("objectId" text,company text,"sourceKey" text,type1 text,type2 text,type3 text,detail text,"createdAt" timestamptz,"updatedAt" timestamptz);CREATE TABLE lessons (uid int,amount int);INSERT INTO lessons VALUES(1855,100)');
- await q('INSERT INTO "_User" ("objectId",company,"legacyUserId","sourceKey","updatedAt","legacyUserData") VALUES ($1,$2,$3,$4,$5,$6::jsonb)',['reading','co',1855,'reading-unified:user:reading','2026-10-13T00:00:00Z','{"UserID":1855,"Purse":88}']);
- await q('INSERT INTO "_Session" VALUES ($1,$2)',['session','_User$reading']);
- const target={objectId:'reading',legacyUserId:1855,sourceKey:'reading-unified:user:reading',updatedAt:'2026-10-13T00:00:00Z'};
- await assert.rejects(retireImportedAccounts(a,'other',[target],'operator','r'),/wrong_company/);
- await assert.rejects(retireImportedAccounts(a,'co',[{...target,legacyUserId:1}],'operator','r'),/target_changed/);
- assert.equal((await q('SELECT * FROM "_Session"')).length,1);
- const r=await retireImportedAccounts(a,'co',[target],'operator','r');assert.equal(r.retired,1);assert.equal(r.detachedSyntheticIds,1);assert.equal(r.revokedSessions,1);
- const row=(await q('SELECT * FROM "_User"'))[0];assert.equal(row.isDeleted,true);assert.equal(row.isDisabled,true);assert.equal(row.legacyUserId,null);assert.equal(row.legacyUserData.UserID,undefined);assert.equal(row.legacyUserData.Purse,88);assert.equal(row.legacyUserData.SyncExcluded,true);
- assert.equal((await q('SELECT * FROM lessons'))[0].amount,100);
- assert.equal((await retireImportedAccounts(a,'co',[target],'operator','r')).alreadyRetired,1);assert.equal((await q('SELECT * FROM "SysLog"')).length,1);
- }finally{await db.close();}
- });
|