host-schema.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /// <reference types="vite/client" />
  2. import { describe, expect, it } from 'vitest';
  3. import { HostFormSchema } from '@/schemas/api/host';
  4. describe('HostFormSchema', () => {
  5. const valid = {
  6. inboundId: 1,
  7. remark: 'cdn-front',
  8. address: 'cdn.example.com',
  9. port: 8443,
  10. security: 'tls',
  11. tags: ['CDN', 'EU'],
  12. mihomoIpVersion: 'dual',
  13. excludeFromSubTypes: ['clash'],
  14. };
  15. it('parses a valid host', () => {
  16. const parsed = HostFormSchema.parse(valid);
  17. expect(parsed.remark).toBe('cdn-front');
  18. expect(parsed.security).toBe('tls');
  19. expect(parsed.tags).toEqual(['CDN', 'EU']);
  20. expect(parsed.excludeFromSubTypes).toEqual(['clash']);
  21. });
  22. it('rejects an empty remark', () => {
  23. expect(() => HostFormSchema.parse({ ...valid, remark: '' })).toThrow();
  24. });
  25. it('accepts a templated remark up to 256 chars and rejects beyond', () => {
  26. expect(() => HostFormSchema.parse({ ...valid, remark: 'x'.repeat(256) })).not.toThrow();
  27. expect(() => HostFormSchema.parse({ ...valid, remark: 'x'.repeat(257) })).toThrow();
  28. });
  29. it('rejects an out-of-range port', () => {
  30. expect(() => HostFormSchema.parse({ ...valid, port: 70000 })).toThrow();
  31. });
  32. it('accepts a single vlessRoute 0-65535 and rejects specs/out-of-range', () => {
  33. expect(() => HostFormSchema.parse({ ...valid, vlessRoute: '443' })).not.toThrow();
  34. expect(() => HostFormSchema.parse({ ...valid, vlessRoute: '0' })).not.toThrow();
  35. expect(() => HostFormSchema.parse({ ...valid, vlessRoute: '65535' })).not.toThrow();
  36. expect(() => HostFormSchema.parse({ ...valid, vlessRoute: '' })).not.toThrow();
  37. expect(() => HostFormSchema.parse({ ...valid, vlessRoute: '53,443' })).toThrow();
  38. expect(() => HostFormSchema.parse({ ...valid, vlessRoute: '1000-2000' })).toThrow();
  39. expect(() => HostFormSchema.parse({ ...valid, vlessRoute: '70000' })).toThrow();
  40. expect(() => HostFormSchema.parse({ ...valid, vlessRoute: 'abc' })).toThrow();
  41. });
  42. it('rejects a bad security enum', () => {
  43. expect(() => HostFormSchema.parse({ ...valid, security: 'bogus' })).toThrow();
  44. });
  45. it('rejects a tag with invalid characters', () => {
  46. expect(() => HostFormSchema.parse({ ...valid, tags: ['lower-case'] })).toThrow();
  47. });
  48. it('rejects more than 10 tags', () => {
  49. expect(() =>
  50. HostFormSchema.parse({ ...valid, tags: Array.from({ length: 11 }, (_, i) => `T${i}`) }),
  51. ).toThrow();
  52. });
  53. it('rejects a bad mihomoIpVersion enum', () => {
  54. expect(() => HostFormSchema.parse({ ...valid, mihomoIpVersion: 'nope' })).toThrow();
  55. });
  56. it('rejects a bad excludeFromSubTypes value', () => {
  57. expect(() => HostFormSchema.parse({ ...valid, excludeFromSubTypes: ['xml'] })).toThrow();
  58. });
  59. it('defaults security to "same" and port to 0', () => {
  60. const parsed = HostFormSchema.parse({ inboundId: 1, remark: 'r' });
  61. expect(parsed.security).toBe('same');
  62. expect(parsed.port).toBe(0);
  63. expect(parsed.tags).toEqual([]);
  64. });
  65. });