Test async ordering - event-loop / queue / channel ordering assertions, JS Promise microtask vs macrotask ordering, Python `asyncio.gather` vs `asyncio.wait_for` semantics, Go goroutine + channel happens-before relationships, async/await re-entrancy. Use deterministic schedulers (sinon fake timers, asyncio test mode) to remove run-to-run variance. Use when a callback fires twice, a later response overwrites an earlier one, or a cancelled parent task leaves a child still running - bugs where completion order, not shared memory, is the defect.
87
86%
Does it follow best practices?
Impact
94%
1.00xAverage score across 3 eval scenarios
Passed
No findings from the security scan
src/searchController.js runs a search on every keystroke. Users report that
results occasionally snap back to what they typed a moment ago: a slow
request for an earlier query lands after a fast request for the current one
and overwrites it.
The controller already carries a guard for this. Nothing tests it. The existing test resolves one request at a time, so the out-of-order case never occurs.
Add src/searchController.test.js proving that a response arriving for a
superseded query does not replace the current results, and that the ordering
under test is decided by the test rather than by how fast the machine
happens to be.
Run npm test before you finish; it must pass.
Leave src/searchController.basic.test.js in place.
Extract the following files before beginning.
=============== FILE: package.json =============== { "name": "search-ui", "version": "0.6.0", "private": true, "scripts": { "test": "node --test" } }
=============== FILE: src/searchController.js =============== 'use strict';
function createSearchController(search) { let latestSequence = 0; let state = { query: null, results: [] };
return { async run(query) { latestSequence += 1; const sequence = latestSequence; const results = await search(query); if (sequence !== latestSequence) { return { applied: false, reason: 'SUPERSEDED' }; } state = { query, results }; return { applied: true, reason: null }; }, snapshot() { return state; }, }; }
module.exports = { createSearchController };
=============== FILE: src/searchController.basic.test.js =============== 'use strict';
const test = require('node:test'); const assert = require('node:assert/strict'); const { createSearchController } = require('./searchController');
test('applies results for a single query', async () => {
const controller = createSearchController(async (query) => [${query}-hit]);
await controller.run('apple');
assert.deepEqual(controller.snapshot(), { query: 'apple', results: ['apple-hit'] }); });