import { describe, it, expect } from 'vitest'; import { initFromPolicy, initGeneratorState, handleLengthChange } from '../generator-policy'; describe('generator site policy constraints', () => { describe('Case 1 — no policy', () => { it('defaults to 64 when unconstrained and fresh', () => { const r = initFromPolicy(null, { currentLength: 64, userHasAdjusted: false }); expect(r.length).toBe(64); }); it('preserves user length when no policy and user adjusted', () => { const r = initFromPolicy(null, { currentLength: 80, userHasAdjusted: true }); expect(r.length).toBe(80); }); it('shows no constraint note', () => { const r = initFromPolicy(null, { currentLength: 64, userHasAdjusted: false }); expect(r.constraintNote).toBeFalsy(); }); it('leaves slider bounds at defaults', () => { const r = initFromPolicy(null, { currentLength: 64, userHasAdjusted: false }); expect(r.sliderMin).toBe(8); expect(r.sliderMax).toBe(256); }); }); describe('Case 2 — site maxLength, fresh open', () => { it('snaps to site maxLength', () => { const r = initFromPolicy( { maxLength: 100 }, { currentLength: 64, userHasAdjusted: false }, ); expect(r.length).toBe(100); }); it('shows constraint note containing maxLength', () => { const r = initFromPolicy( { maxLength: 100 }, { currentLength: 64, userHasAdjusted: false }, ); expect(r.constraintNote).toMatch(/100/); }); it('sets sliderMax to site maxLength', () => { const r = initFromPolicy( { maxLength: 100 }, { currentLength: 64, userHasAdjusted: false }, ); expect(r.sliderMax).toBe(100); }); }); describe('Case 3 — user above site max', () => { it('snaps DOWN to site max', () => { const r = initFromPolicy( { maxLength: 50 }, { currentLength: 80, userHasAdjusted: true }, ); expect(r.length).toBe(50); }); it('shows constraint note after forced snap down', () => { const r = initFromPolicy( { maxLength: 50 }, { currentLength: 80, userHasAdjusted: true }, ); expect(r.constraintNote).toBeTruthy(); }); }); describe('Case 4 — user below site max', () => { it('keeps user length when within bounds', () => { const r = initFromPolicy( { maxLength: 100 }, { currentLength: 20, userHasAdjusted: true }, ); expect(r.length).toBe(20); }); it('shows no constraint note when within bounds', () => { const r = initFromPolicy( { maxLength: 100 }, { currentLength: 20, userHasAdjusted: true }, ); expect(r.constraintNote).toBeFalsy(); }); }); describe('Case 5 — site minLength', () => { it('snaps UP to site minLength', () => { const r = initFromPolicy( { minLength: 12 }, { currentLength: 8, userHasAdjusted: false }, ); expect(r.length).toBe(12); }); it('shows constraint note containing minLength', () => { const r = initFromPolicy( { minLength: 12 }, { currentLength: 8, userHasAdjusted: false }, ); expect(r.constraintNote).toMatch(/12/); }); it('sets sliderMin to site minLength', () => { const r = initFromPolicy( { minLength: 12 }, { currentLength: 8, userHasAdjusted: false }, ); expect(r.sliderMin).toBe(12); }); it('does not snap up when already above minLength', () => { const r = initFromPolicy( { minLength: 12 }, { currentLength: 20, userHasAdjusted: true }, ); expect(r.length).toBe(20); }); }); describe('Case 6 — both min and max', () => { it('snaps to min when below range', () => { const r = initFromPolicy( { minLength: 8, maxLength: 32 }, { currentLength: 4, userHasAdjusted: false }, ); expect(r.length).toBe(8); }); it('snaps to max when above range', () => { const r = initFromPolicy( { minLength: 8, maxLength: 32 }, { currentLength: 64, userHasAdjusted: true }, ); expect(r.length).toBe(32); }); it('keeps user length when within range', () => { const r = initFromPolicy( { minLength: 8, maxLength: 32 }, { currentLength: 20, userHasAdjusted: true }, ); expect(r.length).toBe(20); }); it('defaults to maxLength on fresh open', () => { const r = initFromPolicy( { minLength: 8, maxLength: 32 }, { currentLength: 64, userHasAdjusted: false }, ); expect(r.length).toBe(32); }); it('sets both sliderMin and sliderMax', () => { const r = initFromPolicy( { minLength: 8, maxLength: 32 }, { currentLength: 20, userHasAdjusted: false }, ); expect(r.sliderMin).toBe(8); expect(r.sliderMax).toBe(32); }); }); describe('userHasAdjusted flag', () => { it('is false on fresh generator open', () => { const state = initGeneratorState(); expect(state.userHasAdjusted).toBe(false); }); it('becomes true after slider move', () => { let state = initGeneratorState(); state = handleLengthChange(state, 80); expect(state.userHasAdjusted).toBe(true); }); it('false: policy snap applies', () => { const r = initFromPolicy( { maxLength: 100 }, { currentLength: 64, userHasAdjusted: false }, ); expect(r.length).toBe(100); }); it('true: policy only enforces hard bounds', () => { const r = initFromPolicy( { maxLength: 100 }, { currentLength: 30, userHasAdjusted: true }, ); expect(r.length).toBe(30); }); }); });