protocol-capabilities.test.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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>(
  17. './golden/fixtures/inbound/*.json',
  18. { eager: true, import: 'default' },
  19. );
  20. interface FixtureShape { protocol: string; settings: Record<string, unknown> }
  21. const STREAM_CASES: { network: string; security: string }[] = [
  22. { network: 'tcp', security: 'none' },
  23. { network: 'tcp', security: 'tls' },
  24. { network: 'tcp', security: 'reality' },
  25. { network: 'ws', security: 'none' },
  26. { network: 'ws', security: 'tls' },
  27. { network: 'grpc', security: 'none' },
  28. { network: 'grpc', security: 'tls' },
  29. { network: 'grpc', security: 'reality' },
  30. { network: 'kcp', security: 'none' },
  31. { network: 'httpupgrade', security: 'none' },
  32. { network: 'httpupgrade', security: 'tls' },
  33. { network: 'xhttp', security: 'none' },
  34. { network: 'xhttp', security: 'tls' },
  35. { network: 'xhttp', security: 'reality' },
  36. ];
  37. function fixtureName(path: string): string {
  38. return (path.split('/').pop() ?? path).replace(/\.json$/, '');
  39. }
  40. describe('protocol capability predicates', () => {
  41. const entries = Object.entries(fixtures).sort(([a], [b]) => a.localeCompare(b));
  42. for (const [path, raw] of entries) {
  43. const name = fixtureName(path);
  44. const fix = raw as FixtureShape;
  45. for (const stream of STREAM_CASES) {
  46. it(`${name} :: ${stream.network}/${stream.security}`, () => {
  47. const values = {
  48. protocol: fix.protocol,
  49. streamSettings: { network: stream.network, security: stream.security },
  50. settings: fix.settings,
  51. };
  52. const result = {
  53. canEnableTls: canEnableTls(values),
  54. canEnableReality: canEnableReality(values),
  55. canEnableTlsFlow: canEnableTlsFlow(values),
  56. canEnableStream: canEnableStream(values),
  57. canEnableVisionSeed: canEnableVisionSeed(values),
  58. isSS2022: isSS2022(values),
  59. isSSMultiUser: isSSMultiUser(values),
  60. };
  61. expect(result).toMatchSnapshot();
  62. });
  63. }
  64. }
  65. });