refactor shared source URL detection

This commit is contained in:
Dariusz L
2026-08-07 04:12:57 +02:00
parent c168157727
commit 8a8e2bc3a1
5 changed files with 104 additions and 55 deletions
+1 -6
View File
@@ -8,7 +8,7 @@ import {
normalizePathIdentity,
safeStorage,
} from '../web/resolver/utils/html_utils.js';
import { getModelCardUrl } from '../web/resolver/utils/url_utils.js';
import { getModelCardUrl, getSourceKeyFromUrl } from '../web/resolver/utils/url_utils.js';
import { joinPathPreservingStyle } from '../web/resolver/utils/path_utils.js';
import {
classifyLocalMatches,
@@ -4758,11 +4758,6 @@ test('source link prefers the selected provider page over another provider mirro
civarchivePage
];
},
getContextMenuSourceKeyFromUrl(value = '') {
if (value.includes('civarchive.com')) return 'civarchive';
if (value.includes('huggingface.co')) return 'huggingface';
return '';
},
normalizeContextMenuSourceUrl(value = '') {
return value;
},
+36 -1
View File
@@ -1,7 +1,42 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { getModelCardUrl, parseHuggingFaceFileUrl } from '../web/resolver/utils/url_utils.js';
import {
getModelCardUrl,
getSourceKeyFromUrl,
parseHuggingFaceFileUrl,
} from '../web/resolver/utils/url_utils.js';
import { getSourceKeyFromText } from '../web/resolver/utils/source_labels.js';
test('source URL parsing normalizes supported provider hosts', () => {
const cases = [
['https://huggingface.co/owner/repo', 'huggingface'],
['https://www.huggingface.co/owner/repo', 'huggingface'],
['https://cdn.civarchive.com/models/1', 'civarchive'],
['https://civitai.com/models/1', 'civitai'],
['https://www.civitai.red/models/1', 'civitai'],
['https://unknown.example/models/1', ''],
['not a URL', ''],
];
for (const [value, expected] of cases) {
assert.equal(getSourceKeyFromUrl(value), expected, value);
}
});
test('source URL parsing can preserve text fallback semantics', () => {
assert.equal(getSourceKeyFromUrl('CivArchive mirror', { fallbackToText: true }), 'civarchive');
assert.equal(getSourceKeyFromUrl('LoRA Archive result', { fallbackToText: true }), 'lora_manager_archive');
assert.equal(getSourceKeyFromUrl('unknown.example/model', { fallbackToText: true }), '');
});
test('source text parsing normalizes provider aliases', () => {
assert.equal(getSourceKeyFromText('Hugging Face result'), 'huggingface');
assert.equal(getSourceKeyFromText('Civ Archive result'), 'civarchive');
assert.equal(getSourceKeyFromText('CivitAI result'), 'civitai');
assert.equal(getSourceKeyFromText('LoRA Archive result'), 'lora_manager_archive');
assert.equal(getSourceKeyFromText('unknown'), '');
});
test('Hugging Face file URLs expose repository, revision, path, and filename', () => {
assert.deepEqual(