Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/components/map-projects/MultiAlgoSelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import orderBy from 'lodash/orderBy'
import ConceptIcon from '../concepts/ConceptIcon'
import { OperationsContext } from '../app/LayoutContext'
import { isLikelyCanonicalUrl } from './algorithms'
import { metadataFields, fetchAlgorithmMetadata } from './customAlgoMetadata.js'
import RepoVersionSearchAutocomplete from '../repos/RepoVersionSearchAutocomplete'
import APIService from '../../services/APIService';
import { needsSessionToken, getTokenFlag, tokenInputValue, removeSavedToken } from '../../services/tokenScope';
Expand Down Expand Up @@ -353,9 +354,10 @@ export default function MultiAlgoSelector({
setCustomAlgoState(key, {isLoading: true, requestError: '', requestSuccess: '', tokenRequired: false});

const service = APIService.new();
service.URL = selected.url;

const response = await service.get(token || false, {}, undefined, true);
const response = await fetchAlgorithmMetadata(selected.url, url => {
service.URL = url;
return service.get(token || false, {}, undefined, true);
});
const status = response?.response?.status || response?.status;

if([401, 403].includes(status)) {
Expand Down Expand Up @@ -389,11 +391,7 @@ export default function MultiAlgoSelector({
return;
}

updateSelected(key, {
id: data.ID || data.id || '',
name: data.name || '',
description: data.description || '',
});
updateSelected(key, metadataFields(data));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve duplicate-ID detection when adding canonical metadata

If two configured custom endpoints report the same ID, name and description but different valid canonical URLs, including canonical_url in this patch prevents updateSelected's filter(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 because MapProject.getAlgoDef selects the first configuration by ID and mergeIntoRowMatchState removes existing candidates with the incoming algorithm_id, so the endpoints can overwrite each other's results. Validate ID/name uniqueness independently of the full metadata patch.

Useful? React with 👍 / 👎.

setCustomAlgoState(key, {
isLoading: false,
tokenRequired: false,
Expand Down
73 changes: 73 additions & 0 deletions src/components/map-projects/__tests__/customAlgoMetadata.test.js
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')
})
9 changes: 2 additions & 7 deletions src/components/map-projects/algorithms.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import MatchingIcon from '@mui/icons-material/DeviceHub';
import { isLikelyCanonicalUrl } from './customAlgoMetadata.js'

// Canonical concept-identity config per algorithm type
// (plans/unified-mapper-model.md). Single source of truth shared between
Expand Down Expand Up @@ -47,13 +48,7 @@ export const CONCEPT_IDENTITY_BY_TYPE = {
}
}

// Cheap structural validator for canonical URLs. The unified mapper model
// requires every algorithm's concept_identity.canonical_url to be a URL —
// custom algos rely on user input for this, so the form validates here.
export const isLikelyCanonicalUrl = (value) => {
if(!value || typeof value !== 'string') return false
return /^https?:\/\/[^\s]+$/i.test(value.trim())
}
export { isLikelyCanonicalUrl }

// Walk algosSelected and return the subset that fails project-config
// validation. Currently: custom algos must have a valid canonical_url.
Expand Down
27 changes: 27 additions & 0 deletions src/components/map-projects/customAlgoMetadata.js
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
}
Loading