Repository navigation
Fix/fill custom algo form #131
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
filiperochalopes
wants to merge
2
commits into
OpenConceptLab:main
Choose a base branch
from
filiperochalopes:fix/fill-custom-algo-form
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.
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
73 changes: 73 additions & 0 deletions
73
src/components/map-projects/__tests__/customAlgoMetadata.test.js
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,73 @@ | ||
| import test from 'node:test' | ||
| import assert from 'node:assert/strict' | ||
| import { createServer } from 'node:http' | ||
| import { metadataFields, fetchAlgorithmMetadata } from '../customAlgoMetadata.js' | ||
|
|
||
| test('metadata supplies the canonical URL alongside the other form fields', () => { | ||
| assert.deepEqual(metadataFields({ ID: 'agent', name: 'Agent', canonical_url: 'http://example.test/system' }), { | ||
| id: 'agent', name: 'Agent', description: '', canonical_url: 'http://example.test/system' | ||
| }) | ||
| }) | ||
|
|
||
| test('missing or invalid canonical URL leaves the existing field untouched', () => { | ||
| for (const canonical_url of [undefined, '', 'invalid', null]) { | ||
| assert.deepEqual(metadataFields({ id: 'agent', name: 'Agent', canonical_url }), { | ||
| id: 'agent', name: 'Agent', description: '' | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| test('metadata GET tries the operation without dollar first, then the original on failure', async () => { | ||
| const urls = [] | ||
| const result = await fetchAlgorithmMetadata('https://example.test/api/$match?x=$keep', async url => { | ||
| urls.push(url) | ||
| return urls.length === 1 ? {response: {status: 404}} : {data: {ID: 'x', name: 'X'}} | ||
| }) | ||
| assert.deepEqual(urls, ['https://example.test/api/match?x=$keep', 'https://example.test/api/$match?x=$keep']) | ||
| assert.equal(result.data.ID, 'x') | ||
| }) | ||
|
|
||
| test('metadata GET avoids a retry on success or when the URL has no operation marker', async () => { | ||
| for (const url of ['https://example.test/api/$match/', 'https://example.test/api/match']) { | ||
| const calls = [] | ||
| await fetchAlgorithmMetadata(url, async candidate => { calls.push(candidate); return {data: {id: 'x', name: 'X'}} }) | ||
| assert.equal(calls.length, 1) | ||
| assert.equal(calls[0], 'https://example.test/api/match' + (url.endsWith('/') ? '/' : '')) | ||
| } | ||
| }) | ||
|
|
||
| test('metadata GET retries when the first response is not usable metadata', async () => { | ||
| const calls = [] | ||
| const result = await fetchAlgorithmMetadata('https://example.test/$match', async url => { | ||
| calls.push(url) | ||
| return calls.length === 1 ? {data: {message: 'not metadata'}} : {data: {id: 'x', name: 'X'}} | ||
| }) | ||
| assert.equal(calls.length, 2) | ||
| assert.equal(result.data.id, 'x') | ||
| }) | ||
|
|
||
| test('metadata GET retains an authentication failure if the alternate is missing', async () => { | ||
| const result = await fetchAlgorithmMetadata('https://example.test/$match', async url => ( | ||
| {response: {status: url.includes('$') ? 404 : 401}} | ||
| )) | ||
| assert.equal(result.response.status, 401) | ||
| }) | ||
|
|
||
| test('metadata fallback performs real GET requests against a local server', async t => { | ||
| const requests = [] | ||
| const server = createServer((req, res) => { | ||
| requests.push([req.method, req.url, req.headers.authorization]) | ||
| res.setHeader('Content-Type', 'application/json') | ||
| if(req.url === '/$match') res.end(JSON.stringify({ID: 'x', name: 'X', canonical_url: 'https://example.test/system'})) | ||
| else { res.statusCode = 404; res.end('{}') } | ||
| }) | ||
| await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)) | ||
| t.after(() => new Promise(resolve => server.close(resolve))) | ||
| const url = `http://127.0.0.1:${server.address().port}/$match` | ||
| const response = await fetchAlgorithmMetadata(url, async candidate => { | ||
| const result = await fetch(candidate, {headers: {Authorization: 'Token test'}}) | ||
| return result.ok ? {data: await result.json()} : {response: {status: result.status}} | ||
| }) | ||
| assert.deepEqual(requests, [['GET', '/match', 'Token test'], ['GET', '/$match', 'Token test']]) | ||
| assert.equal(metadataFields(response.data).canonical_url, 'https://example.test/system') | ||
| }) |
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,27 @@ | ||
| // Match the form's URL validation when accepting metadata from a custom API. | ||
| export const isLikelyCanonicalUrl = value => { | ||
| if(!value || typeof value !== 'string') return false | ||
| return /^https?:\/\/[^\s]+$/i.test(value.trim()) | ||
| } | ||
|
|
||
| // Omit an absent or invalid canonical so an existing form value is preserved. | ||
| export const metadataFields = data => ({ | ||
| id: data.ID || data.id || '', | ||
| name: data.name || '', | ||
| description: data.description || '', | ||
| ...(isLikelyCanonicalUrl(data.canonical_url) ? { canonical_url: data.canonical_url.trim() } : {}), | ||
| }) | ||
|
|
||
| // Try the non-operation metadata path before the original POST endpoint. | ||
| export const fetchAlgorithmMetadata = async (url, get) => { | ||
| const [path] = url.split(/[?#]/, 1) | ||
| const metadataUrl = path.replace(/\/\$([^/]+)(\/?)$/, '/$1$2') + url.slice(path.length) | ||
| const first = await get(metadataUrl) | ||
| const data = first?.data || first | ||
| if(metadataUrl === url || (!first?.response && data?.name && (data?.ID || data?.id))) return first | ||
| const second = await get(url) | ||
| const secondData = second?.data || second | ||
| if(!second?.response && secondData?.name && (secondData?.ID || secondData?.id)) return second | ||
| const firstStatus = first?.response?.status || first?.status | ||
| return [401, 403].includes(firstStatus) ? first : second | ||
| } |
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.
If two configured custom endpoints report the same ID, name and description but different valid canonical URLs, including
canonical_urlin this patch preventsupdateSelected'sfilter(value, patch)from matching the first row. The second row therefore receives no conflict error, whereas the previous three-field patch detected the duplicate. This matters becauseMapProject.getAlgoDefselects the first configuration by ID andmergeIntoRowMatchStateremoves existing candidates with the incomingalgorithm_id, so the endpoints can overwrite each other's results. Validate ID/name uniqueness independently of the full metadata patch.Useful? React with 👍 / 👎.