-
Notifications
You must be signed in to change notification settings - Fork 37
Make loc aware of msm #606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ravuthu
wants to merge
3
commits into
main
Choose a base branch
from
loc-msm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+283
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { DA_ADMIN } from '../../../../nx2/utils/utils.js'; | ||
| import { daFetch } from '../../../../nx2/utils/api.js'; | ||
|
|
||
| const sourceOf = (row) => row.base || row.source; | ||
| const linkedOf = (row) => row.satellite || row.linked; | ||
|
|
||
| const msmRowsCache = {}; | ||
|
|
||
| export function fetchMsmRows(org) { | ||
| msmRowsCache[org] ??= (async () => { | ||
| try { | ||
| const resp = await daFetch({ url: `${DA_ADMIN}/config/${org}/` }); | ||
| if (resp.status === 404) return []; | ||
| if (!resp.ok) throw new Error(`config ${resp.status}`); | ||
| const json = await resp.json(); | ||
| return json?.msm?.data || []; | ||
| } catch { | ||
| delete msmRowsCache[org]; | ||
| return []; | ||
| } | ||
| })(); | ||
| return msmRowsCache[org]; | ||
| } | ||
|
|
||
| export function getSourceChain(rows, site) { | ||
| const chain = []; | ||
| const visited = new Set(); | ||
| let current = site; | ||
| while (current && !visited.has(current)) { | ||
| visited.add(current); | ||
| const linkedSite = current; | ||
| const parentRow = rows.find((row) => linkedOf(row) === linkedSite); | ||
| const parent = parentRow && sourceOf(parentRow); | ||
| if (!parent) break; | ||
| chain.push(parent); | ||
| current = parent; | ||
| } | ||
| return chain; | ||
| } | ||
|
|
||
| export async function fetchWithMsmFallback({ org, site, daPath, opts }) { | ||
| const fetchFrom = (fromSite) => daFetch({ | ||
| url: `${DA_ADMIN}/source/${org}/${fromSite}${daPath}`, | ||
| opts, | ||
| }); | ||
|
|
||
| const selfResp = await fetchFrom(site); | ||
| if (selfResp.ok) return { resp: selfResp, resolvedSite: site, inherited: false }; | ||
|
|
||
| if (selfResp.status !== 404) return { resp: selfResp, resolvedSite: site, inherited: false }; | ||
|
|
||
| const rows = await fetchMsmRows(org); | ||
| if (!rows.length) return { resp: selfResp, resolvedSite: site, inherited: false }; | ||
|
|
||
| const chain = getSourceChain(rows, site); | ||
| for (const ancestor of chain) { | ||
| // eslint-disable-next-line no-await-in-loop | ||
| const resp = await fetchFrom(ancestor); | ||
| if (resp.ok) return { resp, resolvedSite: ancestor, inherited: true }; | ||
| } | ||
|
|
||
| return { resp: selfResp, resolvedSite: site, inherited: false }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| import { expect } from '@esm-bundle/chai'; | ||
| import sinon from 'sinon'; | ||
| import { DA_ADMIN } from '../../nx2/utils/utils.js'; | ||
| import { fetchMsmRows, getSourceChain, fetchWithMsmFallback } from '../../nx/blocks/loc/utils/msm.js'; | ||
|
|
||
| const DA_PATH = '/en/about.html'; | ||
|
|
||
| function makeResp(status, body = '') { | ||
| return { | ||
| ok: status >= 200 && status < 300, | ||
| status, | ||
| headers: new Headers(), | ||
| json: async () => body, | ||
| text: async () => (typeof body === 'string' ? body : JSON.stringify(body)), | ||
| }; | ||
| } | ||
|
|
||
| function stubFetch({ org, config, sources = {} }) { | ||
| globalThis.fetch = sinon.stub().callsFake((url) => { | ||
| if (url.includes('/config/')) { | ||
| if (typeof config === 'number') return Promise.resolve(makeResp(config)); | ||
| return Promise.resolve(makeResp(200, { msm: { data: config || [] } })); | ||
| } | ||
| const site = Object.keys(sources).find((s) => url.includes(`/source/${org}/${s}/`)); | ||
| const status = site ? sources[site] : 404; | ||
| return Promise.resolve(makeResp(status, status === 200 ? '<main>content</main>' : '')); | ||
| }); | ||
| } | ||
|
|
||
| const configCalls = () => globalThis.fetch.args.filter(([url]) => url.includes('/config/')); | ||
|
|
||
| describe('msm', () => { | ||
| let originalFetch; | ||
|
|
||
| beforeEach(() => { | ||
| originalFetch = globalThis.fetch; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| globalThis.fetch = originalFetch; | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| describe('getSourceChain', () => { | ||
| it('returns an empty chain when the linked site has no source', () => { | ||
| const rows = [{ source: 'source-site', linked: 'linked-site' }]; | ||
| expect(getSourceChain(rows, 'unrelated-site')).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('resolves the source of a linked site', () => { | ||
| const rows = [{ source: 'source-site', linked: 'linked-site' }]; | ||
| expect(getSourceChain(rows, 'linked-site')).to.deep.equal(['source-site']); | ||
| }); | ||
|
|
||
| it('walks a multi-level chain nearest-source-first', () => { | ||
| const rows = [ | ||
| { source: 'source-site', linked: 'mid-site' }, | ||
| { source: 'mid-site', linked: 'linked-site' }, | ||
| ]; | ||
| expect(getSourceChain(rows, 'linked-site')).to.deep.equal(['mid-site', 'source-site']); | ||
| }); | ||
|
|
||
| it('guards against cycles without looping forever', () => { | ||
| const rows = [ | ||
| { source: 'site-a', linked: 'site-b' }, | ||
| { source: 'site-b', linked: 'site-a' }, | ||
| ]; | ||
| expect(getSourceChain(rows, 'site-a')).to.deep.equal(['site-b', 'site-a']); | ||
| }); | ||
|
|
||
| it('supports the legacy base/satellite column names', () => { | ||
| const rows = [{ base: 'source-site', satellite: 'linked-site' }]; | ||
| expect(getSourceChain(rows, 'linked-site')).to.deep.equal(['source-site']); | ||
| }); | ||
|
|
||
| it('falls back to the alternate column when a cell is blank', () => { | ||
| const rows = [{ base: '', source: 'source-site', satellite: '', linked: 'linked-site' }]; | ||
| expect(getSourceChain(rows, 'linked-site')).to.deep.equal(['source-site']); | ||
| }); | ||
| }); | ||
|
|
||
| describe('fetchMsmRows', () => { | ||
| it('returns the msm sheet rows on a successful config fetch', async () => { | ||
| const rows = [{ source: 'source-site', linked: 'linked-site' }]; | ||
| stubFetch({ org: 'rows-ok', config: rows }); | ||
| expect(await fetchMsmRows('rows-ok')).to.deep.equal(rows); | ||
| }); | ||
|
|
||
| it('returns [] when the config has no msm sheet', async () => { | ||
| globalThis.fetch = sinon.stub().resolves(makeResp(200, { other: { data: [] } })); | ||
| expect(await fetchMsmRows('rows-nomsm')).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('memoizes a successful lookup (one config fetch for repeat calls)', async () => { | ||
| stubFetch({ org: 'rows-memo', config: [{ source: 'source-site', linked: 'linked-site' }] }); | ||
| await fetchMsmRows('rows-memo'); | ||
| await fetchMsmRows('rows-memo'); | ||
| expect(configCalls().length).to.equal(1); | ||
| }); | ||
|
|
||
| it('caches an absent config (404) so non-MSM orgs are not re-probed', async () => { | ||
| stubFetch({ org: 'rows-404', config: 404 }); | ||
| expect(await fetchMsmRows('rows-404')).to.deep.equal([]); | ||
| await fetchMsmRows('rows-404'); | ||
| expect(configCalls().length).to.equal(1); | ||
| }); | ||
|
|
||
| it('does not cache a transient failure (500) so a later call retries', async () => { | ||
| stubFetch({ org: 'rows-500', config: 500 }); | ||
| expect(await fetchMsmRows('rows-500')).to.deep.equal([]); | ||
| await fetchMsmRows('rows-500'); | ||
| expect(configCalls().length).to.equal(2); | ||
| }); | ||
| }); | ||
|
|
||
| describe('fetchWithMsmFallback', () => { | ||
| it('returns the linked-site response without consulting MSM when the file exists', async () => { | ||
| stubFetch({ org: 'self-ok', sources: { 'linked-site': 200 } }); | ||
| const result = await fetchWithMsmFallback({ org: 'self-ok', site: 'linked-site', daPath: DA_PATH }); | ||
| expect(result.resp.ok).to.equal(true); | ||
| expect(result.resolvedSite).to.equal('linked-site'); | ||
| expect(result.inherited).to.equal(false); | ||
| expect(configCalls().length).to.equal(0); | ||
| }); | ||
|
|
||
| it('resolves an inherited page from its source site', async () => { | ||
| stubFetch({ | ||
| org: 'inherit', | ||
| config: [{ source: 'source-site', linked: 'linked-site' }], | ||
| sources: { 'source-site': 200 }, | ||
| }); | ||
| const result = await fetchWithMsmFallback({ org: 'inherit', site: 'linked-site', daPath: DA_PATH }); | ||
| expect(result.resp.ok).to.equal(true); | ||
| expect(result.resolvedSite).to.equal('source-site'); | ||
| expect(result.inherited).to.equal(true); | ||
| }); | ||
|
|
||
| it('walks multiple source sites until one holds the file', async () => { | ||
| stubFetch({ | ||
| org: 'multi', | ||
| config: [ | ||
| { source: 'source-site', linked: 'mid-site' }, | ||
| { source: 'mid-site', linked: 'linked-site' }, | ||
| ], | ||
| sources: { 'source-site': 200 }, | ||
| }); | ||
| const result = await fetchWithMsmFallback({ org: 'multi', site: 'linked-site', daPath: DA_PATH }); | ||
| expect(result.resolvedSite).to.equal('source-site'); | ||
| expect(result.inherited).to.equal(true); | ||
| }); | ||
|
|
||
| it('returns the linked-site 404 when there is no MSM config', async () => { | ||
| stubFetch({ org: 'no-config', config: 404 }); | ||
| const result = await fetchWithMsmFallback({ org: 'no-config', site: 'linked-site', daPath: DA_PATH }); | ||
| expect(result.resp.status).to.equal(404); | ||
| expect(result.resolvedSite).to.equal('linked-site'); | ||
| expect(result.inherited).to.equal(false); | ||
| }); | ||
|
|
||
| it('returns the linked-site 404 when no source site holds the file', async () => { | ||
| stubFetch({ org: 'none-have', config: [{ source: 'source-site', linked: 'linked-site' }] }); | ||
| const result = await fetchWithMsmFallback({ org: 'none-have', site: 'linked-site', daPath: DA_PATH }); | ||
| expect(result.resp.status).to.equal(404); | ||
| expect(result.resolvedSite).to.equal('linked-site'); | ||
| expect(result.inherited).to.equal(false); | ||
| }); | ||
|
|
||
| it('does not fall back on a non-404 error and surfaces the real status', async () => { | ||
| stubFetch({ | ||
| org: 'forbidden', | ||
| config: [{ source: 'source-site', linked: 'linked-site' }], | ||
| sources: { 'linked-site': 403, 'source-site': 200 }, | ||
| }); | ||
| const result = await fetchWithMsmFallback({ org: 'forbidden', site: 'linked-site', daPath: DA_PATH }); | ||
| expect(result.resp.status).to.equal(403); | ||
| expect(result.resolvedSite).to.equal('linked-site'); | ||
| expect(result.inherited).to.equal(false); | ||
| // A permission/server error must not trigger a source-site read. | ||
| expect(configCalls().length).to.equal(0); | ||
| }); | ||
|
|
||
| it('requests the linked site then the source site at the expected DA path', async () => { | ||
| stubFetch({ | ||
| org: 'url-check', | ||
| config: [{ source: 'source-site', linked: 'linked-site' }], | ||
| sources: { 'source-site': 200 }, | ||
| }); | ||
| await fetchWithMsmFallback({ org: 'url-check', site: 'linked-site', daPath: DA_PATH }); | ||
| const requested = globalThis.fetch.args.map(([url]) => url); | ||
| expect(requested).to.include(`${DA_ADMIN}/source/url-check/linked-site${DA_PATH}`); | ||
| expect(requested).to.include(`${DA_ADMIN}/source/url-check/source-site${DA_PATH}`); | ||
| }); | ||
|
|
||
| it('forwards fetch opts (headers) to the source request', async () => { | ||
| stubFetch({ org: 'opts', sources: { 'linked-site': 200 } }); | ||
| const opts = { headers: { 'Cache-Control': 'no-cache' } }; | ||
| await fetchWithMsmFallback({ org: 'opts', site: 'linked-site', daPath: DA_PATH, opts }); | ||
| const sourceCall = globalThis.fetch.args.find(([url]) => url.includes('/source/opts/linked-site/')); | ||
| expect(sourceCall[1].headers['Cache-Control']).to.equal('no-cache'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please hoist const selfResult = { resp: selfResp, resolvedSite: site, inherited: false } once and return it in the ok / non-404 / no-rows / fallthrough paths. four copies is easy to let drift.