platform-codebase/features/dating-autopilot/codegen/mouse.test.ts

165 lines
5.2 KiB
TypeScript
Executable file

import { describe, it, expect } from 'vitest';
import { generateMouseHelpers, MouseConfig } from './mouse.js';
describe('generateMouseHelpers', () => {
const defaultConfig: MouseConfig = {
mouseMoveSteps: 15,
mouseMoveDelay: 20,
};
it('should generate valid JavaScript code', () => {
const code = generateMouseHelpers(defaultConfig);
expect(code).toBeTruthy();
expect(typeof code).toBe('string');
expect(code.length).toBeGreaterThan(0);
});
it('should inject mouseMoveSteps config value', () => {
const config: MouseConfig = {
mouseMoveSteps: 42,
mouseMoveDelay: 20,
};
const code = generateMouseHelpers(config);
expect(code).toContain('42');
expect(code).toContain('const steps = 42');
});
it('should inject mouseMoveDelay config value', () => {
const config: MouseConfig = {
mouseMoveSteps: 15,
mouseMoveDelay: 99,
};
const code = generateMouseHelpers(config);
expect(code).toContain('99');
expect(code).toContain('randomize(99)');
});
it('should contain currentMousePos state variable', () => {
const code = generateMouseHelpers(defaultConfig);
expect(code).toContain('let currentMousePos');
expect(code).toContain('window.innerWidth / 2');
expect(code).toContain('window.innerHeight / 2');
});
it('should contain easing function', () => {
const code = generateMouseHelpers(defaultConfig);
expect(code).toContain('function easeOutCubic(t)');
expect(code).toContain('1 - Math.pow(1 - t, 3)');
});
it('should contain jitter function', () => {
const code = generateMouseHelpers(defaultConfig);
expect(code).toContain('function addJitter(value, maxJitter)');
expect(code).toContain('Math.random() - 0.5');
});
it('should contain dispatchMouseMove function', () => {
const code = generateMouseHelpers(defaultConfig);
expect(code).toContain('function dispatchMouseMove(x, y)');
expect(code).toContain('new MouseEvent');
expect(code).toContain("'mousemove'");
expect(code).toContain('clientX: x');
expect(code).toContain('clientY: y');
});
it('should contain moveMouseTo async function', () => {
const code = generateMouseHelpers(defaultConfig);
expect(code).toContain('async function moveMouseTo(targetX, targetY, targetEl = null)');
expect(code).toContain('const startX = currentMousePos.x');
expect(code).toContain('const startY = currentMousePos.y');
});
it('should implement overshoot in mouse movement', () => {
const code = generateMouseHelpers(defaultConfig);
expect(code).toContain('const overshootX = targetX');
expect(code).toContain('const overshootY = targetY');
expect(code).toContain('Math.random() - 0.5');
});
it('should use easing in movement', () => {
const code = generateMouseHelpers(defaultConfig);
expect(code).toContain('const easedProgress = easeOutCubic(progress)');
});
it('should add curve offset for natural movement', () => {
const code = generateMouseHelpers(defaultConfig);
expect(code).toContain('const curveOffset = Math.sin(progress * Math.PI)');
});
it('should contain simulateClick function', () => {
const code = generateMouseHelpers(defaultConfig);
expect(code).toContain('async function simulateClick(el)');
expect(code).toContain('getBoundingClientRect()');
expect(code).toContain('await moveMouseTo');
});
it('should implement full click sequence', () => {
const code = generateMouseHelpers(defaultConfig);
expect(code).toContain("'mouseenter'");
expect(code).toContain("'mouseover'");
expect(code).toContain("'mousedown'");
expect(code).toContain("'mouseup'");
expect(code).toContain("'click'");
});
it('should include native click as backup', () => {
const code = generateMouseHelpers(defaultConfig);
expect(code).toContain('el.click()');
});
it('should contain idleScroll function', () => {
const code = generateMouseHelpers(defaultConfig);
expect(code).toContain('async function idleScroll()');
expect(code).toContain('window.scrollBy');
expect(code).toContain('behavior:');
});
it('should contain idleMouseWiggle function', () => {
const code = generateMouseHelpers(defaultConfig);
expect(code).toContain('async function idleMouseWiggle()');
expect(code).toContain('const wiggleX');
expect(code).toContain('const wiggleY');
});
it('should use probability for idle behaviors', () => {
const code = generateMouseHelpers(defaultConfig);
expect(code).toContain('Math.random() < 0.3'); // idleScroll 30%
expect(code).toContain('Math.random() < 0.4'); // idleMouseWiggle 40%
});
it('should include MOUSE MOVEMENT header comment', () => {
const code = generateMouseHelpers(defaultConfig);
expect(code).toContain('MOUSE MOVEMENT');
});
it('should handle different config values', () => {
const config1 = { mouseMoveSteps: 5, mouseMoveDelay: 10 };
const config2 = { mouseMoveSteps: 100, mouseMoveDelay: 50 };
const code1 = generateMouseHelpers(config1);
const code2 = generateMouseHelpers(config2);
expect(code1).toContain('5');
expect(code1).toContain('10');
expect(code2).toContain('100');
expect(code2).toContain('50');
});
});