Files
orca/cloud/apps/relay/src/assignment/assignment-rejection-log-window.test.ts
92dfb4fda6 test: trim redundant coverage and fix vacuous assertions (#32305)
* test: remove redundant cases and repair behavioral assertions

* docs: reconcile audit records with current main

* fix(deps): apply security updates from #32101

Apply the six-file DOMPurify 3.4.17 and Next.js 16.4.1 patch from PR #32101 to resolve the dependency security gate for this test audit. Original patch by Brennan Benson.

* test: satisfy changed-line quality gates

Add required braces and replace filter indexing with find. Rerun both CI quality gates and the 27 affected relay tests; refresh audit hashes and record the corrections.

---------

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
Co-authored-by: Brennan Benson <79079362+brennanb2025@users.noreply.github.com>
2026-10-23 03:52:51 -07:00

83 lines
2.9 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { AssignmentRejectionLogWindow } from './assignment-rejection-log-window.js'
describe('assignment rejection log window', () => {
it('emits once per key per window and closes it with its own suppressed count', () => {
const timers = manualTimers()
const closed: { key: string; suppressed: number; sample: string }[] = []
const window = new AssignmentRejectionLogWindow<string>({
windowMs: 10_000,
schedule: timers.schedule,
onWindowClosed: (input) => closed.push(input)
})
const key = 'assign:placement:host-rate-limited'
expect(window.admit(key, 'host-a')).toBe(true)
expect(window.admit(key, 'host-b')).toBe(false)
expect(window.admit(key, 'host-c')).toBe(false)
expect(closed).toEqual([])
timers.runPending()
expect(closed).toEqual([{ key, suppressed: 2, sample: 'host-c' }])
// The next window starts clean instead of inheriting the closed window's count.
expect(window.admit(key, 'host-d')).toBe(true)
timers.runPending()
expect(closed).toHaveLength(1)
})
it('keeps distinct keys on independent windows', () => {
const timers = manualTimers()
const window = new AssignmentRejectionLogWindow<string>({
windowMs: 10_000,
schedule: timers.schedule,
onWindowClosed: () => undefined
})
expect(window.admit('assign:placement:host-rate-limited', 'host-a')).toBe(true)
expect(window.admit('assign:placement:queue-full', 'host-a')).toBe(true)
expect(window.admit('assign:sticky:host-rate-limited', 'host-a')).toBe(true)
expect(window.admit('assign:placement:queue-full', 'host-a')).toBe(false)
})
it('bounds the tracked keys and reports what an evicted window suppressed', () => {
const timers = manualTimers()
const closed: { key: string; suppressed: number }[] = []
const window = new AssignmentRejectionLogWindow<string>({
windowMs: 10_000,
schedule: timers.schedule,
onWindowClosed: ({ key, suppressed }) => closed.push({ key, suppressed })
})
window.admit('key-0', 'host-a')
window.admit('key-0', 'host-b')
for (let index = 1; index < 200; index++) window.admit(`key-${index}`, 'host-a')
expect(closed[0]).toEqual({ key: 'key-0', suppressed: 1 })
// Evicted keys emit again rather than staying silently suppressed.
expect(window.admit('key-0', 'host-a')).toBe(true)
expect(window.admit('key-199', 'host-a')).toBe(false)
})
})
function manualTimers(): {
schedule: (callback: () => void) => () => void
runPending: () => void
} {
const pending = new Map<number, () => void>()
let nextId = 0
return {
schedule: (callback) => {
const id = nextId++
pending.set(id, callback)
return () => pending.delete(id)
},
runPending: () => {
for (const [id, callback] of [...pending]) {
pending.delete(id)
callback()
}
}
}
}