security.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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>('./golden/fixtures/security/*.json', {
  6. eager: true,
  7. 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(
  16. entries.length,
  17. 'expected at least one fixture under golden/fixtures/security',
  18. ).toBeGreaterThan(0);
  19. for (const [path, raw] of entries) {
  20. it(`parses ${fixtureName(path)} byte-stably`, () => {
  21. const parsed = SecuritySettingsSchema.parse(raw);
  22. expect(parsed).toMatchSnapshot();
  23. });
  24. }
  25. });
  26. describe('RealityStreamSettingsSchema dest -> target alias', () => {
  27. it('maps legacy `dest` to `target` when `target` is absent', () => {
  28. const parsed = RealityStreamSettingsSchema.parse({
  29. dest: 'example.com:443',
  30. serverNames: ['example.com'],
  31. });
  32. expect(parsed.target).toBe('example.com:443');
  33. });
  34. it('keeps `target` when both keys are present', () => {
  35. const parsed = RealityStreamSettingsSchema.parse({
  36. target: 'example.com:443',
  37. dest: 'other.com:443',
  38. });
  39. expect(parsed.target).toBe('example.com:443');
  40. });
  41. it('does not let an empty `target` shadow a present `dest`', () => {
  42. const parsed = RealityStreamSettingsSchema.parse({
  43. target: '',
  44. dest: 'example.com:443',
  45. });
  46. expect(parsed.target).toBe('example.com:443');
  47. });
  48. it('migrates `dest` through the security discriminated union', () => {
  49. const parsed = SecuritySettingsSchema.parse({
  50. security: 'reality',
  51. realitySettings: { dest: 'caddy:443', serverNames: ['volov.online'] },
  52. });
  53. if (parsed.security !== 'reality') throw new Error('expected reality branch');
  54. expect(parsed.realitySettings.target).toBe('caddy:443');
  55. });
  56. });