@@ -5,6 +5,7 @@ import { readFileSync } from 'node:fs';
55import { dirname , resolve } from 'node:path' ;
66import { fileURLToPath } from 'node:url' ;
77import { assertEngineUpdateDispatch } from '@objectstack/objectql' ;
8+ import { RestServer } from '@objectstack/rest' ;
89import { runAdminImportUsers , IMPORT_USERS_MAX_ROWS , type IdentityImportDeps } from './admin-import-users.js' ;
910import { settleImportResetDelivery } from './account-invitation-marker.js' ;
1011import type { AdminActor } from './admin-user-endpoints.js' ;
@@ -719,6 +720,75 @@ describe('runAdminImportUsers — CSV payloads', () => {
719720 } ) ;
720721} ) ;
721722
723+ /**
724+ * [#25078] Export → admin user import round trip, through the CSV text-cell rule.
725+ *
726+ * `@objectstack/rest`'s CSV export writes a text cell its consumer would
727+ * interpret in a form that consumer reads as text, and its CSV import parse
728+ * (`parseCsvToRows`, reached through `prepareImportRequest`) reverses exactly
729+ * that. This door parses a `csv` body through that same `prepareImportRequest`,
730+ * so a user list exported as CSV and imported back here must hand better-auth's
731+ * `createUser` every stored name exactly.
732+ *
733+ * The file comes from the REAL export route of `@objectstack/rest`'s
734+ * `RestServer`, over a protocol double serving a two-field `sys_user` schema and
735+ * the rows below. The rule is pinned at its readers in `@objectstack/rest`
736+ * (`csv-text-cell.test.ts`), and its other CSV doors in that package's
737+ * `csv-text-cell-round-trip.test.ts`.
738+ */
739+ describe ( 'runAdminImportUsers — CSV export → import round trip (#25078)' , ( ) => {
740+ /** Plain words after a trigger character: marked by the export. */
741+ const MARKED = [ '=Total' , '+Name' , '-Note' , '@acme' ] ;
742+ /** Ordinary business values, and a user-authored name that begins with the mark itself. */
743+ const UNMARKED = [ '+86 138 0013 8000' , '+1 555 010 0000' , '-120.50' , 'Acme Ltd' , '120.50' , "'Quoted" ] ;
744+ const NAMES = [ ...MARKED , ...UNMARKED ] ;
745+
746+ async function exportUsersCsv ( rows : Array < Record < string , string > > ) : Promise < string > {
747+ const noop = ( ) => { } ;
748+ const server = { get : noop , post : noop , put : noop , delete : noop , patch : noop , use : noop , listen : async ( ) => { } , close : async ( ) => { } } ;
749+ const schema = { name : 'sys_user' , fields : { email : { name : 'email' , type : 'email' } , name : { name : 'name' , type : 'text' } } } ;
750+ const protocol = {
751+ getMetaItem : vi . fn ( async ( ) => ( { type : 'object' , name : 'sys_user' , item : schema } ) ) ,
752+ findData : vi . fn ( async ( ) => ( { object : 'sys_user' , records : rows , total : rows . length } ) ) ,
753+ } ;
754+ const rest = new RestServer ( server as any , protocol as any , { api : { requireAuth : false } } as any ) ;
755+ ( rest as any ) . resolveExecCtx = async ( ) => ( { userId : ACTOR . id } ) ;
756+ rest . registerRoutes ( ) ;
757+ const route : any = rest . getRoutes ( ) . find (
758+ ( r : any ) => r . method === 'GET' && r . path === '/api/v1/data/:object/export' ,
759+ ) ;
760+ expect ( route ) . toBeDefined ( ) ;
761+ const chunks : string [ ] = [ ] ;
762+ const res : any = {
763+ write : ( s : unknown ) => { chunks . push ( String ( s ) ) ; return true ; } ,
764+ end : ( ) => { } ,
765+ header : ( ) => res ,
766+ status : ( ) => res ,
767+ json : ( ) => res ,
768+ } ;
769+ await route . handler ( { params : { object : 'sys_user' } , query : { format : 'csv' } } , res ) ;
770+ return chunks . join ( '' ) ;
771+ }
772+
773+ it ( 'hands createUser every exported name exactly' , async ( ) => {
774+ const rows = NAMES . map ( ( name , i ) => ( { email : `user${ i + 1 } @example.com` , name } ) ) ;
775+ const csv = await exportUsersCsv ( rows ) ;
776+
777+ // The file carried the marks, so the round trip below is not a pass-through.
778+ expect ( csv . split ( '\r\n' ) [ 0 ] ) . toBe ( 'email,name' ) ;
779+ for ( const [ i , v ] of MARKED . entries ( ) ) expect ( csv ) . toContain ( `user${ i + 1 } @example.com,'${ v } \r\n` ) ;
780+ expect ( csv ) . toContain ( ',+86 138 0013 8000\r\n' ) ;
781+ expect ( csv ) . toContain ( ",'Quoted\r\n" ) ;
782+
783+ const m = makeDeps ( ) ;
784+ const res = await runAdminImportUsers ( m . deps , makeRequest ( { passwordPolicy : 'temporary' , csv } ) , ACTOR ) ;
785+ expect ( res . status ) . toBe ( 200 ) ;
786+ expect ( ( res . body . data as any ) . summary . created ) . toBe ( NAMES . length ) ;
787+ const byEmail = new Map ( m . createUser . mock . calls . map ( ( c : any [ ] ) => [ c [ 0 ] . body . email , c [ 0 ] . body . name ] ) ) ;
788+ expect ( rows . map ( ( r ) => byEmail . get ( r . email ) ) ) . toEqual ( NAMES ) ;
789+ } ) ;
790+ } ) ;
791+
722792/**
723793 * [#17422] The IMPLEMENTOR half of #16952's contract.
724794 *
0 commit comments