Files
spoon/apps/next/tests/unit/workspace-helpers.test.ts

73 lines
2.3 KiB
TypeScript

import { describe, expect, test } from 'vitest';
import {
createRequestSequencer,
isNearBottom,
NEAR_BOTTOM_THRESHOLD,
RECOVERY_THRESHOLD,
shouldShowRecovery,
} from '@/components/agent-workspace/workspace-helpers';
describe('createRequestSequencer', () => {
test('only the latest issued token is considered current', () => {
const sequencer = createRequestSequencer();
const first = sequencer.next();
const second = sequencer.next();
// Simulate the first (slower) request resolving after the second: it is
// stale and must be dropped.
expect(sequencer.isLatest(first)).toBe(false);
// The newest request wins.
expect(sequencer.isLatest(second)).toBe(true);
});
test('a fresh request supersedes a previously latest one', () => {
const sequencer = createRequestSequencer();
const token = sequencer.next();
expect(sequencer.isLatest(token)).toBe(true);
sequencer.next();
expect(sequencer.isLatest(token)).toBe(false);
});
});
describe('shouldShowRecovery', () => {
test('defaults to a threshold of 3 consecutive failures', () => {
expect(RECOVERY_THRESHOLD).toBe(3);
expect(shouldShowRecovery(0)).toBe(false);
expect(shouldShowRecovery(1)).toBe(false);
expect(shouldShowRecovery(2)).toBe(false);
expect(shouldShowRecovery(3)).toBe(true);
expect(shouldShowRecovery(4)).toBe(true);
});
test('honours a custom threshold', () => {
expect(shouldShowRecovery(1, 2)).toBe(false);
expect(shouldShowRecovery(2, 2)).toBe(true);
});
});
describe('isNearBottom', () => {
test('true when within the threshold band of the bottom', () => {
expect(
isNearBottom({ scrollHeight: 1000, scrollTop: 940, clientHeight: 100 }),
).toBe(true);
});
test('false when scrolled up beyond the threshold', () => {
expect(
isNearBottom({ scrollHeight: 1000, scrollTop: 200, clientHeight: 100 }),
).toBe(false);
});
test('uses NEAR_BOTTOM_THRESHOLD as the default band', () => {
expect(NEAR_BOTTOM_THRESHOLD).toBe(80);
// Exactly at the threshold is not "near" (strict less-than).
expect(
isNearBottom({ scrollHeight: 1000, scrollTop: 820, clientHeight: 100 }),
).toBe(false);
expect(
isNearBottom({ scrollHeight: 1000, scrollTop: 821, clientHeight: 100 }),
).toBe(true);
});
});