// Testing Laboratory5 Testing Utilities

Automation Utilities & Harness Experiments

Modular engineering artifacts extracted from real testing workflows. Demonstrating custom fixtures, race condition probes, and fault-injection interceptors.

lab-01Database & StateUTILITY PATTERN

Isolated Database Fixture with Automatic Transaction Rollback

PlaywrightPostgreSQLTypeORMTypeScript
Problem Solved:

Running tests in parallel against a shared PostgreSQL instance causes data collisions and flakiness when tests modify the same wallet balance or beneficiary rows.

Engineering Implementation:

A custom Playwright worker fixture that wraps each test execution in an isolated PostgreSQL transaction, rolling back on teardown to eliminate test-data pollution.

// Playwright fixture with atomic transaction rollback
import { test as base } from '@playwright/test';
import { PoolClient } from 'pg';
import { dbPool } from './db-pool';

export const test = base.extend<{ transactionalDb: PoolClient }>({
  transactionalDb: async ({}, use) => {
    const client = await dbPool.connect();
    await client.query('BEGIN');
    
    // Provide isolated client to test body
    await use(client);
    
    // Force rollback on test completion to keep database spotless
    await client.query('ROLLBACK');
    client.release();
  },
});
lab-02ConcurrencyREFERENCE SPEC

Redis Distributed Lock Race Condition Prober

Node.jsioredisPromise.allIdempotency
Problem Solved:

Standard sequential tests miss race conditions where secondary callers arrive while the primary request has dropped the lock but not yet committed the transaction.

Engineering Implementation:

A multi-threaded concurrent request probe that blasts identical Idempotency-Key payloads with microsecond synchronization to detect lock-acquisition race conditions.

// Concurrency race tester
export async function probeConcurrentIdempotency(endpoint: string, idempotencyKey: string, concurrency = 5) {
  const payload = { amount: 100, currency: 'EUR' };
  
  // Synchronize execution burst across all promises
  const barrier = new Promise(resolve => setTimeout(resolve, 50));
  
  const requests = Array.from({ length: concurrency }).map(async (_, idx) => {
    await barrier;
    return fetch(endpoint, {
      method: 'POST',
      headers: {
        'Idempotency-Key': idempotencyKey,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(payload)
    });
  });

  return Promise.all(requests);
}
lab-03Performance (k6)REFERENCE SPEC

Deterministic k6 Load Scenario for High-Throughput FX Rate Quotes

k6JavaScriptDockerLoad Testing
Problem Solved:

Accurately measuring API read throughput limits and memory footprint before enabling public live rate websocket streaming.

Engineering Implementation:

A specialized k6 workload script executing 10 Virtual Users over 15 seconds against the exchange rates endpoint, achieving 8,272 req/s with 0% error rate in local containerized benchmark testing.

import http from 'k6/http';
import { check } from 'k6';

export const options = {
  scenarios: {
    high_throughput_reads: {
      executor: 'constant-vus',
      vus: 10,
      duration: '15s',
    },
  },
  thresholds: {
    http_req_failed: ['rate<0.01'],
    http_req_duration: ['p(95)<10'], // 95% of requests must complete below 10ms
  },
};

export default function () {
  const res = http.get('http://localhost:3000/api/v1/exchange-rates');
  check(res, {
    'status is 200': (r) => r.status === 200,
    'has exchange rates': (r) => JSON.parse(r.body).length >= 6,
  });
}
lab-04Chaos & ReliabilityUTILITY PATTERN

Network Fault-Injection Interceptor for Playwright

PlaywrightNetwork Route MocksError Boundaries
Problem Solved:

Verifies whether frontend UIs crash, show confusing empty states (like WP-QA-ERR-001), or properly present user-friendly error banners and retry actions.

Engineering Implementation:

A pluggable test utility for intercepting specific REST routes and injecting delayed timeouts, corrupted JSON payloads, and HTTP 500/503 errors to verify frontend resilience.

// Route fault injector fixture
export async function injectRouteFault(page: Page, urlPattern: string, fault: '500' | 'timeout' | 'drop') {
  await page.route(urlPattern, async (route) => {
    if (fault === '500') {
      await route.fulfill({
        status: 500,
        contentType: 'application/json',
        body: JSON.stringify({ statusCode: 500, message: 'Simulated Internal Server Error' })
      });
    } else if (fault === 'timeout') {
      await new Promise(res => setTimeout(res, 10000));
      await route.abort('timedout');
    } else {
      await route.abort('failed');
    }
  });
}
lab-05Quality EngineeringUTILITY PATTERN

Known Defect Quarantine Wrapper with test.fail()

PlaywrightRegression StrategyCI Quality Gates
Problem Solved:

Prevents teams from either disabling failing tests (losing visibility) or letting CI stay red (destroying broken-window trust in CI).

Engineering Implementation:

A formal regression engineering pattern that wraps open, unresolved bugs in test.fail() annotations, ensuring active tracking in CI without turning build pipelines red.

// test.fail() pattern for active defect tracking
test('WP-QA-003: GET /transfers/:id with malformed UUID must return 400 instead of 500', async ({ request }) => {
  test.info().annotations.push({
    type: 'issue',
    description: 'WP-QA-003: Postgres 22P02 unhandled error in controller'
  });
  
  // Declares expected failure for active defect
  test.fail(true, 'Known unresolved defect: route returns 500 rather than 400 validation error');
  
  const response = await request.get('/api/v1/transfers/not-a-uuid');
  expect(response.status()).toBe(400); // Fails with 500, marked as expected failure in CI report
});