Files
Will Miao 0b46852f91 feat(search): search LoRAs by trigger word
Users see a trigger word in someone else's prompt and have no way to tell
whether the LoRA is already in their library, so they fall back to searching
the model sites again.

- Read trigger words from civitai.trainedWords through a small helper that
  tolerates the shapes older sidecars contain (None, a bare string, mixed
  lists). Provenance does not matter: CivitAI/CivArchive sync, HuggingFace
  or ModelScope model cards, the AI enrichment skill and manual edits all
  write the same field.
- Add an opt-in trigger_words search option to SearchStrategy, evaluated
  last before the always-on hash fallback. Matching is case-insensitive
  substring only: fuzzy would run the sequence matcher over every declared
  word of every entry and cost seconds on large libraries.
- Expose it as a "Trigger Words" chip on the LoRAs page only, plumb the
  option through page state and the list request, and parse the new
  search_trigger_words query param.
- Cover the strategy (including AI-enriched HuggingFace entries and
  malformed trainedWords), the param parsing, the chip-to-state wiring and
  the request query string. The English key is synced with a
  [TODO: Translate] placeholder in the other locales.
2026-11-05 20:24:18 +08:00

107 lines
3.3 KiB
JavaScript

import { describe, it, expect, vi } from 'vitest';
const {
BASE_MODEL_API_MODULE,
STATE_MODULE,
UI_HELPERS_MODULE,
I18N_MODULE,
STORAGE_MODULE,
API_CONFIG_MODULE,
API_FACTORY_MODULE,
SIDEBAR_MANAGER_MODULE,
} = vi.hoisted(() => ({
BASE_MODEL_API_MODULE: new URL('../../../static/js/api/baseModelApi.js', import.meta.url).pathname,
STATE_MODULE: new URL('../../../static/js/state/index.js', import.meta.url).pathname,
UI_HELPERS_MODULE: new URL('../../../static/js/utils/uiHelpers.js', import.meta.url).pathname,
I18N_MODULE: new URL('../../../static/js/utils/i18nHelpers.js', import.meta.url).pathname,
STORAGE_MODULE: new URL('../../../static/js/utils/storageHelpers.js', import.meta.url).pathname,
API_CONFIG_MODULE: new URL('../../../static/js/api/apiConfig.js', import.meta.url).pathname,
API_FACTORY_MODULE: new URL('../../../static/js/api/modelApiFactory.js', import.meta.url).pathname,
SIDEBAR_MANAGER_MODULE: new URL('../../../static/js/components/SidebarManager.js', import.meta.url).pathname,
}));
vi.mock(STATE_MODULE, () => ({
state: { currentPageType: 'loras', global: { settings: { group_by_model: false } } },
getCurrentPageState: vi.fn(() => ({})),
}));
vi.mock(UI_HELPERS_MODULE, () => ({
showToast: vi.fn(),
}));
vi.mock(I18N_MODULE, () => ({
translate: vi.fn((key) => key),
}));
vi.mock(STORAGE_MODULE, () => ({
getStorageItem: vi.fn(),
getSessionItem: vi.fn(() => null),
removeSessionItem: vi.fn(),
saveMapToStorage: vi.fn(),
}));
vi.mock(API_CONFIG_MODULE, () => ({
getCompleteApiConfig: vi.fn(() => ({
endpoints: {},
config: { displayName: 'LoRA', singularName: 'LoRA' },
})),
getCurrentModelType: vi.fn(() => 'loras'),
isValidModelType: vi.fn(() => true),
DOWNLOAD_ENDPOINTS: {},
HF_ENDPOINTS: {},
MODEL_SOURCE_ENDPOINTS: {},
WS_ENDPOINTS: {},
}));
vi.mock(API_FACTORY_MODULE, () => ({
resetAndReload: vi.fn(),
}));
vi.mock(SIDEBAR_MANAGER_MODULE, () => ({
sidebarManager: { refresh: vi.fn() },
}));
function makePageState(searchOptions) {
return {
filters: { search: 'anime', tags: {} },
searchOptions: {
filename: true,
modelname: true,
tags: false,
creator: false,
recursive: true,
...searchOptions,
},
};
}
describe('BaseModelApiClient trigger-word search params', () => {
async function createClient() {
const { BaseModelApiClient } = await import(BASE_MODEL_API_MODULE);
class TestClient extends BaseModelApiClient {}
return new TestClient('loras');
}
it('sends search_trigger_words when the trigger-word option is enabled', async () => {
const client = await createClient();
const params = client._buildQueryParams({}, makePageState({ triggerWords: true }));
expect(params.get('search')).toBe('anime');
expect(params.get('search_trigger_words')).toBe('true');
});
it('sends search_trigger_words=false when the option is disabled', async () => {
const client = await createClient();
const params = client._buildQueryParams({}, makePageState({ triggerWords: false }));
expect(params.get('search_trigger_words')).toBe('false');
});
it('omits the param on pages without the trigger-word option', async () => {
const client = await createClient();
const params = client._buildQueryParams({}, makePageState({}));
expect(params.has('search_trigger_words')).toBe(false);
});
});