tuic-client-config.test.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { describe, expect, it } from 'vitest';
  2. import { buildTuicClientConfig } from '@/pages/clients/tuicConfig';
  3. import type { ClientRecord, InboundOption } from '@/hooks/useClients';
  4. const client: ClientRecord = {
  5. id: 1,
  6. email: '[email protected]',
  7. uuid: 'e79b9107-1607-4e6c-a496-d8f99e4f0dc5',
  8. password: 'testpassword123',
  9. inboundIds: [10],
  10. };
  11. const inbound: InboundOption = {
  12. id: 10,
  13. tag: 'in-8443-udp',
  14. remark: 'TUIC Main',
  15. protocol: 'tuic',
  16. port: 8443,
  17. tuicServer: {
  18. sni: 'vpn.example.com',
  19. congestion_control: 'cubic',
  20. alpn: ['h3', 'spdy/3.1'],
  21. udp_relay_mode: 'native',
  22. zero_rtt_handshake: true,
  23. },
  24. };
  25. describe('buildTuicClientConfig', () => {
  26. it('builds valid YAML proxy entry from tuicServer option', () => {
  27. const cfg = buildTuicClientConfig(client, inbound, 'server.example.com', '');
  28. expect(cfg).toContain('type: tuic');
  29. expect(cfg).toContain('server: server.example.com');
  30. expect(cfg).toContain('port: 8443');
  31. expect(cfg).toContain('uuid: e79b9107-1607-4e6c-a496-d8f99e4f0dc5');
  32. expect(cfg).toContain('password: "testpassword123"');
  33. expect(cfg).toContain('sni: vpn.example.com');
  34. expect(cfg).toContain('congestion-controller: cubic');
  35. expect(cfg).toContain('udp-relay-mode: native');
  36. expect(cfg).toContain('reduce-rtt: true');
  37. });
  38. it('falls back to endpointHost when sni is empty', () => {
  39. const inboundNoSni: InboundOption = {
  40. ...inbound,
  41. tuicServer: {
  42. ...inbound.tuicServer,
  43. sni: '',
  44. },
  45. };
  46. const cfg = buildTuicClientConfig(client, inboundNoSni, 'server.example.com', '');
  47. expect(cfg).toContain('sni: server.example.com');
  48. });
  49. it('escapes quotes in passwords and remarks', () => {
  50. const dangerousClient: ClientRecord = {
  51. ...client,
  52. password: 'pass"with"quotes\nnewline',
  53. };
  54. const cfg = buildTuicClientConfig(dangerousClient, inbound, 'server.example.com', '');
  55. expect(cfg).toContain('password: "pass\\"with\\"quotes\\nnewline"');
  56. });
  57. });