host-schema.test.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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('rejects a bad security enum', () => {
  33. expect(() => HostFormSchema.parse({ ...valid, security: 'bogus' })).toThrow();
  34. });
  35. it('rejects a tag with invalid characters', () => {
  36. expect(() => HostFormSchema.parse({ ...valid, tags: ['lower-case'] })).toThrow();
  37. });
  38. it('rejects more than 10 tags', () => {
  39. expect(() =>
  40. HostFormSchema.parse({ ...valid, tags: Array.from({ length: 11 }, (_, i) => `T${i}`) }),
  41. ).toThrow();
  42. });
  43. it('rejects a bad mihomoIpVersion enum', () => {
  44. expect(() => HostFormSchema.parse({ ...valid, mihomoIpVersion: 'nope' })).toThrow();
  45. });
  46. it('rejects a bad excludeFromSubTypes value', () => {
  47. expect(() => HostFormSchema.parse({ ...valid, excludeFromSubTypes: ['xml'] })).toThrow();
  48. });
  49. it('defaults security to "same" and port to 0', () => {
  50. const parsed = HostFormSchema.parse({ inboundId: 1, remark: 'r' });
  51. expect(parsed.security).toBe('same');
  52. expect(parsed.port).toBe(0);
  53. expect(parsed.tags).toEqual([]);
  54. });
  55. });