|
| 1 | +import type { |
| 2 | + DevframeConnection, |
| 3 | +} from './index' |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | +import { setupDevframeConnection } from './index' |
| 6 | + |
| 7 | +const storedConnection: DevframeConnection = { |
| 8 | + connectionMeta: { backend: 'static' }, |
| 9 | + metaBaseUrl: 'http://stored.example/__connection.json', |
| 10 | + authToken: 'stored-token', |
| 11 | +} |
| 12 | +const explicitConnection: DevframeConnection = { |
| 13 | + connectionMeta: { backend: 'static' }, |
| 14 | + metaBaseUrl: 'http://explicit.example/__connection.json', |
| 15 | +} |
| 16 | +const getItem = vi.fn<Storage['getItem']>() |
| 17 | +const setItem = vi.fn<Storage['setItem']>() |
| 18 | +const fetchMetadata = vi.fn<typeof fetch>() |
| 19 | +function readGlobal(name: string): unknown { |
| 20 | + return Reflect.get(globalThis, name) |
| 21 | +} |
| 22 | + |
| 23 | +beforeEach(() => { |
| 24 | + vi.clearAllMocks() |
| 25 | + vi.stubGlobal('window', globalThis) |
| 26 | + vi.stubGlobal('parent', { window: globalThis }) |
| 27 | + vi.stubGlobal('location', new URL('http://viewer.example/index.html')) |
| 28 | + vi.stubGlobal('localStorage', { getItem, setItem }) |
| 29 | + vi.stubGlobal('fetch', fetchMetadata) |
| 30 | + vi.stubGlobal('__DEVFRAME_CONNECTION__', storedConnection) |
| 31 | + vi.stubGlobal('__DEVFRAME_CONNECTION_META__', storedConnection.connectionMeta) |
| 32 | + vi.stubGlobal('__DEVFRAME_CONNECTION_AUTH_TOKEN__', 'stored-token') |
| 33 | + getItem.mockReturnValue('stored-token') |
| 34 | + fetchMetadata.mockResolvedValue(Response.json({})) |
| 35 | +}) |
| 36 | + |
| 37 | +afterEach(() => { |
| 38 | + vi.unstubAllGlobals() |
| 39 | +}) |
| 40 | + |
| 41 | +describe('isolated connection setup', () => { |
| 42 | + it('does not discover or persist shared credentials for an explicit isolated connection', async () => { |
| 43 | + expect.assertions(5) |
| 44 | + const connection = await setupDevframeConnection({ |
| 45 | + connection: explicitConnection, |
| 46 | + isolateConnection: true, |
| 47 | + }) |
| 48 | + expect(connection).toBe(explicitConnection) |
| 49 | + expect(connection.authToken).toBeUndefined() |
| 50 | + expect(getItem).not.toHaveBeenCalled() |
| 51 | + expect(setItem).not.toHaveBeenCalled() |
| 52 | + expect(readGlobal('__DEVFRAME_CONNECTION__')).toBe(storedConnection) |
| 53 | + }) |
| 54 | + |
| 55 | + it('fetches the requested base despite a different cached connection', async () => { |
| 56 | + expect.assertions(6) |
| 57 | + fetchMetadata.mockResolvedValue( |
| 58 | + Response.json({ backend: 'static', authToken: 'metadata-token' }), |
| 59 | + ) |
| 60 | + const connection = await setupDevframeConnection({ |
| 61 | + baseURL: 'http://requested.example/provider/', |
| 62 | + isolateConnection: true, |
| 63 | + }) |
| 64 | + expect(fetchMetadata).toHaveBeenCalledExactlyOnceWith( |
| 65 | + 'http://requested.example/provider/__connection.json', |
| 66 | + ) |
| 67 | + expect(connection.metaBaseUrl).toBe('http://requested.example/provider/__connection.json') |
| 68 | + expect(connection.authToken).toBe('metadata-token') |
| 69 | + expect(getItem).not.toHaveBeenCalled() |
| 70 | + expect(setItem).not.toHaveBeenCalled() |
| 71 | + expect(readGlobal('__DEVFRAME_CONNECTION__')).toBe(storedConnection) |
| 72 | + }) |
| 73 | + |
| 74 | + it('accepts explicit metadata and token without reading or writing shared caches', async () => { |
| 75 | + expect.assertions(6) |
| 76 | + const connection = await setupDevframeConnection({ |
| 77 | + connectionMeta: { backend: 'static', authToken: 'metadata-token' }, |
| 78 | + baseURL: 'http://requested.example/', |
| 79 | + authToken: 'explicit-token', |
| 80 | + isolateConnection: true, |
| 81 | + }) |
| 82 | + expect(connection.authToken).toBe('explicit-token') |
| 83 | + expect(connection.metaBaseUrl).toBe('http://requested.example/__connection.json') |
| 84 | + expect(getItem).not.toHaveBeenCalled() |
| 85 | + expect(setItem).not.toHaveBeenCalled() |
| 86 | + expect(fetchMetadata).not.toHaveBeenCalled() |
| 87 | + expect(readGlobal('__DEVFRAME_CONNECTION_META__')).toBe(storedConnection.connectionMeta) |
| 88 | + }) |
| 89 | + |
| 90 | + it.each([{}, { isolateConnection: false }])( |
| 91 | + 'retains default cache discovery with %j', |
| 92 | + async (options) => { |
| 93 | + expect.assertions(5) |
| 94 | + const connection = await setupDevframeConnection({ |
| 95 | + baseURL: 'http://ignored.example/', |
| 96 | + ...options, |
| 97 | + }) |
| 98 | + expect(connection).toBe(storedConnection) |
| 99 | + expect(getItem).toHaveBeenCalled() |
| 100 | + expect(fetchMetadata).not.toHaveBeenCalled() |
| 101 | + expect(setItem).toHaveBeenCalledExactlyOnceWith( |
| 102 | + '__DEVFRAME_CONNECTION_AUTH_TOKEN__', |
| 103 | + 'stored-token', |
| 104 | + ) |
| 105 | + expect(readGlobal('__DEVFRAME_CONNECTION__')).toEqual(storedConnection) |
| 106 | + }, |
| 107 | + ) |
| 108 | + |
| 109 | + it('ignores accessible-parent caches while retaining fetched metadata resolution', async () => { |
| 110 | + expect.assertions(5) |
| 111 | + vi.stubGlobal('__DEVFRAME_CONNECTION__', undefined) |
| 112 | + vi.stubGlobal('__DEVFRAME_CONNECTION_META__', undefined) |
| 113 | + vi.stubGlobal('__DEVFRAME_CONNECTION_AUTH_TOKEN__', undefined) |
| 114 | + const parentWindow = { |
| 115 | + __DEVFRAME_CONNECTION__: storedConnection, |
| 116 | + __DEVFRAME_CONNECTION_AUTH_TOKEN__: 'parent-token', |
| 117 | + } |
| 118 | + vi.stubGlobal('parent', { window: parentWindow }) |
| 119 | + fetchMetadata.mockResolvedValue(Response.json({ backend: 'static', baseUrl: './nested/__connection.json' })) |
| 120 | + const connection = await setupDevframeConnection({ |
| 121 | + baseURL: 'http://requested.example/', |
| 122 | + isolateConnection: true, |
| 123 | + }) |
| 124 | + expect(connection.metaBaseUrl).toBe('http://requested.example/nested/__connection.json') |
| 125 | + expect(connection.authToken).toBeUndefined() |
| 126 | + expect(getItem).not.toHaveBeenCalled() |
| 127 | + expect(setItem).not.toHaveBeenCalled() |
| 128 | + expect(parentWindow.__DEVFRAME_CONNECTION__).toBe(storedConnection) |
| 129 | + }) |
| 130 | +}) |
0 commit comments