|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + DEFAULT_RESOLVE_MODE, |
| 5 | + decideResolution, |
| 6 | + gatewayUrlFor, |
| 7 | + lookupMoshpit, |
| 8 | + parseRegistryName, |
| 9 | + type MoshpitLookup, |
| 10 | +} from './moshpit-resolve'; |
| 11 | + |
| 12 | +const registered = (resolved: string): MoshpitLookup => ({ registered: true, resolved }); |
| 13 | +const unregistered: MoshpitLookup = { registered: false, resolved: '' }; |
| 14 | + |
| 15 | +describe('decideResolution — clearnet mode (the default)', () => { |
| 16 | + it('defaults to clearnet', () => { |
| 17 | + expect(DEFAULT_RESOLVE_MODE).toBe('clearnet'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('leaves a working clearnet domain alone even when Moshpit holds the name', () => { |
| 21 | + // The squatting case, from the safe side: someone holds profullstack.ai on |
| 22 | + // clearnet AND we hold it in Moshpit. Default must not hijack it — silently |
| 23 | + // redirecting a domain that resolves is indistinguishable from a takeover. |
| 24 | + const d = decideResolution({ |
| 25 | + hostname: 'profullstack.ai', |
| 26 | + mode: 'clearnet', |
| 27 | + clearnetResolves: true, |
| 28 | + moshpit: registered('profullstack.ai'), |
| 29 | + }); |
| 30 | + expect(d.use).toBe('clearnet'); |
| 31 | + expect(d.reason).toMatch(/backfill/i); |
| 32 | + }); |
| 33 | + |
| 34 | + it('backfills a name clearnet cannot answer', () => { |
| 35 | + const d = decideResolution({ |
| 36 | + hostname: 'original.sploof', |
| 37 | + mode: 'clearnet', |
| 38 | + clearnetResolves: false, |
| 39 | + moshpit: registered('original.sploof'), |
| 40 | + }); |
| 41 | + expect(d.use).toBe('moshpit'); |
| 42 | + expect(d.resolved).toBe('original.sploof'); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe('decideResolution — moshpit mode (the override)', () => { |
| 47 | + it('overrides a live clearnet domain', () => { |
| 48 | + // The whole point of registering profullstack.ai in Moshpit: your version |
| 49 | + // wins regardless of who holds the clearnet domain. |
| 50 | + const d = decideResolution({ |
| 51 | + hostname: 'profullstack.ai', |
| 52 | + mode: 'moshpit', |
| 53 | + clearnetResolves: true, |
| 54 | + moshpit: registered('profullstack.ai'), |
| 55 | + }); |
| 56 | + expect(d.use).toBe('moshpit'); |
| 57 | + expect(d.reason).toMatch(/overriding the clearnet domain/i); |
| 58 | + expect(d.resolved).toBe('profullstack.ai'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('follows an alias to its target', () => { |
| 62 | + const d = decideResolution({ |
| 63 | + hostname: 'profullstack.agentic', |
| 64 | + mode: 'moshpit', |
| 65 | + clearnetResolves: false, |
| 66 | + moshpit: registered('profullstack.agent'), |
| 67 | + }); |
| 68 | + expect(d.resolved).toBe('profullstack.agent'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('still falls through to clearnet for a name Moshpit does not hold', () => { |
| 72 | + const d = decideResolution({ |
| 73 | + hostname: 'example.com', |
| 74 | + mode: 'moshpit', |
| 75 | + clearnetResolves: true, |
| 76 | + moshpit: unregistered, |
| 77 | + }); |
| 78 | + expect(d.use).toBe('clearnet'); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe('decideResolution — a registry outage must not break browsing', () => { |
| 83 | + it.each(['clearnet', 'moshpit'] as const)('falls back to clearnet in %s mode', (mode) => { |
| 84 | + const d = decideResolution({ |
| 85 | + hostname: 'profullstack.ai', |
| 86 | + mode, |
| 87 | + clearnetResolves: true, |
| 88 | + moshpit: null, |
| 89 | + }); |
| 90 | + expect(d.use).toBe('clearnet'); |
| 91 | + expect(d.reason).toMatch(/unreachable|not consulted/i); |
| 92 | + }); |
| 93 | + |
| 94 | + it('always explains itself', () => { |
| 95 | + // Every branch carries a reason, so an override never looks like a glitch. |
| 96 | + for (const mode of ['clearnet', 'moshpit'] as const) { |
| 97 | + for (const clearnetResolves of [true, false]) { |
| 98 | + for (const moshpit of [null, unregistered, registered('x.y')]) { |
| 99 | + const d = decideResolution({ hostname: 'x.y', mode, clearnetResolves, moshpit }); |
| 100 | + expect(d.reason.length).toBeGreaterThan(0); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +describe('parseRegistryName', () => { |
| 108 | + it('accepts exactly one label and one TLD', () => { |
| 109 | + expect(parseRegistryName('fuck.yeah')).toEqual({ label: 'fuck', tld: 'yeah' }); |
| 110 | + expect(parseRegistryName('California.Oranges')).toEqual({ label: 'california', tld: 'oranges' }); |
| 111 | + expect(parseRegistryName('original.sploof.')).toEqual({ label: 'original', tld: 'sploof' }); |
| 112 | + }); |
| 113 | + |
| 114 | + it('rejects anything that is not a registry name', () => { |
| 115 | + // Sending these to the registry would be asking about a name that cannot |
| 116 | + // exist — and acting on the answer would misroute ordinary browsing. |
| 117 | + expect(parseRegistryName('a.b.c')).toBeNull(); |
| 118 | + expect(parseRegistryName('localhost')).toBeNull(); |
| 119 | + expect(parseRegistryName('192.168.1.1')).toBeNull(); |
| 120 | + expect(parseRegistryName('box.example.com:9161')).toBeNull(); |
| 121 | + expect(parseRegistryName('')).toBeNull(); |
| 122 | + expect(parseRegistryName('-bad.yeah')).toBeNull(); |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +describe('lookupMoshpit', () => { |
| 127 | + const okFetch = (body: unknown): typeof fetch => |
| 128 | + vi.fn(async () => ({ ok: true, json: async () => body })) as unknown as typeof fetch; |
| 129 | + |
| 130 | + it('reads the registry answer', async () => { |
| 131 | + const result = await lookupMoshpit('profullstack.agentic', { |
| 132 | + fetchImpl: okFetch({ name: 'profullstack.agentic', resolved: 'profullstack.agent', registered: true }), |
| 133 | + }); |
| 134 | + expect(result).toEqual({ registered: true, resolved: 'profullstack.agent' }); |
| 135 | + }); |
| 136 | + |
| 137 | + it('never asks about a name the registry could not hold', async () => { |
| 138 | + const fetchImpl = okFetch({ registered: true, resolved: 'x' }); |
| 139 | + expect(await lookupMoshpit('a.b.c', { fetchImpl })).toBeNull(); |
| 140 | + expect(fetchImpl).not.toHaveBeenCalled(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('returns null rather than throwing when the registry is down', async () => { |
| 144 | + const dead = vi.fn(async () => { |
| 145 | + throw new Error('ECONNREFUSED'); |
| 146 | + }) as unknown as typeof fetch; |
| 147 | + expect(await lookupMoshpit('fuck.yeah', { fetchImpl: dead })).toBeNull(); |
| 148 | + }); |
| 149 | + |
| 150 | + it('returns null on a nonsense payload', async () => { |
| 151 | + expect(await lookupMoshpit('fuck.yeah', { fetchImpl: okFetch({ nope: 1 }) })).toBeNull(); |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
| 155 | +describe('gatewayUrlFor', () => { |
| 156 | + it('builds a gateway URL and tolerates a trailing slash', () => { |
| 157 | + expect(gatewayUrlFor('fuck.yeah')).toBe('https://pit.moshcode.sh/n/fuck.yeah'); |
| 158 | + expect(gatewayUrlFor('fuck.yeah', 'https://my.pit/')).toBe('https://my.pit/n/fuck.yeah'); |
| 159 | + }); |
| 160 | +}); |
0 commit comments