1
0

enhancements.test.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * Phase 4 功能增强回归测试
  3. * 覆盖:进制转换 BigInt、FileTime 转换、编解码等
  4. */
  5. import { describe, it, expect } from 'vitest';
  6. // ═══════════════════════════════════════════════════════
  7. // 1. 进制转换 BigInt 支持
  8. // ═══════════════════════════════════════════════════════
  9. describe('进制转换 BigInt', () => {
  10. function radixConvert(srcValue, from, to) {
  11. const src = String(srcValue).trim();
  12. if (!src) return '0';
  13. let decimal;
  14. if (from === 10) {
  15. decimal = BigInt(src);
  16. } else {
  17. decimal = BigInt(0);
  18. const digits = src.toLowerCase();
  19. const base = BigInt(from);
  20. for (let i = 0; i < digits.length; i++) {
  21. const d = parseInt(digits[i], from);
  22. if (isNaN(d)) throw new Error('Invalid digit');
  23. decimal = decimal * base + BigInt(d);
  24. }
  25. }
  26. if (to === 10) return decimal.toString();
  27. if (decimal === 0n) return '0';
  28. const base = BigInt(to);
  29. const chars = '0123456789abcdef';
  30. let result = '';
  31. let n = decimal < 0n ? -decimal : decimal;
  32. while (n > 0n) {
  33. result = chars[Number(n % base)] + result;
  34. n = n / base;
  35. }
  36. return (decimal < 0n ? '-' : '') + result;
  37. }
  38. it('普通数字 10→16', () => {
  39. expect(radixConvert('255', 10, 16)).toBe('ff');
  40. });
  41. it('普通数字 16→10', () => {
  42. expect(radixConvert('ff', 16, 10)).toBe('255');
  43. });
  44. it('超大整数 10→16 不丢精度', () => {
  45. const big = '9999999999999999999999';
  46. const hex = radixConvert(big, 10, 16);
  47. const back = radixConvert(hex, 16, 10);
  48. expect(back).toBe(big);
  49. });
  50. it('超大整数 10→2', () => {
  51. const result = radixConvert('18446744073709551615', 10, 2);
  52. expect(result).toBe('1111111111111111111111111111111111111111111111111111111111111111');
  53. });
  54. it('零', () => {
  55. expect(radixConvert('0', 10, 16)).toBe('0');
  56. });
  57. it('负数', () => {
  58. expect(radixConvert('-255', 10, 16)).toBe('-ff');
  59. });
  60. });
  61. // ═══════════════════════════════════════════════════════
  62. // 2. Windows FILETIME 转换
  63. // ═══════════════════════════════════════════════════════
  64. describe('Windows FILETIME 转换', () => {
  65. const FILETIME_EPOCH_OFFSET = 11644473600000;
  66. function fileTimeToUnixMs(fileTime) {
  67. return Number(BigInt(fileTime) / BigInt(10000)) - FILETIME_EPOCH_OFFSET;
  68. }
  69. function unixMsToFileTime(unixMs) {
  70. return (BigInt(unixMs) + BigInt(FILETIME_EPOCH_OFFSET)) * BigInt(10000);
  71. }
  72. it('已知 FILETIME → Unix 时间 (2024-01-01 00:00:00 UTC)', () => {
  73. // 2024-01-01T00:00:00Z = Unix 1704067200000ms
  74. // FILETIME = (1704067200000 + 11644473600000) * 10000 = 133480416000000000 ... verify:
  75. const expected_unix = 1704067200000;
  76. const ft = String((BigInt(expected_unix) + BigInt(FILETIME_EPOCH_OFFSET)) * BigInt(10000));
  77. const unixMs = fileTimeToUnixMs(ft);
  78. const date = new Date(unixMs);
  79. expect(date.getUTCFullYear()).toBe(2024);
  80. expect(date.getUTCMonth()).toBe(0);
  81. expect(date.getUTCDate()).toBe(1);
  82. });
  83. it('Unix 时间 → FILETIME 往返', () => {
  84. const now = Date.now();
  85. const ft = unixMsToFileTime(now);
  86. const backMs = fileTimeToUnixMs(ft.toString());
  87. expect(Math.abs(backMs - now)).toBeLessThan(1);
  88. });
  89. it('FILETIME epoch (1601-01-01)', () => {
  90. const unixMs = fileTimeToUnixMs('0');
  91. const date = new Date(unixMs);
  92. expect(date.getUTCFullYear()).toBe(1601);
  93. });
  94. });
  95. // ═══════════════════════════════════════════════════════
  96. // 3. evalCore 替换验证
  97. // ═══════════════════════════════════════════════════════
  98. describe('evalCore 安全替换', () => {
  99. it('new Function 可以执行简单代码', () => {
  100. let result = 0;
  101. const fn = new Function('return 42');
  102. result = fn();
  103. expect(result).toBe(42);
  104. });
  105. it('new Function 有独立作用域', () => {
  106. const localVar = 'original';
  107. new Function('var localVar = "modified"')();
  108. expect(localVar).toBe('original');
  109. });
  110. });