color.test.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import * as fc from 'fast-check';
  2. import { describe, it, expect } from 'vitest';
  3. import { getColor } from '../src/utils/color.js';
  4. /**
  5. * Feature: ball-block-breaker, Property 17: 颜色计算有效性
  6. * **Validates: Requirements 8.6**
  7. *
  8. * 对于任意正整数 count,getColor(count) 应返回一个有效的7字符十六进制颜色字符串(格式为 #RRGGBB)。
  9. */
  10. describe('Color Utils', () => {
  11. it('Property 17: getColor returns a valid 7-character hex color string for any positive integer', () => {
  12. fc.assert(
  13. fc.property(
  14. fc.integer({ min: 1, max: 1_000_000 }),
  15. (count) => {
  16. const color = getColor(count);
  17. // Must be a string of exactly 7 characters
  18. expect(typeof color).toBe('string');
  19. expect(color).toHaveLength(7);
  20. // Must start with '#'
  21. expect(color[0]).toBe('#');
  22. // Remaining 6 characters must be valid hex digits [0-9a-f]
  23. const hexPart = color.slice(1);
  24. expect(hexPart).toMatch(/^[0-9a-f]{6}$/);
  25. }
  26. ),
  27. { numRuns: 100 }
  28. );
  29. });
  30. });