Fix bulk code vendor pre-population for single vs multi-client contexts

- vendor-default-account now uses raw vendor default account (not client-specific override)
- Account name is clientized via d-accounts/clientize only for single-client contexts
- Added single-client-id helper that returns client ID only when user has exactly one client
- Added multi-client e2e test verifying no pre-population across multiple clients
- Updated test server to support multi-client mode switching via /test-set-client-mode
- Test server now seeds a second client for multi-client scenarios
This commit is contained in:
2026-05-23 11:21:22 -07:00
parent ba87805d4c
commit 03bfca35cb
3 changed files with 113 additions and 18 deletions

View File

@@ -8,7 +8,10 @@ async function getTestInfo(page: any) {
return testInfoCache;
}
async function navigateToTransactions(page: any) {
async function navigateToTransactions(page: any, clientMode: string = 'mine') {
await page.setExtraHTTPHeaders({
'x-clients': clientMode === 'all' ? '"all"' : '"mine"'
});
await page.goto('/transaction2');
await page.waitForSelector('table tbody tr');
}
@@ -387,6 +390,9 @@ test.describe('Bulk Code Transactions - Account Distribution', () => {
test.describe('Bulk Code Transactions - Vendor Pre-population', () => {
test('should pre-populate default account when vendor is selected', async ({ page }) => {
// Ensure single-client mode
await page.request.get('/test-set-client-mode?mode=single-client');
await navigateToTransactions(page);
await selectTransactionByIndex(page, 0);
await openBulkCodeModal(page);
@@ -440,4 +446,48 @@ test.describe('Bulk Code Transactions - Vendor Pre-population', () => {
await page.waitForSelector('table tbody tr');
});
test('should NOT pre-populate default account when user has multiple clients', async ({ page }) => {
// Switch to multi-client mode
await page.request.get('/test-set-client-mode?mode=multi-client');
// Use 'all' to see all clients in the database
await navigateToTransactions(page, 'all');
await selectTransactionByIndex(page, 0);
await openBulkCodeModal(page);
// Select vendor
const testInfo = await getTestInfo(page);
const vendorId = testInfo.accounts.vendor;
const vendorContainer = page.locator('div[hx-post*="vendor-changed"]').first();
const vendorHidden = vendorContainer.locator('input[type="hidden"]').first();
await vendorHidden.evaluate((el: HTMLInputElement, value: string) => {
const newInput = document.createElement('input');
newInput.type = 'hidden';
newInput.name = el.name;
newInput.value = value;
el.parentNode.replaceChild(newInput, el);
}, vendorId.toString());
// Dispatch change on the container to trigger HTMX
await vendorContainer.evaluate((el: HTMLElement) => {
el.dispatchEvent(new Event('change', { bubbles: true }));
});
// Wait for HTMX response
await page.waitForResponse(response => response.url().includes('/vendor-changed') && response.status() === 200);
await page.waitForTimeout(500);
// Should NOT have pre-populated account rows - only the "New account" button row
const accountRows = page.locator('#account-entries tbody tr');
const rowCount = await accountRows.count();
// With multi-client, no pre-population should happen, so only 1 row (the "New account" button)
expect(rowCount).toBe(1);
// Switch back to single-client mode for other tests
await page.request.get('/test-set-client-mode?mode=single-client');
});
});