1+ 'use strict'
2+ var helper = require ( './test-helper' )
3+ var assert = require ( 'assert' )
4+ const { Client } = helper . pg
5+ const suite = new helper . Suite ( )
6+
7+ // Just verify the test infrastructure works
8+ suite . test ( 'sanity check' , function ( done ) {
9+ var client = new Client ( helper . args )
10+ client . connect ( assert . success ( function ( ) {
11+ client . query ( 'SELECT 1 as num' , assert . success ( function ( result ) {
12+ assert . equal ( result . rows . length , 1 )
13+ client . end ( done )
14+ } ) )
15+ } ) )
16+ } )
17+
18+ // Basic test to check if the _maxResultSize property is passed to Connection
19+ suite . test ( 'client passes maxResultSize to connection' , function ( done ) {
20+ var client = new Client ( {
21+ ...helper . args ,
22+ maxResultSize : 1024 * 1024 // 1MB limit
23+ } )
24+
25+ client . connect ( assert . success ( function ( ) {
26+ assert . equal ( client . connection . _maxResultSize , 1024 * 1024 ,
27+ 'maxResultSize should be passed to the connection' )
28+ client . end ( done )
29+ } ) )
30+ } )
31+
32+ // Check if the correct attachListeners method is being used based on maxResultSize
33+ suite . test ( 'connection uses correct listener implementation' , function ( done ) {
34+ var client = new Client ( {
35+ ...helper . args ,
36+ maxResultSize : 1024 * 1024 // 1MB limit
37+ } )
38+
39+ client . connect ( assert . success ( function ( ) {
40+ // Just a simple check to see if our methods exist on the connection object
41+ assert ( typeof client . connection . _attachListenersStandard === 'function' ,
42+ 'Standard listener method should exist' )
43+ assert ( typeof client . connection . _attachListenersWithSizeLimit === 'function' ,
44+ 'Size-limiting listener method should exist' )
45+ client . end ( done )
46+ } ) )
47+ } )
48+
49+ // Test that small result sets complete successfully with maxResultSize set
50+ suite . test ( 'small result with maxResultSize' , function ( done ) {
51+ var client = new Client ( {
52+ ...helper . args ,
53+ maxResultSize : 1024 * 1024 // 1MB limit
54+ } )
55+
56+ client . connect ( assert . success ( function ( ) {
57+ client . query ( 'SELECT generate_series(1, 10) as num' , assert . success ( function ( result ) {
58+ assert . equal ( result . rows . length , 10 )
59+ client . end ( done )
60+ } ) )
61+ } ) )
62+ } )
63+
64+ // Test for large result size
65+ // Using a separate test to avoid issue with callback being called twice
66+ suite . testAsync ( 'large result triggers error' , async ( ) => {
67+ const client = new Client ( {
68+ ...helper . args ,
69+ maxResultSize : 500 // Very small limit
70+ } )
71+
72+ // Setup error handler
73+ const errorPromise = new Promise ( resolve => {
74+ client . on ( 'error' , err => {
75+ assert . equal ( err . message , 'Query result size exceeded the configured limit' )
76+ assert . equal ( err . code , 'RESULT_SIZE_EXCEEDED' )
77+ resolve ( )
78+ } )
79+ } )
80+
81+ await client . connect ( )
82+
83+ // Start the query but don't await it (it will error)
84+ const queryPromise = client . query ( 'SELECT repeat(\'x\', 1000) as data FROM generate_series(1, 100)' )
85+ . catch ( err => {
86+ // We expect this to error out, silence the rejection
87+ return null
88+ } )
89+
90+ // Wait for error event
91+ await errorPromise
92+
93+ // Make sure the query is done before we end
94+ await queryPromise
95+
96+ // Clean up
97+ await client . end ( ) . catch ( ( ) => { } ) // Ignore errors during cleanup
98+ } )
0 commit comments