@@ -13,16 +13,21 @@ import { cloudActorHeaderSourceFromHeaders, resolveCloudTenantActor } from "./au
1313// the user's own membership rows (rows configurable via membershipQueryRows,
1414// where-conditions captured in selectWheres). The chain is awaitable so
1515// directly-awaited statements resolve.
16- function createFakeDb ( options : {
17- membershipRow ?: { companyId : string ; membershipRole : string ; status : string } ;
16+ type SeededMembership = { companyId : string ; membershipRole : string ; status : string } ;
17+
18+ function createFakeDb ( options ?: {
19+ membershipRow ?: SeededMembership ;
1820 membershipQueryRows ?: Array < { companyId : string ; membershipRole : string | null ; status : string } > ;
21+ seededMemberships ?: SeededMembership [ ] ;
22+ /** Rows returned by the SELECT over `companies` — [] means the stack company does not exist yet. */
23+ companyRows ?: Array < { id : string } > ;
1924 settingsRow ?: Record < string , unknown > | null ;
2025 selectThrows ?: boolean ;
21- } = { } ) {
22- const membershipRow =
23- options . membershipRow ?? { companyId : "company-x" , membershipRole : "owner" , status : "active" } ;
26+ } ) {
27+ const membershipRow : SeededMembership =
28+ options ? .membershipRow ?? { companyId : "company-x" , membershipRole : "owner" , status : "active" } ;
2429 const settingsRow =
25- options . settingsRow === undefined
30+ options ? .settingsRow === undefined
2631 ? {
2732 id : "00000000-0000-0000-0000-000000000001" ,
2833 singletonKey : "default" ,
@@ -32,12 +37,20 @@ function createFakeDb(options: {
3237 createdAt : new Date ( ) ,
3338 updatedAt : new Date ( ) ,
3439 }
35- : options . settingsRow ;
40+ : options ? .settingsRow ;
3641 const insertedTables : unknown [ ] = [ ] ;
3742 const deletedTables : unknown [ ] = [ ] ;
43+ const selectedTables : unknown [ ] = [ ] ;
3844 const selectWheres : Array < { table : unknown ; condition : unknown } > = [ ] ;
45+ const insertedValues = new Map < unknown , Record < string , unknown > > ( ) ;
46+ let currentTable : unknown = null ;
47+ const memberships = options ?. seededMemberships ?? [ membershipRow ] ;
48+ const companyRows = options ?. companyRows ?? [ ] ;
3949 const chain : Record < string , unknown > = { } ;
40- chain . values = ( ) => chain ;
50+ chain . values = ( values : Record < string , unknown > ) => {
51+ if ( currentTable !== null ) insertedValues . set ( currentTable , values ) ;
52+ return chain ;
53+ } ;
4154 chain . onConflictDoUpdate = ( ) => chain ;
4255 chain . onConflictDoNothing = ( ) => chain ;
4356 chain . where = ( ) => chain ;
@@ -46,33 +59,40 @@ function createFakeDb(options: {
4659 const db = {
4760 insert : ( table : unknown ) => {
4861 insertedTables . push ( table ) ;
62+ currentTable = table ;
4963 return chain ;
5064 } ,
5165 delete : ( table : unknown ) => {
5266 deletedTables . push ( table ) ;
53- return chain ;
67+ return { where : async ( ) => undefined } ;
5468 } ,
5569 select : ( ) => {
56- if ( options . selectThrows ) throw new Error ( "select unavailable" ) ;
70+ if ( options ? .selectThrows ) throw new Error ( "select unavailable" ) ;
5771 return {
58- from : ( table : unknown ) => ( {
59- where : ( condition : unknown ) => {
60- selectWheres . push ( { table, condition } ) ;
61- const rows =
62- table === instanceSettings && settingsRow
63- ? [ settingsRow ]
64- : table === companyMemberships
65- ? ( options . membershipQueryRows ?? [ ] )
66- : [ ] ;
67- return {
68- then : ( resolve : ( v : unknown ) => unknown ) => Promise . resolve ( rows ) . then ( resolve ) ,
69- } ;
70- } ,
71- } ) ,
72+ from : ( table : unknown ) => {
73+ selectedTables . push ( table ) ;
74+ return {
75+ where : ( condition ?: unknown ) => {
76+ if ( condition !== undefined ) selectWheres . push ( { table, condition } ) ;
77+ return {
78+ then : ( resolve : ( v : unknown ) => unknown ) => {
79+ const result = table === companies
80+ ? companyRows
81+ : table === instanceSettings && settingsRow
82+ ? [ settingsRow ]
83+ : table === companyMemberships
84+ ? ( options ?. membershipQueryRows ?? memberships )
85+ : [ ] ;
86+ return Promise . resolve ( result ) . then ( resolve ) ;
87+ } ,
88+ } ;
89+ } ,
90+ } ;
91+ } ,
7292 } ;
7393 } ,
7494 } as unknown as Db ;
75- return { db, insertedTables, deletedTables, selectWheres } ;
95+ return { db, insertedTables, deletedTables, selectedTables , selectWheres, insertedValues } ;
7696}
7797
7898function settingsRowWith ( experimental : Record < string , unknown > ) {
0 commit comments