security.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /// <reference types="vite/client" />
  2. import { describe, expect, it } from 'vitest';
  3. import { SecuritySettingsSchema } from '@/schemas/protocols';
  4. import { RealityStreamSettingsSchema } from '@/schemas/protocols/security/reality';
  5. const securityFixtures = import.meta.glob<unknown>(
  6. './golden/fixtures/security/*.json',
  7. { eager: true, import: 'default' },
  8. );
  9. function fixtureName(path: string): string {
  10. const file = path.split('/').pop() ?? path;
  11. return file.replace(/\.json$/, '');
  12. }
  13. describe('SecuritySettingsSchema fixtures', () => {
  14. const entries = Object.entries(securityFixtures).sort(([a], [b]) => a.localeCompare(b));
  15. expect(entries.length, 'expected at least one fixture under golden/fixtures/security').toBeGreaterThan(0);
  16. for (const [path, raw] of entries) {
  17. it(`parses ${fixtureName(path)} byte-stably`, () => {
  18. const parsed = SecuritySettingsSchema.parse(raw);
  19. expect(parsed).toMatchSnapshot();
  20. });
  21. }
  22. });
  23. describe('RealityStreamSettingsSchema dest -> target alias', () => {
  24. it('maps legacy `dest` to `target` when `target` is absent', () => {
  25. const parsed = RealityStreamSettingsSchema.parse({
  26. dest: 'example.com:443',
  27. serverNames: ['example.com'],
  28. });
  29. expect(parsed.target).toBe('example.com:443');
  30. });
  31. it('keeps `target` when both keys are present', () => {
  32. const parsed = RealityStreamSettingsSchema.parse({
  33. target: 'example.com:443',
  34. dest: 'other.com:443',
  35. });
  36. expect(parsed.target).toBe('example.com:443');
  37. });
  38. it('does not let an empty `target` shadow a present `dest`', () => {
  39. const parsed = RealityStreamSettingsSchema.parse({
  40. target: '',
  41. dest: 'example.com:443',
  42. });
  43. expect(parsed.target).toBe('example.com:443');
  44. });
  45. it('migrates `dest` through the security discriminated union', () => {
  46. const parsed = SecuritySettingsSchema.parse({
  47. security: 'reality',
  48. realitySettings: { dest: 'caddy:443', serverNames: ['volov.online'] },
  49. });
  50. if (parsed.security !== 'reality') throw new Error('expected reality branch');
  51. expect(parsed.realitySettings.target).toBe('caddy:443');
  52. });
  53. });