import {test} from 'node:test';import assert from 'node:assert/strict';import vm from 'node:vm';import {readFileSync} from 'node:fs';import {PGlite} from '@electric-sql/pglite'; const source=readFileSync(new URL('../deploy-admin-functions.mjs',import.meta.url),'utf8').match(/const operationsGatewayCode = String.raw`([\s\S]*?)\n`;/)[1]; test('完整来源超过5000条仍可分页、搜索较早记录,空末页保留真实总数且租户隔离',async()=>{ const db=new PGlite();try{ await db.exec(`CREATE TABLE records(company text,"objectId" text,"targetId" text,"generalId" integer,"commonUpdatedAt" timestamptz,"studentObjectId" text,"studentRealName" text,"studentNickname" text,"studentUsername" text,"studentLegacy" jsonb,"courseTitle" text,"userId" text,dqrq text,learned int,ygg int,djq int); INSERT INTO records SELECT 'co','r'||g,'r'||g,g,'2026-10-12'::timestamptz-g*interval '1 second','s','学员'||g,'','','{}','单词','20','20261012',20,15,5 FROM generate_series(1,6005) g; INSERT INTO records SELECT 'other','private','private',99999,now(),'x','其他帐套','','','{}','单词','99','20261012',20,20,0;`); const ctx=vm.createContext({Date,Psql:{query:async(sql,args)=>(await db.query(sql,args)).rows}});vm.runInContext(source,ctx); const sql='SELECT * FROM records WHERE company=$1 AND ($2=\'\' OR "userId"=$2) ORDER BY "commonUpdatedAt" DESC'; let page=await ctx.businessRecordSqlPage('learning-records',sql,['co',''],{page:251,pageSize:20});assert.equal(page.total,6005);assert.equal(page.items.length,20);assert.equal(page.items[0].generalId,5001); page=await ctx.businessRecordSqlPage('learning-records',sql,['co',''],{search:'学员6005'});assert.equal(page.total,1);assert.equal(page.items[0].generalId,6005); page=await ctx.businessRecordSqlPage('learning-records',sql,['co',''],{page:999,pageSize:20});assert.equal(page.total,6005);assert.equal(page.items.length,0); for(const search of ['其他帐套','%','_'])assert.equal((await ctx.businessRecordSqlPage('learning-records',sql,['co',''],{search})).total,0); }finally{await db.close();} });