@@ -5,11 +5,12 @@ import messages from '../../src/locales/account-ui.js'
55import { forthrightStockOrderIssue , forthrightBuyingPower } from '../../src/utils/forthright.js'
66import { buildStockOrder , validateStockOrder , stockQuantityPrecision , STOCK_OPEN_STATUSES } from '../../src/utils/stockOrder.js'
77import { US_STOCK_BROKER_IDS , HK_STOCK_BROKER_IDS , isDirectOpenApiBroker , brokerCurrency } from '../../src/constants/exchanges.js'
8+ import { stockSymbolsMatch , stockFillRows } from '../../src/utils/stockRecords.js'
89
910const script = readFileSync ( new URL ( '../../src/components/StockTradePanel.vue' , import . meta. url ) , 'utf8' ) . split ( '<script>' ) [ 1 ] . split ( '</script>' ) [ 0 ] . replace ( / ^ i m p o r t .* $ / gm, '' ) . replace ( 'export default' , 'return' )
1011const brokerReads = [ ]
11- const brokerApi = Object . fromEntries ( [ 'account' , 'positions' , 'orders' ] . map ( kind => [ kind , async ( ...args ) => { brokerReads . push ( { kind, args} ) ; return { data :{ } } } ] ) )
12- const options = new Function ( 'showToast' , 'forthrightErrorMessage' , 'buildStockOrder' , 'validateStockOrder' , 'stockQuantityPrecision' , 'STOCK_OPEN_STATUSES' , 'ExchangeLogo' , 'AlpacaTradePanel' , 'forthrightStockOrderIssue' , 'US_STOCK_BROKER_IDS' , 'HK_STOCK_BROKER_IDS' , 'brokerApi' , 'isDirectOpenApiBroker' , 'brokerCurrency' , script ) ( ( ) => { } , error => error . message , buildStockOrder , validateStockOrder , stockQuantityPrecision , STOCK_OPEN_STATUSES , null , null , forthrightStockOrderIssue , US_STOCK_BROKER_IDS , HK_STOCK_BROKER_IDS , brokerApi , isDirectOpenApiBroker , brokerCurrency )
12+ const brokerApi = Object . fromEntries ( [ 'account' , 'positions' , 'orders' , 'fills' ] . map ( kind => [ kind , async ( ...args ) => { brokerReads . push ( { kind, args} ) ; return { data :{ } } } ] ) )
13+ const options = new Function ( 'showToast' , 'forthrightErrorMessage' , 'buildStockOrder' , 'validateStockOrder' , 'stockQuantityPrecision' , 'STOCK_OPEN_STATUSES' , 'ExchangeLogo' , 'AlpacaTradePanel' , 'forthrightStockOrderIssue' , 'US_STOCK_BROKER_IDS' , 'HK_STOCK_BROKER_IDS' , 'brokerApi' , 'isDirectOpenApiBroker' , 'brokerCurrency' , 'stockSymbolsMatch' , 'stockFillRows' , script ) ( ( ) => { } , error => error . message , buildStockOrder , validateStockOrder , stockQuantityPrecision , STOCK_OPEN_STATUSES , null , null , forthrightStockOrderIssue , US_STOCK_BROKER_IDS , HK_STOCK_BROKER_IDS , brokerApi , isDirectOpenApiBroker , brokerCurrency , stockSymbolsMatch , stockFillRows )
1314function panel ( ) {
1415 const vm = { ...options . data ( ) , market : 'HKStock' , symbol : '03690' , $route : { query : { credential_id : '18' , exchange_id : 'forthright' } } , $t : key => key }
1516 for ( const [ name , method ] of Object . entries ( options . methods ) ) vm [ name ] = method . bind ( vm )
@@ -146,6 +147,47 @@ test('missing financial values remain unavailable while zero is valid', () => {
146147 assert . equal ( vm . quantity ( null ) , '—' )
147148} )
148149
150+ test ( 'HK aliases match without crossing markets or merging US share classes' , ( ) => {
151+ const vm = panel ( ) ; vm . symbol = '00700'
152+ for ( const symbol of [ '700' , '00700.HK' , 'HK.00700' , 'HKStock:00700' ] ) assert . equal ( vm . matchesSymbol ( { symbol} ) , true )
153+ assert . equal ( vm . matchesSymbol ( { symbol :'USStock:00700' } ) , false )
154+ vm . market = 'USStock' ; vm . symbol = 'BRK.B' ; vm . selectedCredential = { exchange_id :'alpaca' }
155+ assert . equal ( vm . matchesSymbol ( { symbol :'BRK/B' } ) , true )
156+ for ( const symbol of [ 'BRKB' , 'BRK-B' ] ) assert . equal ( vm . matchesSymbol ( { symbol} ) , false )
157+ } )
158+
159+ test ( 'each direct broker reads account-scoped fills for trade history' , async ( ) => {
160+ for ( const exchange_id of [ 'tiger' , 'webull' , 'forthright' ] ) {
161+ const vm = panel ( ) ; vm . selectedCredential = { id :18 , exchange_id} ; vm . displaySymbol = '03690' ; vm . tradeContext = '18:HKStock:03690'
162+ await vm . loadHistory ( )
163+ assert . deepEqual ( brokerReads . at ( - 1 ) , { kind :'fills' , args :[ exchange_id , 18 , { symbol :'03690' , limit :100 } ] } )
164+ }
165+ } )
166+
167+ test ( 'history shows filled shares and retains partial cancelled fills' , async ( ) => {
168+ const vm = panel ( ) ; vm . tradeContext = '18:HKStock:03690' ; vm . displaySymbol = '03690'
169+ vm . request = async ( ) => ( { data :[
170+ { id :'partial' , symbol :'03690.HK' , quantity :100 , filled_qty :20 , filled_avg_price :24 , status :'cancelled' , commission :1 , commission_ccy :'HKD' } ,
171+ { id :'unfilled' , symbol :'03690.HK' , quantity :100 , filled_qty :0 }
172+ ] } )
173+ await vm . loadHistory ( )
174+ assert . equal ( vm . history . length , 1 )
175+ assert . equal ( vm . history [ 0 ] . quantity , 20 )
176+ assert . equal ( vm . history [ 0 ] . filled_avg_price , 24 )
177+ assert . equal ( vm . history [ 0 ] . fee_amount , 1 )
178+ } )
179+
180+ test ( 'late history from another account cannot replace current records' , async ( ) => {
181+ const vm = panel ( ) ; vm . tradeContext = '18:HKStock:03690' ; vm . refresh = async ( ) => { }
182+ let release
183+ vm . request = ( ) => new Promise ( resolve => { release = resolve } )
184+ const pending = vm . loadHistory ( )
185+ vm . tradeContext = '19:HKStock:03690' ; vm . history = [ { id :'current' } ]
186+ release ( { data :[ { id :'old' , filled_qty :100 } ] } )
187+ await pending
188+ assert . deepEqual ( vm . history , [ { id :'current' } ] )
189+ } )
190+
149191test ( 'slow responses from the previous account cannot overwrite the selected account' , async ( ) => {
150192 const vm = panel ( )
151193 vm . selectedCredential = { id : 18 , exchange_id : 'forthright' }
0 commit comments