protocol-capabilities.test.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /// <reference types="vite/client" />
  2. import { describe, expect, it } from 'vitest';
  3. import {
  4. canEnableTls,
  5. canEnableReality,
  6. canEnableTlsFlow,
  7. canEnableStream,
  8. canEnableVisionSeed,
  9. isSS2022,
  10. isSSMultiUser,
  11. } from '@/lib/xray/protocol-capabilities';
  12. // Pure-function tests for the capability predicates. Each fixture × stream
  13. // case is locked via snapshot — these were captured at the close of the
  14. // legacy class migration and verified byte-equal to the legacy Inbound
  15. // class instance methods. Drift past this baseline is a regression.
  16. const fixtures = import.meta.glob<unknown>('./golden/fixtures/inbound/*.json', {
  17. eager: true,
  18. import: 'default',
  19. });
  20. interface FixtureShape {
  21. protocol: string;
  22. settings: Record<string, unknown>;
  23. }
  24. const STREAM_CASES: { network: string; security: string }[] = [
  25. { network: 'tcp', security: 'none' },
  26. { network: 'tcp', security: 'tls' },
  27. { network: 'tcp', security: 'reality' },
  28. { network: 'ws', security: 'none' },
  29. { network: 'ws', security: 'tls' },
  30. { network: 'grpc', security: 'none' },
  31. { network: 'grpc', security: 'tls' },
  32. { network: 'grpc', security: 'reality' },
  33. { network: 'kcp', security: 'none' },
  34. { network: 'httpupgrade', security: 'none' },
  35. { network: 'httpupgrade', security: 'tls' },
  36. { network: 'xhttp', security: 'none' },
  37. { network: 'xhttp', security: 'tls' },
  38. { network: 'xhttp', security: 'reality' },
  39. ];
  40. function fixtureName(path: string): string {
  41. return (path.split('/').pop() ?? path).replace(/\.json$/, '');
  42. }
  43. describe('protocol capability predicates', () => {
  44. const entries = Object.entries(fixtures).sort(([a], [b]) => a.localeCompare(b));
  45. for (const [path, raw] of entries) {
  46. const name = fixtureName(path);
  47. const fix = raw as FixtureShape;
  48. for (const stream of STREAM_CASES) {
  49. it(`${name} :: ${stream.network}/${stream.security}`, () => {
  50. const values = {
  51. protocol: fix.protocol,
  52. streamSettings: { network: stream.network, security: stream.security },
  53. settings: fix.settings,
  54. };
  55. const result = {
  56. canEnableTls: canEnableTls(values),
  57. canEnableReality: canEnableReality(values),
  58. canEnableTlsFlow: canEnableTlsFlow(values),
  59. canEnableStream: canEnableStream(values),
  60. canEnableVisionSeed: canEnableVisionSeed(values),
  61. isSS2022: isSS2022(values),
  62. isSSMultiUser: isSSMultiUser(values),
  63. };
  64. expect(result).toMatchSnapshot();
  65. });
  66. }
  67. }
  68. });