|
| 1 | +import { |
| 2 | + type RemoteManifestShape, |
| 3 | + createManifestFallbackAssetUrl, |
| 4 | + getRequestedAssetDirectory, |
| 5 | + isExposeAssetRequestPath, |
| 6 | + resolveManifestFallbackAssetPath, |
| 7 | +} from '../shared/manifestFallback'; |
| 8 | + |
| 9 | +describe('manifest fallback shared helpers', () => { |
| 10 | + it('identifies expose asset request paths', () => { |
| 11 | + expect( |
| 12 | + isExposeAssetRequestPath( |
| 13 | + '/static/js/async/__federation_expose_RemoteClientCounter.js', |
| 14 | + ), |
| 15 | + ).toBe(true); |
| 16 | + expect( |
| 17 | + isExposeAssetRequestPath( |
| 18 | + '/static/css/async/__federation_expose_RemoteClientCounter.css', |
| 19 | + ), |
| 20 | + ).toBe(true); |
| 21 | + expect(isExposeAssetRequestPath('/static/js/async/743.32436c1247.js')).toBe( |
| 22 | + false, |
| 23 | + ); |
| 24 | + }); |
| 25 | + |
| 26 | + it('derives requested async asset directory from pathname', () => { |
| 27 | + expect( |
| 28 | + getRequestedAssetDirectory( |
| 29 | + '/static/js/async/__federation_expose_RemoteClientCounter.js', |
| 30 | + ), |
| 31 | + ).toBe('static/js/async/'); |
| 32 | + expect( |
| 33 | + getRequestedAssetDirectory( |
| 34 | + '/static/css/async/__federation_expose_RemoteClientCounter.css', |
| 35 | + ), |
| 36 | + ).toBe('static/css/async/'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('resolves fallback assets from shared and exposes manifest entries', () => { |
| 40 | + const manifest: RemoteManifestShape = { |
| 41 | + shared: [ |
| 42 | + { |
| 43 | + assets: { |
| 44 | + js: { |
| 45 | + sync: [ |
| 46 | + 'static/js/async/__federation_expose_actions.44d8f1d7ae.js', |
| 47 | + ], |
| 48 | + async: [], |
| 49 | + }, |
| 50 | + css: { |
| 51 | + sync: [], |
| 52 | + async: [], |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + ], |
| 57 | + exposes: [ |
| 58 | + { |
| 59 | + assets: { |
| 60 | + js: { |
| 61 | + sync: [ |
| 62 | + 'static/js/async/__federation_expose_RemoteClientCounter.7745fe5f0a.js', |
| 63 | + ], |
| 64 | + async: [], |
| 65 | + }, |
| 66 | + css: { |
| 67 | + sync: [], |
| 68 | + async: [], |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + ], |
| 73 | + }; |
| 74 | + |
| 75 | + expect( |
| 76 | + resolveManifestFallbackAssetPath( |
| 77 | + '/static/js/async/__federation_expose_RemoteClientCounter.js', |
| 78 | + manifest, |
| 79 | + ), |
| 80 | + ).toBe( |
| 81 | + 'static/js/async/__federation_expose_RemoteClientCounter.7745fe5f0a.js', |
| 82 | + ); |
| 83 | + expect( |
| 84 | + resolveManifestFallbackAssetPath( |
| 85 | + '/static/js/async/__federation_expose_actions.js', |
| 86 | + manifest, |
| 87 | + ), |
| 88 | + ).toBe('static/js/async/__federation_expose_actions.44d8f1d7ae.js'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('supports absolute manifest assets and rejects non-matching paths', () => { |
| 92 | + const manifest: RemoteManifestShape = { |
| 93 | + shared: [ |
| 94 | + { |
| 95 | + assets: { |
| 96 | + js: { |
| 97 | + sync: [ |
| 98 | + 'http://127.0.0.1:3999/static/js/async/__federation_expose_nestedActions.a8ce95b11a.js', |
| 99 | + ], |
| 100 | + async: [], |
| 101 | + }, |
| 102 | + css: { |
| 103 | + sync: [], |
| 104 | + async: [], |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + ], |
| 109 | + }; |
| 110 | + |
| 111 | + expect( |
| 112 | + resolveManifestFallbackAssetPath( |
| 113 | + '/static/js/async/__federation_expose_nestedActions.js', |
| 114 | + manifest, |
| 115 | + ), |
| 116 | + ).toBe( |
| 117 | + 'http://127.0.0.1:3999/static/js/async/__federation_expose_nestedActions.a8ce95b11a.js', |
| 118 | + ); |
| 119 | + expect( |
| 120 | + resolveManifestFallbackAssetPath( |
| 121 | + '/static/js/async/not-an-expose.js', |
| 122 | + manifest, |
| 123 | + ), |
| 124 | + ).toBeUndefined(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('builds safe fallback URL and merges request query params', () => { |
| 128 | + expect( |
| 129 | + createManifestFallbackAssetUrl({ |
| 130 | + remoteOrigin: 'http://127.0.0.1:3999', |
| 131 | + fallbackAssetPath: |
| 132 | + 'http://127.0.0.1:3999/static/js/async/__federation_expose_RemoteClientCounter.7745fe5f0a.js?manifest=1', |
| 133 | + requestSearch: '?cache=1', |
| 134 | + requestedAssetDirectory: 'static/js/async/', |
| 135 | + }), |
| 136 | + ).toBe( |
| 137 | + 'http://127.0.0.1:3999/static/js/async/__federation_expose_RemoteClientCounter.7745fe5f0a.js?manifest=1&cache=1', |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + it('rejects unsafe fallback URLs', () => { |
| 142 | + expect( |
| 143 | + createManifestFallbackAssetUrl({ |
| 144 | + remoteOrigin: 'http://127.0.0.1:3999', |
| 145 | + fallbackAssetPath: |
| 146 | + 'https://cdn.example.com/static/js/async/__federation_expose_RemoteClientCounter.7745fe5f0a.js', |
| 147 | + requestSearch: '', |
| 148 | + requestedAssetDirectory: 'static/js/async/', |
| 149 | + }), |
| 150 | + ).toBeUndefined(); |
| 151 | + expect( |
| 152 | + createManifestFallbackAssetUrl({ |
| 153 | + remoteOrigin: 'http://127.0.0.1:3999', |
| 154 | + fallbackAssetPath: |
| 155 | + 'static/js/async/../__federation_expose_RemoteClientCounter.7745fe5f0a.js', |
| 156 | + requestSearch: '', |
| 157 | + requestedAssetDirectory: 'static/js/async/', |
| 158 | + }), |
| 159 | + ).toBeUndefined(); |
| 160 | + expect( |
| 161 | + createManifestFallbackAssetUrl({ |
| 162 | + remoteOrigin: 'http://127.0.0.1:3999', |
| 163 | + fallbackAssetPath: |
| 164 | + 'static/js/async/__federation_expose_RemoteClientCounter.js', |
| 165 | + requestSearch: '', |
| 166 | + requestedAssetDirectory: 'static/js/async/', |
| 167 | + requestUrl: |
| 168 | + 'http://127.0.0.1:3999/static/js/async/__federation_expose_RemoteClientCounter.js', |
| 169 | + }), |
| 170 | + ).toBeUndefined(); |
| 171 | + }); |
| 172 | +}); |
0 commit comments