* feat(maintenance): repair old Pair prompts' empty category and tags (#550) Squashed from PR #579. Closes #550. * feat(tagging): saved instructions with a default per engine (#431) Squashed from PR #587. * feat(tagging): pick saved instructions in the tagging dialogs (#431) Squashed from PR #588. Closes #431.
This commit is contained in:
@@ -64,11 +64,31 @@ function measureMaintenance(mainSelector) {
|
||||
};
|
||||
}
|
||||
|
||||
/** D102: the Metadata cache row says why its review is unavailable and its button is aria-disabled (D93's dimmed style). */
|
||||
function mapMaintenanceConcept(reference) {
|
||||
/**
|
||||
* D192 (#550, beyond the concept): Database adds "Pair category and tags" after "Unused prompt components", shaped like
|
||||
* the concept's plain Review plan row (Optimize database's).
|
||||
*/
|
||||
function mapPairRow(reference) {
|
||||
return {
|
||||
...reference,
|
||||
surfaces: reference.surfaces.map(surface => ({ ...surface, rows: surface.rows.map(row => row.title !== 'Metadata cache' ? row : {
|
||||
surfaces: reference.surfaces.map(surface => {
|
||||
const plain = surface.rows.find(row => row.title === 'Optimize database');
|
||||
const at = surface.rows.findIndex(row => row.title === 'Unused prompt components');
|
||||
if (!plain || at < 0) return surface;
|
||||
return { ...surface, tree: [...surface.tree, 'div.operation'], rows: [...surface.rows.slice(0, at + 1),
|
||||
{ ...plain, title: 'Pair category and tags', description: 'Fill an empty category or tags on prompts older ' +
|
||||
'Pair node runs saved, from their saved pairs. Only empty fields of never-edited prompts.' },
|
||||
...surface.rows.slice(at + 1)] };
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
/** D102: the Metadata cache row says why its review is unavailable and its button is aria-disabled (D93's dimmed style). */
|
||||
function mapMaintenanceConcept(reference) {
|
||||
const mapped = mapPairRow(reference);
|
||||
return {
|
||||
...mapped,
|
||||
surfaces: mapped.surfaces.map(surface => ({ ...surface, rows: surface.rows.map(row => row.title !== 'Metadata cache' ? row : {
|
||||
...row, description: `${row.description} Nothing is cached in this browser.`,
|
||||
actions: row.actions.map(action => ({ ...action, opacity: '0.55', cursor: 'not-allowed' })) }) })),
|
||||
};
|
||||
@@ -209,8 +229,8 @@ test('maintenance comparison detects mutations', async ({ page, request }) => {
|
||||
const expected = mapMaintenanceConcept(concept);
|
||||
const production = await productionMaintenance(page, 'dark');
|
||||
expect(diffInventory(expected, production)).toEqual([]);
|
||||
// Unmapped, the section differs only on the D102 row.
|
||||
expect(diffInventory(concept, production).map(item => item.path).sort()).toEqual([
|
||||
// Mapped only for the D192 row, the section differs only on the D102 row.
|
||||
expect(diffInventory(mapPairRow(concept), production).map(item => item.path).sort()).toEqual([
|
||||
'surfaces.1.rows.3.actions.0.cursor', 'surfaces.1.rows.3.actions.0.opacity', 'surfaces.1.rows.3.description']);
|
||||
const measure = () => page.evaluate(measureMaintenance, '#workspace-main');
|
||||
const paths = async () => diffInventory(expected, await measure()).map(item => item.path);
|
||||
|
||||
@@ -35,6 +35,8 @@ const MODEL_OPTIONS = ['WD14 SwinV2 · ~400 MB', 'WD14 ViT · ~400 MB', 'GGUF Jo
|
||||
const SUGGESTIONS = 'From 1 to 100: the most suggestions Generate shows for one image.';
|
||||
const SKIPPED_DESCRIPTION = 'Suggested tags that contain one of these words are hidden. Words are separated by spaces, _ or -; '
|
||||
+ 'other characters such as ( ) belong to the word.';
|
||||
const DEFAULT_DESCRIPTION = 'The Tag image dialog and batches start from this.';
|
||||
const INSTRUCTIONS_DESCRIPTION = 'Instructions for the language-model engines. Built-in can’t be changed; duplicate it to make your own.';
|
||||
const BATCH = 'Tag each output image from its own pixels with the model above. Prompts are not changed. Review the scope first.';
|
||||
|
||||
/** Runs in the page: Auto-tagging's surface under `mainSelector`: rows, field, actions, operation, geometry and tokens. */
|
||||
@@ -64,7 +66,7 @@ function measureTagging(mainSelector) {
|
||||
text: [...row.firstElementChild.children].map(node => [name(node), text(node), node.localName === 'label' ? node.htmlFor : null]),
|
||||
control: control(row), flexWrap: getComputedStyle(row).flexWrap,
|
||||
controlBelow: row.lastElementChild.getBoundingClientRect().top >= row.firstElementChild.getBoundingClientRect().bottom - 0.5 })),
|
||||
field: { tree: [...field.children].map(name), label: [text(field.querySelector('label')), field.querySelector('label').htmlFor],
|
||||
field: field && { tree: [...field.children].map(name), label: [text(field.querySelector('label')), field.querySelector('label').htmlFor],
|
||||
textarea: { id: field.querySelector('textarea').id, value: field.querySelector('textarea').value,
|
||||
...css(field.querySelector('textarea'), 'fontSize', 'minHeight', 'backgroundColor', 'color', 'borderTopColor') },
|
||||
...css(field, 'display', 'rowGap', 'fontSize', 'marginTop', 'marginBottom') },
|
||||
@@ -81,7 +83,22 @@ function measureTagging(mainSelector) {
|
||||
label: css(block.querySelector('label'), 'fontSize', 'fontWeight', 'color'),
|
||||
description: css(block.querySelector('.setting-block-text p'), 'fontSize', 'color', 'marginTop'),
|
||||
input: css(block.querySelector('input'), 'fontSize', 'backgroundColor', 'color', 'borderTopColor') })(
|
||||
surface.querySelector(':scope > .setting-block')),
|
||||
surface.querySelector(':scope > .setting-block[data-setting="tag-skipped"]')),
|
||||
// D203 (#431): Saved instructions (the concept has a template field instead).
|
||||
instructions: (block => block && { tree: [...block.children].map(name), key: block.dataset.key,
|
||||
text: [...block.firstElementChild.children].map(node => [node.localName, text(node)]),
|
||||
list: [...block.querySelectorAll('[role="option"]')].map(option => [text(option.firstElementChild),
|
||||
[...option.querySelectorAll('.badges > .chip')].map(chip => [text(chip), chip.className]), option.getAttribute('aria-selected')]),
|
||||
listRole: block.querySelector('.instruction-list').getAttribute('role'),
|
||||
actions: [...block.querySelectorAll('.instruction-actions > button')].map(item => [text(item), item.className, item.disabled]),
|
||||
editor: [...block.querySelectorAll('.instruction-editor .field')].map(item => [text(item.querySelector('label > span')),
|
||||
item.querySelector('input, textarea').value, item.querySelector('input, textarea').readOnly]),
|
||||
note: text(block.querySelector('#instruction-note')),
|
||||
layout: getComputedStyle(block.querySelector('.instructions-layout')).gridTemplateColumns.split(' ').length,
|
||||
...css(block, 'paddingTop', 'paddingBottom', 'borderTopWidth', 'borderTopColor'),
|
||||
label: css(block.querySelector('label'), 'fontSize', 'fontWeight', 'color'),
|
||||
textarea: css(block.querySelector('textarea'), 'fontSize', 'backgroundColor', 'borderTopColor') })(
|
||||
surface.querySelector(':scope > .setting-block[data-setting="tag-instructions"]')),
|
||||
operations: [...surface.querySelectorAll(':scope > .operation')].map(row => ({ tree: [...row.children].map(name),
|
||||
title: text(row.querySelector('h3')), description: text(row.querySelector('p')),
|
||||
actions: [...row.querySelectorAll('.row-actions > button')].map(item => [text(item), item.className]) })),
|
||||
@@ -115,14 +132,21 @@ function measureTagging(mainSelector) {
|
||||
* The concept's Auto-tagging with the recorded deviations applied, for the fixture (all engines on disk, none loaded).
|
||||
* `phone` is a viewport at the concept's 700px breakpoint or below.
|
||||
*/
|
||||
function mapTaggingConcept(reference, { template, phone }) {
|
||||
function mapTaggingConcept(reference, { builtin, saved, phone }) {
|
||||
const [model, existing, general, character] = reference.rows;
|
||||
const [preview] = reference.actions.buttons;
|
||||
// D203: Built-in first, then the saved entries, in each default select.
|
||||
const names = ['Built-in', ...saved.map(entry => entry.name)];
|
||||
const defaultRow = (engine, long) => ({ ...existing,
|
||||
text: [['label', `Default for ${long}`, `instruction-default-${engine}`], ['p', DEFAULT_DESCRIPTION, null]],
|
||||
control: { ...existing.control, id: `instruction-default-${engine}`, options: names.map(label => [label, false]),
|
||||
selected: names[1] } });
|
||||
return {
|
||||
...reference,
|
||||
// D133 adds a row after the thresholds, D202 the Skipped tags block after it; D134 adds the operation after the
|
||||
// actions.
|
||||
tree: [...reference.tree.slice(0, 6), 'div.setting-row', 'div.setting-block', ...reference.tree.slice(6), 'div.operation'],
|
||||
// D133 adds a row after the thresholds, D202 the Skipped tags block after it; D203 replaces the template field
|
||||
// with Saved instructions, the two default rows and their note; D134 adds the operation after the actions.
|
||||
tree: [...reference.tree.slice(0, 6), 'div.setting-row', 'div.setting-block', 'div.setting-block', 'div.setting-row',
|
||||
'div.setting-row', 'p.instruction-reset.small', reference.tree[7], 'div.operation'],
|
||||
rows: [
|
||||
// D132 (OQ5, OQ6): the four real engines with their download size, and the chosen one's live status.
|
||||
{ ...model, text: [model.text[0], ['p', 'Downloaded. Not loaded.', null]],
|
||||
@@ -133,7 +157,26 @@ function mapTaggingConcept(reference, { template, phone }) {
|
||||
// D133: the persisted suggestion count, on the concept's number row.
|
||||
{ ...general, text: [['label', 'Suggestions per image', 'tag-suggestions'], ['p', SUGGESTIONS, null]],
|
||||
control: { ...general.control, id: 'tag-suggestions', value: '10' } },
|
||||
// D203: the default of each language-model engine (the fixture's saved entry is both), on the concept's
|
||||
// select row.
|
||||
defaultRow('gguf', 'GGUF JoyCaption'), defaultRow('hf', 'HuggingFace JoyCaption'),
|
||||
],
|
||||
// D203: Saved instructions in the concept's setting-row tokens (as D202), Built-in selected and read-only.
|
||||
instructions: { tree: ['div.setting-block-text', 'div.instructions-layout'], key: 'autotag_instructions',
|
||||
text: [['label', 'Saved instructions'], ['p', `${INSTRUCTIONS_DESCRIPTION} ${saved.length} of 20 saved · Built\u2011in is not counted.`]],
|
||||
list: [['Built-in', [['Read-only', 'chip']], 'true'],
|
||||
...saved.map((entry, index) => [entry.name, index === 0
|
||||
? [['Default for GGUF', 'chip blue'], ['Default for HuggingFace', 'chip blue']] : [], 'false'])],
|
||||
listRole: 'listbox',
|
||||
actions: [['+ New', '', false], ['Duplicate', '', false], ['Delete', 'danger', true]],
|
||||
editor: [['Name', 'Built-in', true], ['Text', builtin, true]],
|
||||
note: 'Built-in instructions can’t be changed. Duplicate them to make your own.',
|
||||
layout: phone ? 1 : 2,
|
||||
paddingTop: reference.row.paddingTop, paddingBottom: reference.row.paddingBottom,
|
||||
borderTopWidth: reference.row.borderTopWidth, borderTopColor: reference.row.borderTopColor,
|
||||
label: reference.label,
|
||||
textarea: { fontSize: reference.field.textarea.fontSize, backgroundColor: reference.field.textarea.backgroundColor,
|
||||
borderTopColor: reference.field.textarea.borderTopColor } },
|
||||
// D202 (#431): the Skipped tags block in the concept's setting-row tokens: its label and description as a row's,
|
||||
// the row's padding and rule above, the concept's field input; an empty list says so.
|
||||
skipped: { tree: ['div.setting-block-text', 'div.skip-add', 'p.skip-problem.small', 'div.skip-chips.tags', 'p.skip-preview.small'],
|
||||
@@ -148,15 +191,15 @@ function mapTaggingConcept(reference, { template, phone }) {
|
||||
marginTop: reference.description.marginTop },
|
||||
input: { fontSize: '12px', backgroundColor: reference.field.textarea.backgroundColor, color: reference.field.textarea.color,
|
||||
borderTopColor: reference.field.textarea.borderTopColor } },
|
||||
// The saved template (the server's default until one is saved) replaces the concept's sample text.
|
||||
field: { ...reference.field, textarea: { ...reference.field.textarea, value: template } },
|
||||
// D203: no template field.
|
||||
field: null,
|
||||
// D132: Download (for an engine already on disk, "Download again") and Unload replace the two simulated jobs.
|
||||
actions: { ...reference.actions, buttons: [preview, ['Download again', ''], ['Unload', '']] },
|
||||
// D134: the prompt-tag batch, reviewed first.
|
||||
operations: [{ tree: ['div', 'div.row-actions'], title: 'Tag output images', description: BATCH, actions: [['Review', '']] }],
|
||||
// D132 (#398): every select shows its whole chosen option at every width (at 390 the concept's 120px cap cuts
|
||||
// them; production lets each grow past it).
|
||||
selectsFit: reference.selectsFit.map(() => true),
|
||||
selectsFit: [...reference.selectsFit, true, true].map(() => true),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -180,7 +223,9 @@ async function productionTagging(page, theme) {
|
||||
async function savedTemplate(request, page) {
|
||||
const response = await request.get('/prompt_manager/settings');
|
||||
await expect(response).toBeOK();
|
||||
return { template: (await response.json()).settings.autotag_template, phone: page.viewportSize().width <= 700 };
|
||||
const { settings } = await response.json();
|
||||
return { builtin: settings.autotag_builtin_instructions, saved: settings.autotag_instructions,
|
||||
phone: page.viewportSize().width <= 700 };
|
||||
}
|
||||
|
||||
for (const theme of THEMES) for (const [width, height] of SIZES) {
|
||||
@@ -204,14 +249,15 @@ for (const theme of THEMES) for (const width of [390, 320]) {
|
||||
await page.locator('#workspace-main #tag-existing').selectOption('replace_after_review');
|
||||
await page.locator('#workspace-main section.surface').screenshot({ path: testInfo.outputPath(`production-tagging-selects-${theme}-${width}.png`) });
|
||||
const selects = page.locator('#workspace-main section.surface > .setting-row > select');
|
||||
await expect(selects).toHaveCount(2);
|
||||
for (let index = 0; index < 2; index += 1) {
|
||||
// D203 adds the two per-engine default selects.
|
||||
await expect(selects).toHaveCount(4);
|
||||
for (let index = 0; index < 4; index += 1) {
|
||||
const select = selects.nth(index);
|
||||
const values = await select.evaluate(node => [...node.options].map(option => option.value));
|
||||
for (const value of values) {
|
||||
await select.selectOption(value);
|
||||
const measured = await page.evaluate(measureTagging, '#workspace-main');
|
||||
expect(measured.selectsFit, `${await select.getAttribute('id')}=${value}`).toEqual([true, true]);
|
||||
expect(measured.selectsFit, `${await select.getAttribute('id')}=${value}`).toEqual([true, true, true, true]);
|
||||
expect(measured.pageScroll, value).toBe(false);
|
||||
}
|
||||
}
|
||||
@@ -225,15 +271,16 @@ test('auto-tagging comparison detects mutations', async ({ page, request }) => {
|
||||
const expected = mapTaggingConcept(concept, saved);
|
||||
const production = await productionTagging(page, 'dark');
|
||||
expect(diffInventory(expected, production)).toEqual([]);
|
||||
// Unmapped, the section differs only where D132, D133, D134 and D202 are recorded (and the saved template's text).
|
||||
// Unmapped, the section differs only where D132, D133, D134, D202 and D203 are recorded (and the saved template's text).
|
||||
// (The concept has no Skipped tags block, so every path under it is one D202 difference.)
|
||||
expect(diffInventory(concept, production).map(item => (item.path.startsWith('skipped') ? 'skipped'
|
||||
expect(diffInventory(concept, production).map(item => (/^(skipped|instructions|field)\b/.test(item.path) ? item.path.split('.')[0]
|
||||
: item.path.split('.').slice(0, 3).join('.')))
|
||||
.filter((path, index, all) => all.indexOf(path) === index).sort()).toEqual([
|
||||
'actions.buttons.1', 'actions.buttons.2', 'field.textarea.value', 'operations.0.actions', 'operations.0.description',
|
||||
'operations.0.title', 'operations.0.tree', 'operations.length', 'rows.0.control', 'rows.0.text', 'rows.4.control', 'rows.4.controlBelow',
|
||||
'rows.4.flexWrap', 'rows.4.text', 'rows.4.tree', 'rows.length', 'skipped', 'tree.6', 'tree.7', 'tree.8', 'tree.9', 'tree.10',
|
||||
'tree.length'].sort());
|
||||
'actions.buttons.1', 'actions.buttons.2', 'field', 'instructions', 'operations.0.actions', 'operations.0.description',
|
||||
'operations.0.title', 'operations.0.tree', 'operations.length', 'rows.0.control', 'rows.0.text',
|
||||
...[4, 5, 6].flatMap(row => ['control', 'controlBelow', 'flexWrap', 'text', 'tree'].map(key => `rows.${row}.${key}`)),
|
||||
'rows.length', 'selectsFit.2', 'selectsFit.3', 'selectsFit.length', 'skipped',
|
||||
...Array.from({ length: 8 }, (_, index) => `tree.${index + 6}`), 'tree.length'].sort());
|
||||
const measure = () => page.evaluate(measureTagging, '#workspace-main');
|
||||
const paths = async () => diffInventory(expected, await measure()).map(item => item.path);
|
||||
// A removed row.
|
||||
@@ -284,8 +331,8 @@ test('auto-tagging at 390 wraps only the engine row', async ({ page, request })
|
||||
const concept = await conceptTagging(page, 'dark');
|
||||
const production = await productionTagging(page, 'dark');
|
||||
expect(diffInventory(mapTaggingConcept(concept, await savedTemplate(request, page)), production)).toEqual([]);
|
||||
expect(production.rows.map(row => row.flexWrap)).toEqual(['wrap', 'nowrap', 'nowrap', 'nowrap', 'nowrap']);
|
||||
expect(production.selectsFit).toEqual([true, true]);
|
||||
expect(production.rows.map(row => row.flexWrap)).toEqual(['wrap', 'nowrap', 'nowrap', 'nowrap', 'nowrap', 'nowrap', 'nowrap']);
|
||||
expect(production.selectsFit).toEqual([true, true, true, true]);
|
||||
});
|
||||
|
||||
/** Runs in the page: the open review dialog: its title, body and the concept's tokens. */
|
||||
|
||||
@@ -92,6 +92,8 @@ export function cardActionsInventory(card) {
|
||||
};
|
||||
}
|
||||
|
||||
// #431: the fixture's saved "Describe the image" entry, the default of both engines (workspace_tagging_fixture.py).
|
||||
export const FIXTURE_INSTRUCTIONS_ID='d35c71be0a1e';
|
||||
// D84 (plan D61): the legacy engines' values, labels and sizes, GGUF JoyCaption in the concept's Ollama slot.
|
||||
const MODEL_LABELS=Object.freeze({'wd14-swinv2':['wd14-swinv2','WD14 SwinV2 · ~400 MB'],'wd14-vit':['wd14-vit','WD14 ViT · ~400 MB'],
|
||||
ollama:['gguf','GGUF JoyCaption (Recommended) · ~4.5 GB'],huggingface:['hf','HuggingFace JoyCaption · ~16 GB']});
|
||||
@@ -106,17 +108,24 @@ const CONCEPT_LABELS=Object.freeze({'WD14 SwinV2 · fast classifier':'WD14 SwinV
|
||||
* (`chip`: a concept `.chip` inventory, the heading's size chip) around the row's own text (12px) in the checked
|
||||
* row's colours (accent border, `--soft-blue`);
|
||||
* - D84 (plan D61): the legacy engine labels and values, and the server's default instructions (`instructions`);
|
||||
* - D204 (#431): a "Saved instructions" select (`picker`: its value and options) before the instructions, on the
|
||||
* concept's select field tokens;
|
||||
* - D91 (plan D68): a Suggestions count field after the model's own fields, on the concept's number field tokens
|
||||
* (`numberField`: the concept's General confidence field) with the configured count (`limit`);
|
||||
* - D29: the prototype's "Sample suggestions from …" status reads "Suggestions from …".
|
||||
* The opened state is compared unmapped: both show the concept's on-screen "Generate suggestions with the selected
|
||||
* model before approving." (its renderTagModelOptions, a1062–1076, replaces the markup's sentence at once).
|
||||
*/
|
||||
export function mapConceptTagging(reference,{instructions,limit=10,chip,numberField}) {
|
||||
export function mapConceptTagging(reference,{instructions,limit=10,chip,numberField,
|
||||
picker={value:FIXTURE_INSTRUCTIONS_ID,options:[['builtin','Built-in'],[FIXTURE_INSTRUCTIONS_ID,`${instructions} · default`]]}}) {
|
||||
const [select,...own]=reference.modelPanel.fields;
|
||||
// D204 (#431): a "Saved instructions" select on the concept's select field, before the instructions.
|
||||
const pickField={...select,label:'Saved instructions',control:{...select.control,id:'image-tag-instructions-pick',
|
||||
value:picker.value,options:picker.options}};
|
||||
const suggestionsField={...numberField,label:'Suggestions',control:{...numberField.control,id:'image-suggestion-limit',
|
||||
value:String(limit),min:'1',max:'100',step:'1'}};
|
||||
const mappedOwn=own.map(field=>field.control.id==='image-tag-instructions'?{...field,control:{...field.control,value:instructions}}:field);
|
||||
const mappedOwn=own.flatMap(field=>field.control.id==='image-tag-instructions'
|
||||
?[pickField,{...field,control:{...field.control,value:instructions}}]:[field]);
|
||||
const rows=reference.suggestions.rows.map(row=>({...row,
|
||||
control:row.control?.kind==='checkbox'&&row.control.checked?{kind:'remove',label:`Remove ${row.name}`}:row.control,
|
||||
borderTopLeftRadius:chip.borderTopLeftRadius}));
|
||||
|
||||
@@ -64,7 +64,9 @@ test('the section shows the concept rows; Metadata cache follows Q5 (D102)', asy
|
||||
item.querySelector('.row-actions > button').textContent, item.querySelector('.row-actions > button').className]),
|
||||
]))).toEqual([
|
||||
['Database', [['Integrity & relationship checks', 'Check', ''], ['Duplicate prompt text', 'Review plan', ''],
|
||||
['Optimize database', 'Review plan', ''], ['Unused prompt components', 'Review plan', 'danger']]],
|
||||
['Optimize database', 'Review plan', ''], ['Unused prompt components', 'Review plan', 'danger'],
|
||||
// D192 (#550): beyond the concept.
|
||||
['Pair category and tags', 'Review plan', '']]],
|
||||
['Images & cache', [['Missing-file links', 'Review plan', 'danger'], ['Duplicate image files', 'Find duplicates', ''],
|
||||
['Thumbnail cache', 'Review options', ''], ['Metadata cache', 'Review plan', '']]],
|
||||
]);
|
||||
@@ -177,6 +179,70 @@ test("plan dialog shows the server's scope, protected, backup policy and media l
|
||||
expect(await serverJson(request, '/__test__/system/digest')).toEqual(before);
|
||||
});
|
||||
|
||||
test('D192: Pair category and tags reviews old Pair prompts, keeps an edited one and fills the other (#550)',
|
||||
async ({ page, request }) => {
|
||||
await expect(await request.post('/__test__/system/pair-metadata')).toBeOK();
|
||||
const answers = [];
|
||||
page.on('response', response => { if (response.url().endsWith(PREVIEW_URL)) answers.push(response.json()); });
|
||||
await openMaintenance(page);
|
||||
const pairRow = row(page, 'Pair category and tags');
|
||||
await expect(pairRow.locator('p')).toHaveText('Fill an empty category or tags on prompts older Pair node runs saved, ' +
|
||||
'from their saved pairs. Only empty fields of never-edited prompts.');
|
||||
await pairRow.getByRole('button', { name: 'Review plan', exact: true }).click();
|
||||
const plan = dialog(page);
|
||||
await expect(plan.locator('#modal-body > h3')).toHaveText('Pair category and tags');
|
||||
const answer = await answers[0];
|
||||
expect(answer.operation).toBe('pair_metadata');
|
||||
expect(await gridOf(page)).toEqual([
|
||||
['Scope', '1 prompt to fill: 1 category, 1 tag set'],
|
||||
['Protected', '1 edited after it was saved'],
|
||||
['Backup', `Create and verify before changes (${answer.backup.destination_label})`],
|
||||
['Media on disk', 'No image file or saved pair is changed.'],
|
||||
]);
|
||||
await expect(plan.locator('details > summary')).toHaveText('Listed records (3)');
|
||||
await plan.locator('details > summary').click();
|
||||
// Protected rows come first, the Record row last.
|
||||
const lines = (await plan.locator('details > pre').textContent()).split('\n');
|
||||
expect(lines).toHaveLength(3);
|
||||
expect(lines[0]).toBe('S550 harbor · Kept: edited after it was saved; its empty category or tags may be intentional');
|
||||
expect(lines[1]).toMatch(/^S550 lantern · Fills category “night” and 2 tags: s550-glow, s550-warm \(pair \d+\)$/);
|
||||
expect(lines[2]).toMatch(/^Record · The job's report lists every filled field\. Download it to keep it/);
|
||||
await plan.getByRole('button', { name: 'Apply reviewed operation', exact: true }).click();
|
||||
await expect(dialog(page)).toHaveCount(0);
|
||||
await expect(page).toHaveURL(/#\/activity\/[0-9a-f]{32}$/);
|
||||
const job = await ended(request, page.url().split('/').pop());
|
||||
expect([job.title, job.state, job.counters.changed, job.counters.protected])
|
||||
.toEqual(['Pair category and tags', 'finished', 1, 1]);
|
||||
const main = page.locator('#workspace-main');
|
||||
await expect(main.locator('h1')).toHaveText('Pair category and tags');
|
||||
await expect(main.getByRole('button', { name: 'Download report', exact: true })).toBeVisible();
|
||||
// #550 live test: the Changed group lists the path-less record rows as plain text, never an open action.
|
||||
const results = main.getByRole('region', { name: 'Results', exact: true });
|
||||
await results.locator('.outcome-list > button').filter({ hasText: 'Changed' }).click();
|
||||
const records = results.locator('.outcome-files > .file-row');
|
||||
await expect(records).toHaveCount(1);
|
||||
await expect(records.first()).toHaveText(/^Prompt \d+: category “night”; tags s550-glow, s550-warm$/);
|
||||
expect(await records.first().evaluate(node => node.localName)).toBe('div');
|
||||
await expect(results.locator('.outcome-files button')).toHaveCount(0);
|
||||
await expect(results.getByRole('searchbox', { name: 'Filter results' })).toHaveAttribute('placeholder', 'Filter records…');
|
||||
// Protected lists each kept prompt with the review's reason (#550 live re-check).
|
||||
await results.locator('.outcome-list > button').filter({ hasText: 'Protected' }).click();
|
||||
await expect(records).toHaveCount(1);
|
||||
await expect(records.first()).toHaveText(
|
||||
/^Prompt \d+: kept, edited after it was saved; its empty category or tags may be intentional$/);
|
||||
// An empty group says records, not files.
|
||||
await results.locator('.outcome-list > button').filter({ hasText: 'Errors' }).click();
|
||||
await expect(results.locator('.outcome-files > p.empty-state')).toHaveText('No records in this group.');
|
||||
const library = await serverJson(request, '/prompt_manager/library');
|
||||
const lantern = library.items.find(item => item.text === 'S550 lantern');
|
||||
const harbor = library.items.find(item => item.text === 'S550 harbor');
|
||||
const filled = (await serverJson(request, `/prompt_manager/library/${lantern.id}?archive=all`)).positive;
|
||||
expect([filled.category, [...filled.tags].sort()]).toEqual(['night', ['s550-glow', 's550-warm']]);
|
||||
expect((await serverJson(request, `/prompt_manager/library/${harbor.id}?archive=all`)).positive.category).toBeFalsy();
|
||||
await page.goto(`${workspace}#/library`);
|
||||
await expect(page.locator(`article[data-prompt-id="${lantern.id}"] .card-title > .chip`)).toHaveText('night');
|
||||
});
|
||||
|
||||
test('a plan with nothing to do shows only Close, never Apply', async ({ page, request }) => {
|
||||
const applies = [];
|
||||
page.on('request', item => { if (item.url().endsWith(APPLY_URL)) applies.push(item.url()); });
|
||||
|
||||
@@ -112,13 +112,14 @@ test('model options are the four real engines with size and the description show
|
||||
await expect(status(page)).toHaveText('Unsaved changes.');
|
||||
// The other rows keep the concept's copy and the saved values (D133 adds Suggestions per image).
|
||||
expect(await surface(page).locator(':scope > .setting-row label').allTextContents()).toEqual(['Tagging model', 'Existing tags',
|
||||
'General-tag confidence', 'Character-tag confidence', 'Suggestions per image']);
|
||||
'General-tag confidence', 'Character-tag confidence', 'Suggestions per image', 'Default for GGUF JoyCaption',
|
||||
'Default for HuggingFace JoyCaption']);
|
||||
await expect(page.locator('#tag-existing')).toHaveValue('skip');
|
||||
await expect(page.locator('#threshold-general')).toHaveValue('0.35');
|
||||
await expect(page.locator('#threshold-character')).toHaveValue('0.85');
|
||||
await expect(page.locator('#tag-suggestions')).toHaveValue('10');
|
||||
const settings = await serverJson(request, SETTINGS_URL);
|
||||
await expect(page.locator('#tag-template')).toHaveValue(settings.settings.autotag_template);
|
||||
// D203 (#431): Saved instructions replace the template field.
|
||||
await expect(page.locator('#tag-template')).toHaveCount(0);
|
||||
// Unload is offered only while a model is loaded (none is).
|
||||
await expect(actionOf(page, 'Unload')).toBeDisabled();
|
||||
});
|
||||
@@ -500,3 +501,74 @@ test('skipped tags preview checks library tags only without a label list, and a
|
||||
await expect(skippedBlock(page)).toHaveAttribute('aria-invalid', 'true');
|
||||
await page.unrouteAll({ behavior: 'ignoreErrors' });
|
||||
});
|
||||
|
||||
// D203 (#431): Saved instructions: Built-in read-only, New, edit, per-engine defaults, Delete resets a default, Save
|
||||
// preferences and a reload; the server pairs the two keys. D205: the batch review names the engine's default.
|
||||
const instructionOption = (page, name) => page.locator('#instruction-list [role="option"]').filter({ has: page.locator(`span:text-is("${name}")`) });
|
||||
|
||||
test('saved instructions: new, edit, per-engine default, delete, save and reload (D203)', async ({ page, request }) => {
|
||||
await openTagging(page);
|
||||
const settings = (await serverJson(request, SETTINGS_URL)).settings;
|
||||
await expect(page.locator('#instructions-count')).toHaveText('1 of 20 saved · Built\u2011in is not counted.');
|
||||
await expect(page.locator('#instruction-list [role="option"] > span:first-child')).toHaveText(['Built-in', 'Describe the image']);
|
||||
await expect(page.locator('#instruction-name')).toHaveValue('Built-in');
|
||||
await expect(page.locator('#instruction-text')).toHaveValue(settings.autotag_builtin_instructions);
|
||||
await expect(page.locator('#instruction-text')).toHaveAttribute('readonly', '');
|
||||
await expect(page.locator('#instruction-delete')).toBeDisabled();
|
||||
await expect(page.locator('#instruction-default-gguf')).toHaveValue(settings.autotag_instructions[0].id);
|
||||
// + New, then name and text it, and make it HuggingFace's default.
|
||||
await page.locator('#instruction-new').click();
|
||||
await expect(page.locator('#instruction-name')).toHaveValue('New instructions');
|
||||
await expect(page.locator('#instruction-note')).toHaveText('The text must not be empty.');
|
||||
await page.locator('#instruction-name').fill('Photo realism');
|
||||
await page.locator('#instruction-text').fill('Tag camera angle and lighting.');
|
||||
await expect(page.locator('#instruction-text').locator('xpath=..').locator('label .muted')).toHaveText('30 of 4,000 characters');
|
||||
await page.locator('#instruction-default-hf').selectOption({ label: 'Photo realism' });
|
||||
await expect(instructionOption(page, 'Photo realism').locator('.chip')).toHaveText(['Default for HuggingFace']);
|
||||
await expect(status(page)).toHaveText('Unsaved changes.');
|
||||
await save(page).click();
|
||||
await expect(status(page)).toHaveText('Saved to the server configuration.');
|
||||
let saved = (await serverJson(request, SETTINGS_URL)).settings;
|
||||
expect(saved.autotag_instructions.map(entry => [entry.name, entry.text])).toEqual([['Describe the image', 'Describe the image'],
|
||||
['Photo realism', 'Tag camera angle and lighting.']]);
|
||||
expect(saved.autotag_default_instructions.hf).toBe(saved.autotag_instructions[1].id);
|
||||
// A reload shows the saved list; deleting GGUF's default resets it to Built-in and says so.
|
||||
await openTagging(page);
|
||||
await instructionOption(page, 'Describe the image').click();
|
||||
await page.locator('#instruction-delete').click();
|
||||
await expect(page.locator('#instruction-reset')).toHaveText('GGUF now uses Built-in.');
|
||||
await expect(page.locator('#instruction-default-gguf')).toHaveValue('builtin');
|
||||
await save(page).click();
|
||||
await expect(status(page)).toHaveText('Saved to the server configuration.');
|
||||
saved = (await serverJson(request, SETTINGS_URL)).settings;
|
||||
expect(saved.autotag_instructions.map(entry => entry.name)).toEqual(['Photo realism']);
|
||||
expect(saved.autotag_default_instructions.gguf).toBe('builtin');
|
||||
expect(saved.autotag_template).toBe(saved.autotag_builtin_instructions);
|
||||
});
|
||||
|
||||
test('a refused instructions save names the entry and highlights the block (D203)', async ({ page }) => {
|
||||
await openTagging(page);
|
||||
await page.locator('#instruction-duplicate').click();
|
||||
await page.locator('#instruction-name').fill('describe the IMAGE');
|
||||
await expect(page.locator('#instruction-note')).toHaveText('The name “describe the IMAGE” is already used.');
|
||||
await save(page).click();
|
||||
await expect(status(page)).toHaveText('Saved instructions: entry 2 name “describe the IMAGE” is already used.');
|
||||
await expect(surface(page).locator('.setting-block[data-setting="tag-instructions"]')).toHaveAttribute('aria-invalid', 'true');
|
||||
});
|
||||
|
||||
test('the batch review names the language model\'s default instructions (D205)', async ({ page }) => {
|
||||
await openTagging(page);
|
||||
await page.locator('#tag-model').selectOption('gguf');
|
||||
await page.locator('#tag-existing').selectOption('merge');
|
||||
const answer = page.waitForResponse(item => item.url().endsWith(PREVIEW_URL));
|
||||
await reviewButton(page).click();
|
||||
await answer;
|
||||
await expect(dialog(page).locator('#modal-title')).toHaveText('Review tag output images');
|
||||
const grid = await gridOf(page);
|
||||
expect(grid.slice(-3)).toEqual([['Instructions', 'Describe the image'], ['Settings', 'Up to 10 tags per image'],
|
||||
grid.at(-1)]);
|
||||
// The batch sends the default's text.
|
||||
const start = page.waitForRequest(item => item.url().endsWith('/prompt_manager/jobs/autotag'));
|
||||
await dialog(page).getByRole('button', { name: 'Start tagging', exact: true }).click();
|
||||
expect((await start).postDataJSON().options).toEqual({ instructions: 'Describe the image' });
|
||||
});
|
||||
|
||||
@@ -341,3 +341,21 @@ test('a running scan makes the start report busy', async ({ page, request }) =>
|
||||
await request.post(`/__test__/jobs/${scan.job.id}/finish`, { data: { state: 'finished' } });
|
||||
}
|
||||
});
|
||||
|
||||
// D204 (#431): Auto-tag selected has the same picker; the job sends the text the dialog shows.
|
||||
test('Auto-tag selected picks saved instructions and sends the text shown (D204)', async ({ page }) => {
|
||||
await select(page, FIRST);
|
||||
await barButton(page, 'Auto-tag selected').click();
|
||||
await expect(title(page)).toHaveText('Auto-tag selected images');
|
||||
const body = modal(page).locator('#modal-body');
|
||||
await body.getByRole('combobox', { name: 'Tagging model', exact: true }).selectOption('gguf');
|
||||
const pick = body.getByRole('combobox', { name: 'Saved instructions', exact: true });
|
||||
await expect(pick).toHaveAttribute('id', 'bulk-tag-instructions-pick');
|
||||
await expect(pick.locator('option')).toHaveText(['Built-in', 'Describe the image · default']);
|
||||
const text = body.getByRole('textbox', { name: 'Tagging instructions', exact: true });
|
||||
await text.fill('Bulk one-off.');
|
||||
await expect(pick.locator('option:checked')).toHaveText('Edited for this run');
|
||||
const started = jobStart(page);
|
||||
await actionButtons(page).filter({ hasText: 'Start auto-tagging' }).click();
|
||||
expect((await started).postDataJSON().options.instructions).toBe('Bulk one-off.');
|
||||
});
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { startCoverage, saveCoverage } from '../../tools/vendor/browser-coverage.mjs';
|
||||
import { test, expect } from './fixtures.mjs';
|
||||
import { saveImageTags } from './concept-inventory.mjs';
|
||||
import { taggingInventory } from './tagging-canonical-comparison.mjs';
|
||||
import { FIXTURE_INSTRUCTIONS_ID, taggingInventory } from './tagging-canonical-comparison.mjs';
|
||||
|
||||
const images = '/prompt_manager/workspace#/images';
|
||||
const MODULE = '/prompt_manager/js/workspace/image-tagging.mjs';
|
||||
@@ -861,3 +861,49 @@ test('suggestions omit skipped tags and say how many were hidden', async ({ page
|
||||
await expect(chips(page).locator(':scope > span:first-child')).toHaveText(['sandstone', 'arch', 'sky', 'dune']);
|
||||
await expect(dialog(page).locator('#tag-suggestions > p.small')).toHaveText(`${CHIP_HINT} 2 skipped tags were not suggested.`);
|
||||
});
|
||||
|
||||
// D204 (#431): the Saved instructions select above Tagging instructions: the engine default, a pick, a one-off edit
|
||||
// ("Edited for this run" and the muted note), an engine switch resets, WD14 hides both; Generate sends the text shown.
|
||||
test('the saved instructions picker fills, edits one-off and resets per engine (D204)', async ({ page, request }) => {
|
||||
const settings = await (await request.get('/prompt_manager/settings')).json();
|
||||
const builtin = settings.settings.autotag_builtin_instructions;
|
||||
await openTag(page);
|
||||
const options = dialog(page).locator('#image-tag-model-options');
|
||||
const pick = options.getByRole('combobox', { name: 'Saved instructions', exact: true });
|
||||
const text = options.getByRole('textbox', { name: 'Tagging instructions', exact: true });
|
||||
const note = options.locator('.instructions-edited');
|
||||
await expect(pick).toHaveCount(0);
|
||||
await model(page).selectOption('gguf');
|
||||
await expect(pick.locator('option')).toHaveText(['Built-in', 'Describe the image · default']);
|
||||
await expect(pick).toHaveAttribute('id', 'image-tag-instructions-pick');
|
||||
await expect(text).toHaveValue('Describe the image');
|
||||
await expect(note).toBeHidden();
|
||||
// A keyboard pick (the select focused) keeps focus on the select the fields are re-rendered with.
|
||||
await pick.focus();
|
||||
await pick.selectOption('builtin');
|
||||
await expect(text).toHaveValue(builtin);
|
||||
await expect(pick).toBeFocused();
|
||||
await text.fill('Only the sky.');
|
||||
await expect(text).toHaveAttribute('aria-describedby', 'image-tag-instructions-pick-note');
|
||||
await expect(pick.locator('option:checked')).toHaveText('Edited for this run');
|
||||
await expect(pick.locator('option[value="edited"]')).toBeDisabled();
|
||||
await expect(note).toHaveText('Edits apply to this run only. Manage saved instructions in System › Auto-tagging.');
|
||||
await expect(text).toBeFocused();
|
||||
// Generate sends the edited text; the saved entries are unchanged.
|
||||
const sent = page.waitForRequest(item => item.url().endsWith('/image-annotations/suggest'));
|
||||
await generate(page).click();
|
||||
expect((await sent).postDataJSON().options.instructions).toBe('Only the sky.');
|
||||
// Choosing an entry replaces the edit; switching engines starts from the new engine's default.
|
||||
await pick.selectOption('builtin');
|
||||
await expect(text).toHaveValue(builtin);
|
||||
await expect(note).toBeHidden();
|
||||
await text.fill('Edited again.');
|
||||
await model(page).selectOption('hf');
|
||||
await expect(text).toHaveValue('Describe the image');
|
||||
await expect(pick).toHaveValue(FIXTURE_INSTRUCTIONS_ID);
|
||||
await model(page).selectOption('wd14-vit');
|
||||
await expect(pick).toHaveCount(0);
|
||||
await expect(text).toHaveCount(0);
|
||||
expect((await (await request.get('/prompt_manager/settings')).json()).settings.autotag_instructions)
|
||||
.toEqual(settings.settings.autotag_instructions);
|
||||
});
|
||||
|
||||
@@ -41,6 +41,11 @@ read. These per-server hooks only change what a System test needs on top:
|
||||
- ``POST /__test__/system/duplicate-prompts`` (S7) adds two prompts whose
|
||||
text differs only in case and spacing (a duplicate prompt text group);
|
||||
``{"again": true}`` adds a third, so a reviewed plan no longer matches;
|
||||
- ``POST /__test__/system/pair-metadata`` (#550) saves two saved pairs the
|
||||
way the Pair node did before #546 (the pair holds the category and tags,
|
||||
its new positive prompt none): ``S550 lantern`` (category ``night``, tags
|
||||
``s550-glow``, ``s550-warm``) fills; ``S550 harbor`` was edited after it was
|
||||
saved, so the plan keeps it;
|
||||
- ``POST /__test__/system/duplicate-files`` (S7) writes one PNG's bytes three
|
||||
times: ``output/s7-dupes/same.png``, ``input/s7-dupes/same.png`` (the same
|
||||
relative path in the other source) and ``output/s7-dupes/copy.png``;
|
||||
@@ -344,6 +349,25 @@ def register_system_commands(app, routes, api, root: Path):
|
||||
conn.commit()
|
||||
return web.json_response({"success": True})
|
||||
|
||||
@routes.post("/__test__/system/pair-metadata")
|
||||
async def pair_metadata(request):
|
||||
def seed():
|
||||
api.db.save_prompt_pair(
|
||||
"S550 lantern",
|
||||
"S550 blurry",
|
||||
category="night",
|
||||
tags=["s550-glow", "s550-warm"],
|
||||
)
|
||||
api.db.save_prompt_pair("S550 harbor", category="night")
|
||||
with api.db.model.get_connection() as conn:
|
||||
conn.execute(
|
||||
"UPDATE prompts SET updated_at='2099-01-01T00:00:00+00:00' "
|
||||
"WHERE text='S550 harbor'"
|
||||
)
|
||||
|
||||
await api._run_in_executor(seed)
|
||||
return web.json_response({"success": True})
|
||||
|
||||
@routes.post("/__test__/system/duplicate-files")
|
||||
async def duplicate_files(request):
|
||||
body = await request.json() if request.can_read_body else {}
|
||||
|
||||
@@ -391,10 +391,15 @@ DOWNLOAD_HOLD_LIMIT_S = 60
|
||||
DEFAULTS = (0.35, 0.85, 10)
|
||||
|
||||
|
||||
# The saved "Describe the image" entry's id (#431).
|
||||
FIXTURE_INSTRUCTIONS_ID = "d35c71be0a1e"
|
||||
|
||||
|
||||
def _set_defaults(model, values) -> None:
|
||||
"""Set the saved tagging defaults (``AutotagConfig``, #328); a reset also
|
||||
puts back the saved model, batch policy and template (the fake model's
|
||||
short instructions, which the concept comparisons show)."""
|
||||
puts back the saved model, batch policy, skipped tags and instructions
|
||||
(the fake model's short instructions, which the concept comparisons
|
||||
show)."""
|
||||
from py.config import AutotagConfig
|
||||
|
||||
general, character, limit = values
|
||||
@@ -404,7 +409,21 @@ def _set_defaults(model, values) -> None:
|
||||
if values == DEFAULTS:
|
||||
AutotagConfig.MODEL_TYPE = "wd14-swinv2"
|
||||
AutotagConfig.EXISTING = "skip"
|
||||
AutotagConfig.TEMPLATE = model.default_prompt
|
||||
# #431: Built-in is always the shipped text, so the fake model's short
|
||||
# instructions are a saved entry, the default of both engines (the
|
||||
# concept comparisons show it).
|
||||
AutotagConfig.INSTRUCTIONS = (
|
||||
{
|
||||
"id": FIXTURE_INSTRUCTIONS_ID,
|
||||
"name": model.default_prompt,
|
||||
"text": model.default_prompt,
|
||||
},
|
||||
)
|
||||
AutotagConfig.DEFAULT_INSTRUCTIONS = {
|
||||
"gguf": FIXTURE_INSTRUCTIONS_ID,
|
||||
"hf": FIXTURE_INSTRUCTIONS_ID,
|
||||
}
|
||||
AutotagConfig._LEGACY_TEMPLATE = None
|
||||
AutotagConfig.SKIPPED_TAGS = ()
|
||||
|
||||
|
||||
|
||||
@@ -185,9 +185,12 @@
|
||||
"monitor.scheduled",
|
||||
"revision",
|
||||
"settings",
|
||||
"settings.autotag_builtin_instructions",
|
||||
"settings.autotag_character_threshold",
|
||||
"settings.autotag_default_instructions",
|
||||
"settings.autotag_existing",
|
||||
"settings.autotag_general_threshold",
|
||||
"settings.autotag_instructions",
|
||||
"settings.autotag_model_type",
|
||||
"settings.autotag_skipped_tags",
|
||||
"settings.autotag_suggestion_limit",
|
||||
|
||||
@@ -53,8 +53,12 @@ function observed(overrides = {}, capture = {}) {
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
// #431: the saved instructions; the fixture's entry is the default of both engines.
|
||||
const SAVED_ENTRY = { id: '0123456789ab', name: 'Saved', text: DEFAULT_PROMPT };
|
||||
const models = (overrides = {}) => ({
|
||||
success: true, models: { 'wd14-swinv2': { downloaded: true } }, default_prompt: DEFAULT_PROMPT,
|
||||
success: true, models: { 'wd14-swinv2': { downloaded: true } },
|
||||
instructions: [{ id: 'builtin', name: 'Built-in', text: 'Built-in text.' }, SAVED_ENTRY],
|
||||
default_instructions: { gguf: SAVED_ENTRY.id, hf: SAVED_ENTRY.id },
|
||||
wd14_general_threshold: 0.35, wd14_character_threshold: 0.85, suggestion_limit: 10, ...overrides,
|
||||
});
|
||||
const SANDSTONE = [
|
||||
@@ -244,10 +248,13 @@ test('dialog starts on the saved model unless a preset or the user chose one', a
|
||||
});
|
||||
|
||||
test('an unreadable or malformed models read falls back to the documented defaults', async () => {
|
||||
for (const route of [() => fail(500, null), () => response(200, models({ default_prompt: 4, suggestion_limit: 0, wd14_general_threshold: 2, wd14_character_threshold: 'x' }))]) {
|
||||
for (const route of [() => fail(500, null), () => response(200, models({ instructions: 4, suggestion_limit: 0, wd14_general_threshold: 2, wd14_character_threshold: 'x' }))]) {
|
||||
const { c } = await opened({ [`GET ${MODELS}`]: route });
|
||||
assert.equal(c.state.phase, 'ready');
|
||||
assert.deepEqual({ ...c.state.options }, { general_threshold: 0.35, character_threshold: 0.85, instructions: '', suggestion_limit: 10 });
|
||||
// #431: the client's Built-in copy (D204's read failure).
|
||||
const { BUILTIN_INSTRUCTIONS } = await load();
|
||||
assert.deepEqual({ ...c.state.options }, { general_threshold: 0.35, character_threshold: 0.85, instructions: BUILTIN_INSTRUCTIONS,
|
||||
suggestion_limit: 10 });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -950,3 +957,85 @@ test('the chip hint says how many skipped tags were not suggested (#431)', async
|
||||
assert.equal(c.state.chipHint, hint, String(skipped));
|
||||
}
|
||||
});
|
||||
|
||||
// D204 (#431): the Saved instructions picker of both dialogs.
|
||||
const PHOTO_ENTRY = { id: '0123456789ac', name: 'Photo', text: 'Photo tags.' };
|
||||
const picker = (overrides = {}) => ({ [`GET ${MODELS}`]: () => response(200, models({ model_type: 'gguf',
|
||||
instructions: [{ id: 'builtin', name: 'Built-in', text: 'Built-in text.' }, SAVED_ENTRY, PHOTO_ENTRY],
|
||||
default_instructions: { gguf: SAVED_ENTRY.id, hf: PHOTO_ENTRY.id }, ...overrides })) });
|
||||
|
||||
test('the picker starts at the engine default; a pick replaces the text and an edit is one-off (D204)', async () => {
|
||||
const { c } = await opened(picker());
|
||||
assert.deepEqual([c.state.instructionId, c.state.options.instructions, c.state.instructionsEdited], [SAVED_ENTRY.id, DEFAULT_PROMPT, false]);
|
||||
const revision = c.state.optionsRevision;
|
||||
c.pickInstructions(PHOTO_ENTRY.id);
|
||||
assert.deepEqual([c.state.instructionId, c.state.options.instructions, c.state.instructionsEdited], [PHOTO_ENTRY.id, 'Photo tags.', false]);
|
||||
assert.equal(c.state.optionsRevision, revision + 1, 'a pick re-renders the textarea');
|
||||
c.setOption('instructions', 'Photo tags, please.');
|
||||
assert.equal(c.state.instructionsEdited, true);
|
||||
assert.equal(c.state.optionsRevision, revision + 1, 'typing never re-renders');
|
||||
// Choosing an entry again replaces the edit without asking; an unknown id changes nothing.
|
||||
c.pickInstructions(PHOTO_ENTRY.id);
|
||||
assert.deepEqual([c.state.options.instructions, c.state.instructionsEdited], ['Photo tags.', false]);
|
||||
c.pickInstructions('nope');
|
||||
assert.equal(c.state.instructionId, PHOTO_ENTRY.id);
|
||||
// Switching engines resets to the new engine's default and drops a one-off edit; WD14 is never "edited".
|
||||
c.setOption('instructions', 'edited');
|
||||
c.setModel('hf');
|
||||
assert.deepEqual([c.state.instructionId, c.state.options.instructions, c.state.instructionsEdited], [PHOTO_ENTRY.id, 'Photo tags.', false]);
|
||||
c.setModel('wd14-vit');
|
||||
assert.equal(c.state.instructionsEdited, false);
|
||||
c.setModel('gguf');
|
||||
assert.deepEqual([c.state.instructionId, c.state.options.instructions], [SAVED_ENTRY.id, DEFAULT_PROMPT]);
|
||||
c.dispose();
|
||||
});
|
||||
|
||||
test('a preset shows the entry with its text, else "Edited for this run" (D204)', async () => {
|
||||
const { c } = await opened(picker(), { preset: { model: 'hf', options: { instructions: DEFAULT_PROMPT } } });
|
||||
assert.deepEqual([c.state.instructionId, c.state.instructionsEdited], [SAVED_ENTRY.id, false]);
|
||||
const { c: other } = await opened(picker(), { preset: { model: 'hf', options: { instructions: 'Something else' } } });
|
||||
assert.deepEqual([other.state.instructionId, other.state.options.instructions, other.state.instructionsEdited],
|
||||
[PHOTO_ENTRY.id, 'Something else', true]);
|
||||
for (const controller of [c, other]) controller.dispose();
|
||||
});
|
||||
|
||||
test('a models read without instructions offers Built-in only with the client copy (D204)', async () => {
|
||||
const { BUILTIN_INSTRUCTIONS, readChoices } = await load();
|
||||
assert.equal(BUILTIN_INSTRUCTIONS.startsWith('Generate only comma-separated Danbooru tags'), true);
|
||||
for (const value of [null, {}, { instructions: [] }, { instructions: [{ id: 'x', name: 'X', text: 'y' }] }]) {
|
||||
assert.deepEqual(readChoices(value).entries.map(entry => entry.id), ['builtin'], JSON.stringify(value));
|
||||
}
|
||||
// A default naming no entry is Built-in.
|
||||
assert.deepEqual({ ...readChoices({ instructions: [{ id: 'builtin', name: 'Built-in', text: 't' }], default_instructions: { gguf: 'gone' } }).defaults },
|
||||
{ gguf: 'builtin', hf: 'builtin' });
|
||||
const { c } = await opened(picker({ instructions: 'broken' }));
|
||||
assert.deepEqual([c.state.instructionId, c.state.options.instructions], ['builtin', BUILTIN_INSTRUCTIONS]);
|
||||
c.dispose();
|
||||
});
|
||||
|
||||
test('the bulk options pick and reset like the Tag image dialog (D204)', async () => {
|
||||
const { createTaggingOptionsController } = await load();
|
||||
const options = createTaggingOptionsController({ api: harness(defaultRoutes(picker())).api });
|
||||
await options.open();
|
||||
assert.deepEqual([options.state.model, options.state.instructionId, options.state.options.instructions], ['gguf', SAVED_ENTRY.id, DEFAULT_PROMPT]);
|
||||
options.pickInstructions(PHOTO_ENTRY.id);
|
||||
assert.equal(options.request().options.instructions, 'Photo tags.');
|
||||
options.setOption('instructions', 'One-off.');
|
||||
assert.equal(options.state.instructionsEdited, true);
|
||||
assert.equal(options.request().options.instructions, 'One-off.');
|
||||
options.setModel('hf');
|
||||
assert.deepEqual([options.state.instructionId, options.state.instructionsEdited], [PHOTO_ENTRY.id, false]);
|
||||
options.pickInstructions('nope');
|
||||
options.dispose();
|
||||
options.pickInstructions(SAVED_ENTRY.id);
|
||||
assert.equal(options.state.instructionId, PHOTO_ENTRY.id);
|
||||
});
|
||||
|
||||
test('the client Built-in copy equals the server DEFAULT_TAG_TEMPLATE (#431)', async () => {
|
||||
const { BUILTIN_INSTRUCTIONS } = await load();
|
||||
const source = require('node:fs').readFileSync(require('node:path').join(__dirname, '../../py/tagging_defaults.py'), 'utf8');
|
||||
const block = source.slice(source.indexOf('DEFAULT_TAG_TEMPLATE = ('), source.indexOf('\n)\n', source.indexOf('DEFAULT_TAG_TEMPLATE = (')));
|
||||
const text = [...block.matchAll(/"((?:[^"\\]|\\.)*)"/g)].map(([, part]) => JSON.parse(`"${part}"`)).join('');
|
||||
assert.ok(text.length > 100);
|
||||
assert.equal(BUILTIN_INSTRUCTIONS, text);
|
||||
});
|
||||
|
||||
@@ -180,7 +180,9 @@ test('the bulk options start from the server defaults and wait for them before S
|
||||
assert.equal(options.state.canStart, false);
|
||||
assert.equal(options.state.canGenerate, false);
|
||||
const opening = options.open();
|
||||
answer({ wd14_general_threshold: 0.5, wd14_character_threshold: 0.9, default_prompt: 'Say it', suggestion_limit: 7,
|
||||
answer({ wd14_general_threshold: 0.5, wd14_character_threshold: 0.9, suggestion_limit: 7,
|
||||
instructions: [{ id: 'builtin', name: 'Built-in', text: 'Built-in text.' }, { id: '0123456789ab', name: 'Say', text: 'Say it' }],
|
||||
default_instructions: { gguf: '0123456789ab', hf: 'builtin' },
|
||||
models: { 'wd14-swinv2': { downloaded: true }, 'wd14-vit': { downloaded: false }, gguf: { downloaded: true } } });
|
||||
await opening;
|
||||
assert.equal(options.state.canStart, true);
|
||||
|
||||
@@ -0,0 +1,257 @@
|
||||
// System › Auto-tagging "Saved instructions" (#431, D203): Built-in read-only, New, Duplicate, Delete (a deleted default
|
||||
// resets to Built-in), the caps, the editor drafts and the per-engine default selects.
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const instructionsModule = import('../../web/js/workspace/system-instructions.mjs');
|
||||
const stateModule = import('../../web/js/workspace/system-state.mjs');
|
||||
const coreModule = import('../../web/js/shared/toolkit/core.js');
|
||||
|
||||
/** A minimal DOM: elements with children, attributes, listeners (aborted by their signal) and focus. */
|
||||
function fakeDOM() {
|
||||
const previous = global.document;
|
||||
class Element {
|
||||
constructor(tag) {
|
||||
this.tagName = tag.toUpperCase();
|
||||
this.children = [];
|
||||
this.attributes = {};
|
||||
this.listeners = [];
|
||||
this.dataset = {};
|
||||
this.className = '';
|
||||
this.id = '';
|
||||
this.value = '';
|
||||
this.title = '';
|
||||
this.disabled = false;
|
||||
this._text = '';
|
||||
}
|
||||
get localName() { return this.tagName.toLowerCase(); }
|
||||
get textContent() { return this._text + this.children.map(child => (typeof child === 'string' ? child : child.textContent)).join(''); }
|
||||
set textContent(text) { this.children = []; this._text = String(text); }
|
||||
append(...nodes) { for (const node of nodes) { if (typeof node !== 'string') node.parentNode = this; this.children.push(node); } }
|
||||
replaceChildren(...nodes) { this.children = []; this._text = ''; this.append(...nodes); }
|
||||
querySelectorAll(selector) {
|
||||
assert.equal(selector, 'button');
|
||||
const found = [];
|
||||
const walk = node => { for (const child of node.children) if (typeof child !== 'string') { if (child.tagName === 'BUTTON') found.push(child); walk(child); } };
|
||||
walk(this);
|
||||
return found;
|
||||
}
|
||||
focus() { global.document.activeElement = this; }
|
||||
setAttribute(name, value) { this.attributes[name] = String(value); }
|
||||
getAttribute(name) { return Object.hasOwn(this.attributes, name) ? this.attributes[name] : null; }
|
||||
removeAttribute(name) { delete this.attributes[name]; }
|
||||
addEventListener(type, callback, options = {}) {
|
||||
if (options.signal?.aborted) return;
|
||||
const entry = { type, callback };
|
||||
this.listeners.push(entry);
|
||||
options.signal?.addEventListener('abort', () => { this.listeners = this.listeners.filter(item => item !== entry); });
|
||||
}
|
||||
dispatch(type, event = {}) {
|
||||
return Promise.all(this.listeners.filter(item => item.type === type).map(({ callback }) => callback({ type, target: this, ...event })));
|
||||
}
|
||||
}
|
||||
global.document = { createElement: tag => new Element(tag), activeElement: null };
|
||||
const walk = (node, found = []) => { found.push(node); node.children.forEach(child => typeof child !== 'string' && walk(child, found)); return found; };
|
||||
return { find: (root, predicate) => walk(root).find(predicate), restore() { global.document = previous; } };
|
||||
}
|
||||
|
||||
|
||||
const PHOTO = { id: '0123456789ab', name: 'Custom template', text: 'Photo tags.' };
|
||||
const ANIME = { id: '0123456789ac', name: 'Anime', text: 'Anime tags.' };
|
||||
const HOST = { autotag_builtin_instructions: 'Built-in text.', autotag_instructions: [PHOTO, ANIME],
|
||||
autotag_default_instructions: { gguf: PHOTO.id, hf: PHOTO.id } };
|
||||
|
||||
async function mounted({ host = HOST } = {}) {
|
||||
const { savedInstructionsBlock } = await instructionsModule;
|
||||
const { createSystemDrafts } = await stateModule;
|
||||
const { createScope } = await coreModule;
|
||||
const dom = fakeDOM();
|
||||
const drafts = createSystemDrafts({
|
||||
readBrowser: () => ({}), readRuntime: () => ({}), readHost: async () => ({ values: host, revision: 'sha256:one' }),
|
||||
writeBrowser: async () => ({}), writeHost: async () => ({ success: true, persisted: true }), writeRuntime: async () => ({}),
|
||||
});
|
||||
const scope = createScope();
|
||||
let counter = 0;
|
||||
const random = bytes => bytes.map(() => (counter += 1));
|
||||
const block = savedInstructionsBlock({ scope, drafts, random });
|
||||
const root = global.document.createElement('div');
|
||||
root.append(...block.nodes);
|
||||
scope.cleanup(drafts.subscribe(block.repaint));
|
||||
block.repaint();
|
||||
await drafts.reloadHost();
|
||||
const byId = id => dom.find(root, node => node.id === id);
|
||||
const options = () => byId('instruction-list').children.map(option => [option.children[0].textContent,
|
||||
option.children[1]?.children.map(badge => badge.textContent) ?? [], option.getAttribute('aria-selected')]);
|
||||
const click = id => byId(id).dispatch('click');
|
||||
// A click on an option reaches the list's one listener (as it bubbles in a browser).
|
||||
const pick = name => byId('instruction-list').dispatch('click', { target: byId('instruction-list').children
|
||||
.find(option => option.children[0].textContent === name).children[0] });
|
||||
return { dom, drafts, scope, block, byId, options, click, pick, root };
|
||||
}
|
||||
|
||||
test('helpers: Built-in first, defaults, free names, ids and the server sentences', async () => {
|
||||
const { allEntries, defaultEntry, freeName, newId, listProblem, entryNote } = await instructionsModule;
|
||||
assert.deepEqual(allEntries(HOST).map(entry => entry.name), ['Built-in', 'Custom template', 'Anime']);
|
||||
assert.equal(allEntries(HOST)[0].text, 'Built-in text.');
|
||||
assert.equal(defaultEntry(HOST, 'hf').text, 'Photo tags.');
|
||||
assert.equal(defaultEntry({ ...HOST, autotag_default_instructions: { gguf: 'gone', hf: 'builtin' } }, 'gguf').name, 'Built-in');
|
||||
assert.equal(freeName('New instructions', allEntries(HOST)), 'New instructions');
|
||||
assert.equal(freeName('anime', allEntries(HOST)), 'anime (2)');
|
||||
assert.equal(freeName('Anime', [...allEntries(HOST), { name: 'Anime (2)' }]), 'Anime (3)');
|
||||
let calls = 0;
|
||||
const id = newId([{ id: '000000000000' }], bytes => { calls += 1; return bytes.fill(calls === 1 ? 0 : 171); });
|
||||
assert.equal(id, 'abababababab');
|
||||
assert.match(newId([]), /^[0-9a-f]{12}$/);
|
||||
const list = entries => listProblem({ autotag_instructions: entries });
|
||||
assert.equal(list([PHOTO]), null);
|
||||
assert.equal(list([{ ...PHOTO, name: ' ' }]), 'Saved instructions: entry 1 needs a name.');
|
||||
assert.equal(list([PHOTO, { ...ANIME, name: 'x'.repeat(61) }]), 'Saved instructions: entry 2 name must be at most 60 characters.');
|
||||
assert.equal(list([{ ...PHOTO, name: 'a\nb' }]), 'Saved instructions: entry 1 name must not contain a line break.');
|
||||
assert.equal(list([{ ...PHOTO, name: 'built-in' }]), 'Saved instructions: the name “Built-in” is reserved.');
|
||||
assert.equal(list([PHOTO, { ...ANIME, name: ' custom TEMPLATE ' }]), 'Saved instructions: entry 2 name “custom TEMPLATE” is already used.');
|
||||
assert.equal(list([{ ...PHOTO, text: ' ' }]), 'Saved instructions: entry 1 text must be non-empty text.');
|
||||
assert.equal(list([{ ...PHOTO, text: 'x'.repeat(4001) }]), 'Saved instructions: entry 1 text must be at most 4000 characters.');
|
||||
const long = { ...PHOTO, text: 'x'.repeat(4001) };
|
||||
assert.equal(entryNote(long, [long]), 'The text must be at most 4,000 characters.');
|
||||
});
|
||||
|
||||
test('the list shows Built-in read-only first, the saved entries and their default chips', async () => {
|
||||
const view = await mounted();
|
||||
try {
|
||||
assert.deepEqual(view.options(), [['Built-in', ['Read-only'], 'true'],
|
||||
['Custom template', ['Default for GGUF', 'Default for HuggingFace'], 'false'], ['Anime', [], 'false']]);
|
||||
assert.equal(view.byId('instructions-count').textContent, '2 of 20 saved · Built‑in is not counted.');
|
||||
assert.equal(view.byId('instruction-name').value, 'Built-in');
|
||||
assert.equal(view.byId('instruction-name').readOnly, true);
|
||||
assert.equal(view.byId('instruction-text').value, 'Built-in text.');
|
||||
assert.equal(view.byId('instruction-text').readOnly, true);
|
||||
assert.equal(view.byId('instruction-note').textContent, 'Built-in instructions can’t be changed. Duplicate them to make your own.');
|
||||
assert.equal(view.byId('instruction-delete').disabled, true);
|
||||
assert.equal(view.byId('instruction-list').getAttribute('role'), 'listbox');
|
||||
assert.equal(view.block.nodes[0].dataset.key, 'autotag_instructions');
|
||||
assert.deepEqual(view.byId('instruction-default-gguf').children.map(option => option.textContent), ['Built-in', 'Custom template', 'Anime']);
|
||||
assert.equal(view.byId('instruction-default-gguf').value, PHOTO.id);
|
||||
assert.equal(view.byId('instruction-default-hf').dataset.key, 'autotag_default_instructions');
|
||||
} finally { view.scope.dispose(); view.dom.restore(); }
|
||||
});
|
||||
|
||||
test('editing a saved entry drafts the list; its counts and note follow', async () => {
|
||||
const view = await mounted();
|
||||
try {
|
||||
await view.pick('Anime');
|
||||
assert.equal(view.byId('instruction-name').readOnly, false);
|
||||
assert.equal(view.byId('instruction-delete').disabled, false);
|
||||
view.byId('instruction-name').value = 'Anime tags';
|
||||
await view.byId('instruction-name').dispatch('input');
|
||||
view.byId('instruction-text').value = 'Tags, anime.';
|
||||
await view.byId('instruction-text').dispatch('input');
|
||||
assert.deepEqual(view.drafts.get('host', 'autotag_instructions'), [PHOTO, { ...ANIME, name: 'Anime tags', text: 'Tags, anime.' }]);
|
||||
assert.equal(view.byId('instruction-name').parentNode.children[0].children[1].textContent, '10 of 60');
|
||||
assert.equal(view.byId('instruction-text').parentNode.children[0].children[1].textContent, '12 of 4,000 characters');
|
||||
view.byId('instruction-name').value = 'custom template';
|
||||
await view.byId('instruction-name').dispatch('input');
|
||||
assert.equal(view.byId('instruction-note').textContent, 'The name “custom template” is already used.');
|
||||
// Built-in is never edited.
|
||||
await view.pick('Built-in');
|
||||
view.byId('instruction-text').value = 'changed';
|
||||
await view.byId('instruction-text').dispatch('input');
|
||||
assert.equal(view.drafts.get('host', 'autotag_instructions')[0], PHOTO);
|
||||
} finally { view.scope.dispose(); view.dom.restore(); }
|
||||
});
|
||||
|
||||
test('New and Duplicate append an entry with a fresh id and a free name; the caps disable them', async () => {
|
||||
const view = await mounted();
|
||||
try {
|
||||
await view.click('instruction-new');
|
||||
const added = view.drafts.get('host', 'autotag_instructions').at(-1);
|
||||
assert.match(added.id, /^[0-9a-f]{12}$/);
|
||||
assert.deepEqual([added.name, added.text], ['New instructions', '']);
|
||||
assert.equal(view.options().at(-1)[2], 'true');
|
||||
assert.equal(view.byId('instruction-note').textContent, 'The text must not be empty.');
|
||||
await view.pick('Built-in');
|
||||
await view.click('instruction-duplicate');
|
||||
assert.deepEqual(view.drafts.get('host', 'autotag_instructions').at(-1).name, 'Built-in copy');
|
||||
assert.equal(view.drafts.get('host', 'autotag_instructions').at(-1).text, 'Built-in text.');
|
||||
const full = await mounted({ host: { ...HOST, autotag_instructions: Array.from({ length: 20 },
|
||||
(_, i) => ({ id: i.toString(16).padStart(12, '0'), name: `n${i}`, text: 't' })) } });
|
||||
try {
|
||||
assert.equal(full.byId('instruction-new').disabled, true);
|
||||
assert.equal(full.byId('instruction-duplicate').disabled, true);
|
||||
assert.equal(full.byId('instruction-cap').hidden, false);
|
||||
await full.click('instruction-new');
|
||||
assert.equal(full.drafts.get('host', 'autotag_instructions').length, 20);
|
||||
} finally { full.scope.dispose(); }
|
||||
} finally { view.scope.dispose(); view.dom.restore(); }
|
||||
});
|
||||
|
||||
test('deleting a default resets that engine to Built-in and says so; a select change clears the note', async () => {
|
||||
const view = await mounted();
|
||||
try {
|
||||
await view.pick('Custom template');
|
||||
await view.click('instruction-delete');
|
||||
assert.deepEqual(view.drafts.get('host', 'autotag_instructions'), [ANIME]);
|
||||
assert.deepEqual(view.drafts.get('host', 'autotag_default_instructions'), { gguf: 'builtin', hf: 'builtin' });
|
||||
assert.equal(view.byId('instruction-reset').textContent, 'GGUF and HuggingFace now use Built-in.');
|
||||
assert.equal(view.options()[1][2], 'true', 'the next entry is selected');
|
||||
view.byId('instruction-default-hf').value = ANIME.id;
|
||||
await view.byId('instruction-default-hf').dispatch('change');
|
||||
assert.deepEqual(view.drafts.get('host', 'autotag_default_instructions'), { gguf: 'builtin', hf: ANIME.id });
|
||||
assert.equal(view.byId('instruction-reset').textContent, '');
|
||||
await view.click('instruction-delete');
|
||||
assert.equal(view.byId('instruction-reset').textContent, 'HuggingFace now uses Built-in.');
|
||||
assert.deepEqual(view.options().map(([name]) => name), ['Built-in']);
|
||||
} finally { view.scope.dispose(); view.dom.restore(); }
|
||||
});
|
||||
|
||||
test('arrow keys move the selection; before the read everything is disabled', async () => {
|
||||
const view = await mounted();
|
||||
try {
|
||||
const list = view.byId('instruction-list');
|
||||
await list.dispatch('keydown', { key: 'ArrowDown' });
|
||||
assert.equal(view.byId('instruction-name').value, 'Custom template');
|
||||
await list.dispatch('keydown', { key: 'End' });
|
||||
assert.equal(view.byId('instruction-name').value, 'Anime');
|
||||
await list.dispatch('keydown', { key: 'ArrowDown' });
|
||||
assert.equal(view.byId('instruction-name').value, 'Anime');
|
||||
await list.dispatch('keydown', { key: 'Home' });
|
||||
assert.equal(view.byId('instruction-name').value, 'Built-in');
|
||||
await list.dispatch('keydown', { key: 'a' });
|
||||
assert.equal(list.getAttribute('aria-activedescendant'), 'instruction-option-builtin');
|
||||
} finally { view.scope.dispose(); view.dom.restore(); }
|
||||
const unread = await mounted({ host: {} });
|
||||
try {
|
||||
assert.equal(unread.byId('instruction-new').disabled, true);
|
||||
assert.equal(unread.byId('instruction-default-gguf').disabled, true);
|
||||
} finally { unread.scope.dispose(); unread.dom.restore(); }
|
||||
});
|
||||
|
||||
test('the reset note goes once the defaults are saved; a long name is cut to fit its copy suffix', async () => {
|
||||
const view = await mounted();
|
||||
try {
|
||||
await view.pick('Custom template');
|
||||
await view.click('instruction-delete');
|
||||
assert.equal(view.byId('instruction-reset').textContent, 'GGUF and HuggingFace now use Built-in.');
|
||||
await view.drafts.save();
|
||||
assert.equal(view.byId('instruction-reset').textContent, '');
|
||||
await view.pick('Anime');
|
||||
view.byId('instruction-name').value = 'x'.repeat(60);
|
||||
await view.byId('instruction-name').dispatch('input');
|
||||
await view.click('instruction-duplicate');
|
||||
const copy = view.drafts.get('host', 'autotag_instructions').at(-1).name;
|
||||
assert.equal(copy, `${'x'.repeat(55)} copy`);
|
||||
await view.click('instruction-duplicate');
|
||||
assert.equal([...view.drafts.get('host', 'autotag_instructions').at(-1).name].length <= 60, true);
|
||||
} finally { view.scope.dispose(); view.dom.restore(); }
|
||||
});
|
||||
|
||||
test('option clicks are one listener on the list, not one per repaint', async () => {
|
||||
const view = await mounted();
|
||||
try {
|
||||
const list = view.byId('instruction-list');
|
||||
const before = list.listeners.length;
|
||||
for (let i = 0; i < 5; i += 1) await view.pick('Anime');
|
||||
assert.equal(list.listeners.length, before);
|
||||
assert.equal(view.byId('instruction-list').children.every(option => option.listeners.length === 0), true);
|
||||
assert.equal(view.byId('instruction-name').value, 'Anime');
|
||||
} finally { view.scope.dispose(); view.dom.restore(); }
|
||||
});
|
||||
@@ -21,8 +21,23 @@ test('the section lists the concept rows in its order, labels and danger marks',
|
||||
assert.deepEqual(MAINTENANCE_SURFACES.map(([heading]) => heading), surfaces.map(text => text.replace('&', '&')));
|
||||
const production = MAINTENANCE_SURFACES.flatMap(([, rows]) => rows.map(([title, description, label, , danger = false]) =>
|
||||
[title, description, label, danger]));
|
||||
assert.deepEqual(production, concept.map(([title, ...rest]) => [title.replace('&', '&'), ...rest]));
|
||||
assert.equal(production.length, 8);
|
||||
// D192 (#550, beyond the concept): "Pair category and tags" follows "Unused prompt components" in Database.
|
||||
const expected = concept.map(([title, ...rest]) => [title.replace('&', '&'), ...rest]);
|
||||
expected.splice(expected.findIndex(([title]) => title === 'Unused prompt components') + 1, 0, PAIR_ROW);
|
||||
assert.deepEqual(production, expected);
|
||||
assert.equal(production.length, 9);
|
||||
});
|
||||
|
||||
const PAIR_ROW = Object.freeze(['Pair category and tags', 'Fill an empty category or tags on prompts older Pair node runs ' +
|
||||
'saved, from their saved pairs. Only empty fields of never-edited prompts.', 'Review plan', false]);
|
||||
|
||||
test('D192: the Pair category and tags row reviews the pair_metadata plan under its title (#550)', async () => {
|
||||
const { MAINTENANCE_SURFACES } = await maintenance;
|
||||
const { OPERATION_TITLES } = await review;
|
||||
const [, database] = MAINTENANCE_SURFACES[0];
|
||||
const row = database.find(([title]) => title === PAIR_ROW[0]);
|
||||
assert.deepEqual(row, [PAIR_ROW[0], PAIR_ROW[1], PAIR_ROW[2], 'pair_metadata']);
|
||||
assert.equal(OPERATION_TITLES.pair_metadata, 'Pair category and tags');
|
||||
});
|
||||
|
||||
const PLAN = Object.freeze({
|
||||
@@ -210,3 +225,40 @@ test('a rotated-log cleanup the log overtook offers Review again (S12)', async (
|
||||
assert.deepEqual(reviewAgainOf({ kind: 'maintenance', state: 'failed', scope: { operation: 'rotated_logs', failure: 'plan-changed' } }),
|
||||
{ operation: 'rotated_logs' });
|
||||
});
|
||||
|
||||
// #550 live test: a records operation's rows have no path, only a detail ("Prompt 12: category …").
|
||||
const RECORD_JOB = Object.freeze({
|
||||
id: 'a'.repeat(32), kind: 'maintenance', title: 'Pair category and tags', state: 'finished',
|
||||
admission: 'records', cancellable: false, progress: 100, processed: 0, total: 0, status_line: 'Filled 2 prompts',
|
||||
scope: { operation: 'pair_metadata' }, counters: { changed: 2, protected: 0, errors: 0 },
|
||||
started_at: 1, finished_at: 2,
|
||||
});
|
||||
|
||||
test('#550: path-less record rows are kept with their detail; file rows are unchanged', async () => {
|
||||
const { parseDetail, filterRows } = await jobView;
|
||||
const parsed = parseDetail({ ...RECORD_JOB, outcomes: { changed: [
|
||||
{ path: '', root: '', root_index: null, detail: 'Prompt 2325: category “asian”; tags asian' },
|
||||
{ path: '', root: '', root_index: null, detail: '' },
|
||||
{ path: 'a/b.png', root: 'output', root_index: 0, detail: 'Link removed' },
|
||||
] }, log: [] });
|
||||
assert.deepEqual(parsed.outcomes.changed, [
|
||||
{ path: '', root: '', root_index: null, detail: 'Prompt 2325: category “asian”; tags asian', record: true },
|
||||
{ path: 'a/b.png', root: 'output', root_index: 0, detail: 'Link removed', record: false },
|
||||
]);
|
||||
// The filter reads a record's detail and a file's path.
|
||||
assert.deepEqual(filterRows(parsed.outcomes.changed, 'asian').map(row => row.detail),
|
||||
['Prompt 2325: category “asian”; tags asian']);
|
||||
assert.deepEqual(filterRows(parsed.outcomes.changed, 'a/b').map(row => row.path), ['a/b.png']);
|
||||
});
|
||||
|
||||
test('#550: a records operation says records, not files, for an empty group and a filter', async () => {
|
||||
const { groupCopy, EMPTY_GROUP, NO_MATCH } = await jobView;
|
||||
assert.deepEqual(groupCopy(RECORD_JOB), { empty: 'No records in this group.', noMatch: 'No listed record matches this filter.',
|
||||
placeholder: 'Filter records…' });
|
||||
for (const operation of ['duplicate_prompts', 'unused_components']) {
|
||||
assert.equal(groupCopy({ ...RECORD_JOB, scope: { operation } }).empty, 'No records in this group.');
|
||||
}
|
||||
const files = { empty: EMPTY_GROUP, noMatch: NO_MATCH, placeholder: 'Filter by file path…' };
|
||||
assert.deepEqual(groupCopy({ ...RECORD_JOB, scope: { operation: 'missing_links' } }), files);
|
||||
assert.deepEqual(groupCopy({ ...RECORD_JOB, kind: 'scan', scope: {} }), files);
|
||||
});
|
||||
|
||||
@@ -42,6 +42,8 @@ function fakeDOM() {
|
||||
this.children.push(node);
|
||||
}
|
||||
}
|
||||
replaceChildren(...nodes) { this.children = []; this._text = ''; this.append(...nodes); }
|
||||
focus() { global.document.activeElement = this; }
|
||||
remove() { if (this.parentNode) this.parentNode.children = this.parentNode.children.filter(child => child !== this); this.parentNode = null; }
|
||||
setAttribute(name, value) { this.attributes[name] = String(value); }
|
||||
getAttribute(name) { return Object.hasOwn(this.attributes, name) ? this.attributes[name] : null; }
|
||||
@@ -65,7 +67,9 @@ function fakeDOM() {
|
||||
}
|
||||
|
||||
const HOST = { autotag_model_type: 'wd14-swinv2', autotag_existing: 'skip', autotag_general_threshold: 0.35,
|
||||
autotag_character_threshold: 0.85, autotag_suggestion_limit: 10, autotag_template: 'Describe the image as tags.' };
|
||||
autotag_character_threshold: 0.85, autotag_suggestion_limit: 10, autotag_builtin_instructions: 'Built-in tags.',
|
||||
autotag_instructions: [{ id: '0123456789ab', name: 'Describe', text: 'Describe the image as tags.' }],
|
||||
autotag_default_instructions: { gguf: '0123456789ab', hf: 'builtin' } };
|
||||
const entry = (label, size, extra = {}) => ({ name: label, label, size_label: size, downloaded: true, loaded: false, ...extra });
|
||||
const MODELS = {
|
||||
success: true,
|
||||
@@ -124,7 +128,8 @@ test('the section keeps the concept surface, rows, labels and actions in order',
|
||||
for (const option of ['Skip already tagged', 'Merge with existing', 'Replace after review']) assert.ok(body.includes(option) && SOURCE.includes(`'${option}'`));
|
||||
assert.ok(body.includes('<h2>Auto-tagging</h2>') && SOURCE.includes("'h2', 'Auto-tagging'"));
|
||||
assert.ok(body.includes('One tagging configuration for images and prompt-library actions. Every result can be reviewed before applying.'));
|
||||
assert.ok(body.includes('LLM tag template') && SOURCE.includes("'LLM tag template'"));
|
||||
// D203 (#431): Saved instructions replace the concept's template field.
|
||||
assert.ok(body.includes('LLM tag template') && !SOURCE.includes("'LLM tag template'"));
|
||||
assert.ok(body.includes('"Preview tagging review", "tag-review", "", "ca-primary"'));
|
||||
// Prototype copy is never shipped.
|
||||
for (const sample of ['Simulate download', 'Simulate unload', 'Ollama']) assert.ok(!SOURCE.includes(sample), sample);
|
||||
@@ -167,27 +172,25 @@ test('controls draft their host keys; numbers as numbers, other text as typed',
|
||||
const { SETTING_KEYS, coerceNumber, draftValue } = await tagging;
|
||||
assert.deepEqual(SETTING_KEYS, { 'tag-model': 'autotag_model_type', 'tag-existing': 'autotag_existing',
|
||||
'threshold-general': 'autotag_general_threshold', 'threshold-character': 'autotag_character_threshold',
|
||||
'tag-suggestions': 'autotag_suggestion_limit', 'tag-template': 'autotag_template' });
|
||||
'tag-suggestions': 'autotag_suggestion_limit' });
|
||||
assert.deepEqual(['0.4', ' 1.5 ', '15', '', 'abc', '1e-1'].map(coerceNumber), [0.4, 1.5, 15, '', 'abc', 0.1]);
|
||||
assert.equal(draftValue('autotag_suggestion_limit', '15'), 15);
|
||||
assert.equal(draftValue('autotag_template', ' 12 '), ' 12 ');
|
||||
assert.equal(draftValue('autotag_existing', ' 12 '), ' 12 ');
|
||||
assert.equal(draftValue('autotag_existing', undefined), '');
|
||||
const { dom, drafts, scope, byId } = await mounted();
|
||||
try {
|
||||
assert.equal(byId('tag-model').value, 'wd14-swinv2');
|
||||
assert.equal(byId('threshold-general').value, '0.35');
|
||||
assert.equal(byId('tag-template').value, HOST.autotag_template);
|
||||
assert.deepEqual([byId('threshold-general').min, byId('threshold-general').max, byId('threshold-general').step], ['0', '1', '0.05']);
|
||||
assert.deepEqual([byId('tag-suggestions').min, byId('tag-suggestions').max], ['1', '100']);
|
||||
byId('tag-existing').value = 'merge'; await byId('tag-existing').dispatch('change');
|
||||
byId('threshold-general').value = '0.4'; await byId('threshold-general').dispatch('change');
|
||||
byId('tag-suggestions').value = '15'; await byId('tag-suggestions').dispatch('change');
|
||||
byId('tag-template').value = 'Tags only.'; await byId('tag-template').dispatch('input');
|
||||
assert.deepEqual(drafts.changes('host'), { autotag_existing: 'merge', autotag_general_threshold: 0.4,
|
||||
autotag_suggestion_limit: 15, autotag_template: 'Tags only.' });
|
||||
autotag_suggestion_limit: 15 });
|
||||
// Every control names its stored key, so a refused save highlights it (D92).
|
||||
for (const id of Object.keys(SETTING_KEYS)) assert.equal(byId(id).dataset.key, SETTING_KEYS[id]);
|
||||
assert.equal(byId('tag-template').dataset.setting, 'tag-template');
|
||||
assert.equal(byId('tag-template'), undefined);
|
||||
} finally { scope.dispose(); assert.equal(dom.listenerCount(), 0); dom.restore(); }
|
||||
});
|
||||
|
||||
@@ -201,7 +204,8 @@ test('opening the section reads the model status once; the select waits for it',
|
||||
assert.deepEqual(byId('tag-model').options.map(option => option.textContent),
|
||||
['WD14 SwinV2 · ~400 MB', 'WD14 ViT · ~400 MB', 'GGUF JoyCaption · ~4.5 GB', 'HuggingFace JoyCaption · ~16 GB']);
|
||||
assert.deepEqual(surface.children.map(node => node.className),
|
||||
['', '', 'setting-row', 'setting-row', 'setting-row', 'setting-row', 'setting-row', 'setting-block', 'field', 'row-actions', 'operation']);
|
||||
['', '', 'setting-row', 'setting-row', 'setting-row', 'setting-row', 'setting-row', 'setting-block', 'setting-block', 'setting-row',
|
||||
'setting-row', 'small instruction-reset', 'row-actions', 'operation']);
|
||||
} finally { scope.dispose(); dom.restore(); }
|
||||
const failed = await mounted({ modelsFail: true });
|
||||
try {
|
||||
@@ -302,7 +306,9 @@ test('Preview tagging review opens the Tag image dialog with the section’s val
|
||||
assert.deepEqual(taggingPreset(HOST), { model: 'wd14-swinv2', options: { general_threshold: 0.35, character_threshold: 0.85,
|
||||
suggestion_limit: 10 } });
|
||||
assert.deepEqual(taggingPreset({ ...HOST, autotag_model_type: 'gguf' }), { model: 'gguf',
|
||||
options: { instructions: HOST.autotag_template, suggestion_limit: 10 } });
|
||||
options: { instructions: 'Describe the image as tags.', suggestion_limit: 10 } });
|
||||
// D205 (#431): each language-model engine reads its own drafted default; Built-in's text comes from the server.
|
||||
assert.deepEqual(taggingPreset({ ...HOST, autotag_model_type: 'hf' }).options.instructions, 'Built-in tags.');
|
||||
assert.equal(draftProblem(HOST), null);
|
||||
assert.deepEqual(draftProblem({ ...HOST, autotag_general_threshold: 1.5 }),
|
||||
{ key: 'autotag_general_threshold', message: 'General-tag confidence must be between 0 and 1.' });
|
||||
@@ -319,15 +325,17 @@ test('Preview tagging review opens the Tag image dialog with the section’s val
|
||||
assert.equal(taggingPreset({ ...HOST, autotag_general_threshold: 0.1 + 0.2 }).options.general_threshold, 0.3);
|
||||
assert.equal(draftProblem({ ...HOST, autotag_suggestion_limit: 2.5 }).key, 'autotag_suggestion_limit');
|
||||
assert.equal(draftProblem({ ...HOST, autotag_suggestion_limit: 0 }, { limit: false }), null);
|
||||
assert.deepEqual(draftProblem({ ...HOST, autotag_model_type: 'hf', autotag_template: ' ' }),
|
||||
{ key: 'autotag_template', message: 'LLM tag template must be non-empty text.' });
|
||||
assert.deepEqual(draftProblem({ ...HOST, autotag_model_type: 'hf', autotag_template: 'x'.repeat(4001) }),
|
||||
{ key: 'autotag_template', message: 'LLM tag template must be at most 4000 characters.' });
|
||||
// #431: a saved entry the server would refuse holds back a language-model run, with the server's sentence.
|
||||
const entries = list => ({ ...HOST, autotag_model_type: 'hf', autotag_instructions: list });
|
||||
assert.deepEqual(draftProblem(entries([{ id: '0123456789ab', name: 'A', text: ' ' }])),
|
||||
{ key: 'autotag_instructions', message: 'Saved instructions: entry 1 text must be non-empty text.' });
|
||||
assert.deepEqual(draftProblem(entries([{ id: '0123456789ab', name: 'A', text: 'x'.repeat(4001) }])),
|
||||
{ key: 'autotag_instructions', message: 'Saved instructions: entry 1 text must be at most 4000 characters.' });
|
||||
// Characters are code points, as Python's len counts them: 4000 emoji (8000 UTF-16 units) are allowed.
|
||||
assert.equal(draftProblem({ ...HOST, autotag_model_type: 'hf', autotag_template: '🏜'.repeat(4000) }), null);
|
||||
assert.equal(draftProblem({ ...HOST, autotag_model_type: 'hf', autotag_template: '🏜'.repeat(4001) }).key, 'autotag_template');
|
||||
// A WD14 run never reads the template, nor a language model the thresholds.
|
||||
assert.equal(draftProblem({ ...HOST, autotag_template: '' }), null);
|
||||
assert.equal(draftProblem(entries([{ id: '0123456789ab', name: 'A', text: '🏜'.repeat(4000) }])), null);
|
||||
assert.equal(draftProblem(entries([{ id: '0123456789ab', name: 'A', text: '🏜'.repeat(4001) }])).key, 'autotag_instructions');
|
||||
// A WD14 run never reads the instructions, nor a language model the thresholds.
|
||||
assert.equal(draftProblem({ ...HOST, autotag_instructions: [{ id: '0123456789ab', name: '', text: '' }] }), null);
|
||||
assert.equal(draftProblem({ ...HOST, autotag_model_type: 'gguf', autotag_general_threshold: 7 }), null);
|
||||
|
||||
const none = await mounted({ image: null });
|
||||
@@ -372,9 +380,12 @@ test('the batch review grid, body and Cancel-only reasons (D134, #379, CUJ 4–5
|
||||
['Settings', 'General 0.35 · Character 0.85 · up to 10 tags per image'], ['Backup', 'Create and verify before changes']]);
|
||||
const llm = { ...HOST, autotag_model_type: 'gguf' };
|
||||
assert.deepEqual(batchGrid({ ...PLAN, model: { type: 'gguf' }, backup: { policy: 'off' }, estimate_seconds: null,
|
||||
estimate_device: null, suggestion_limit: 4 }, llm).slice(-3),
|
||||
estimate_device: null, suggestion_limit: 4 }, llm).slice(-4),
|
||||
[['Time', 'Language-model engines take longer per image. Activity shows the time left once tagging starts.'],
|
||||
['Settings', 'Template of 27 characters · up to 4 tags per image'], ['Backup', 'No safety backup (the policy is off)']]);
|
||||
['Instructions', 'Describe'], ['Settings', 'Up to 4 tags per image'], ['Backup', 'No safety backup (the policy is off)']]);
|
||||
// D205 (#431): HF shows its own default by name.
|
||||
assert.deepEqual(batchGrid({ ...PLAN, model: { type: 'hf' } }, { ...llm, autotag_model_type: 'hf' }).find(([term]) => term === 'Instructions'),
|
||||
['Instructions', 'Built-in']);
|
||||
assert.equal(batchGrid({}, HOST)[2][1], '0');
|
||||
assert.equal(batchGrid({}, HOST)[6][1], 'General 0.35 · Character 0.85');
|
||||
assert.equal(existingLabel('odd'), 'odd');
|
||||
@@ -382,7 +393,7 @@ test('the batch review grid, body and Cancel-only reasons (D134, #379, CUJ 4–5
|
||||
options: { general_threshold: 0.35, character_threshold: 0.85 } });
|
||||
// #369: a threshold a hair off the grid is sent snapped to it.
|
||||
assert.equal(batchBody({ ...HOST, autotag_character_threshold: 0.1 + 0.2 }).options.character_threshold, 0.3);
|
||||
assert.deepEqual(batchBody(llm), { model_type: 'gguf', existing: 'skip', options: { instructions: HOST.autotag_template } });
|
||||
assert.deepEqual(batchBody(llm), { model_type: 'gguf', existing: 'skip', options: { instructions: 'Describe the image as tags.' } });
|
||||
assert.equal(batchCloseNote(PLAN), null);
|
||||
assert.equal(batchCloseNote({ ...PLAN, review_required: true }), REPLACE_NOTE);
|
||||
assert.equal(REPLACE_NOTE, 'Replacing tags needs a review of each image. Use Preview tagging review, or choose Skip or Merge for a batch run.');
|
||||
|
||||
@@ -1144,8 +1144,10 @@ class TestEnvelopes(AnnotationAPITestCase):
|
||||
self.assertEqual(
|
||||
sorted(await self.json_of(resp)),
|
||||
[
|
||||
"default_instructions", # #431 adds the saved instructions
|
||||
"default_prompt",
|
||||
"existing", # #328 adds the saved batch policy and model
|
||||
"instructions",
|
||||
"loaded_model_type",
|
||||
"model_loaded",
|
||||
"model_type",
|
||||
|
||||
@@ -1536,7 +1536,8 @@ class TestSavedTaggingDefaults(AutotagAPITestCase):
|
||||
"EXISTING",
|
||||
"GENERAL_THRESHOLD",
|
||||
"CHARACTER_THRESHOLD",
|
||||
"TEMPLATE",
|
||||
"INSTRUCTIONS",
|
||||
"DEFAULT_INSTRUCTIONS",
|
||||
"SUGGESTION_LIMIT",
|
||||
):
|
||||
self.addCleanup(setattr, AutotagConfig, attr, getattr(AutotagConfig, attr))
|
||||
@@ -1557,7 +1558,11 @@ class TestSavedTaggingDefaults(AutotagAPITestCase):
|
||||
self.config.EXISTING = "replace_after_review"
|
||||
self.config.GENERAL_THRESHOLD = 0.4
|
||||
self.config.CHARACTER_THRESHOLD = 0.7
|
||||
self.config.TEMPLATE = "Saved template"
|
||||
# #431: the saved engine's default instructions.
|
||||
self.config.INSTRUCTIONS = (
|
||||
{"id": "0123456789ab", "name": "Saved", "text": "Saved template"},
|
||||
)
|
||||
self.config.DEFAULT_INSTRUCTIONS = {"gguf": "0123456789ab", "hf": "builtin"}
|
||||
self.config.SUGGESTION_LIMIT = 15
|
||||
data = await (await self.client.get("/prompt_manager/autotag/models")).json()
|
||||
self.assertEqual(
|
||||
@@ -1581,6 +1586,27 @@ class TestSavedTaggingDefaults(AutotagAPITestCase):
|
||||
"suggestion_limit": 15,
|
||||
},
|
||||
)
|
||||
# Built-in first with every text, and each engine's default.
|
||||
self.assertEqual(
|
||||
data["instructions"],
|
||||
[
|
||||
{"id": "builtin", "name": "Built-in", "text": DEFAULT_TAG_TEMPLATE},
|
||||
{"id": "0123456789ab", "name": "Saved", "text": "Saved template"},
|
||||
],
|
||||
)
|
||||
self.assertEqual(
|
||||
data["default_instructions"], {"gguf": "0123456789ab", "hf": "builtin"}
|
||||
)
|
||||
# HF reads its own default; a WD14 engine reads GGUF's.
|
||||
for model_type, prompt in (
|
||||
("hf", DEFAULT_TAG_TEMPLATE),
|
||||
("wd14-vit", "Saved template"),
|
||||
):
|
||||
self.config.MODEL_TYPE = model_type
|
||||
data = await (
|
||||
await self.client.get("/prompt_manager/autotag/models")
|
||||
).json()
|
||||
self.assertEqual(data["default_prompt"], prompt, model_type)
|
||||
|
||||
async def test_single_without_thresholds_uses_saved_ones(self):
|
||||
image = self._make_image(self.output_dir)
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
"""Pair category and tags repair through the maintenance routes (#550).
|
||||
|
||||
``pair_metadata`` is reviewed and applied like the other maintenance plans:
|
||||
``options`` must be ``{}``, the job is titled "Pair category and tags", the
|
||||
safety backup follows the ``maintenance.safety_backup`` policy, and the job's
|
||||
downloadable report lists every filled prompt, never cut at 200 rows.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
_mock_server = MagicMock()
|
||||
_mock_server.PromptServer.instance.routes = MagicMock()
|
||||
sys.modules.setdefault("server", _mock_server)
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from py.api.jobs import MAX_OUTCOME_ROWS # noqa: E402
|
||||
from py.api.maintenance import MAINTENANCE_TITLES # noqa: E402
|
||||
from py.config import PromptManagerConfig # noqa: E402
|
||||
|
||||
try:
|
||||
from tests.test_api_maintenance import WAIT, ApplyTestCase # noqa: E402
|
||||
except ImportError: # discovered with tests/ as the top-level directory
|
||||
from test_api_maintenance import WAIT, ApplyTestCase # noqa: E402
|
||||
|
||||
OPERATION = "pair_metadata"
|
||||
TITLE = "Pair category and tags"
|
||||
|
||||
|
||||
class PairMetadataAPITestCase(ApplyTestCase):
|
||||
def pair(self, positive, negative="", **metadata):
|
||||
pair_id = self.api.db.save_prompt_pair(positive, negative, **metadata)
|
||||
with self.api.db.model.get_connection() as conn:
|
||||
prompt = conn.execute(
|
||||
"SELECT positive_prompt_id FROM prompt_pairs WHERE id=?", (pair_id,)
|
||||
).fetchone()[0]
|
||||
return pair_id, prompt
|
||||
|
||||
def category(self, prompt_id):
|
||||
with self.api.db.model.get_connection() as conn:
|
||||
return conn.execute(
|
||||
"SELECT category FROM prompts WHERE id=?", (prompt_id,)
|
||||
).fetchone()[0]
|
||||
|
||||
|
||||
class TestPairMetadataRoutes(PairMetadataAPITestCase):
|
||||
async def test_preview_accepts_the_operation_and_no_options(self):
|
||||
self.assertEqual(MAINTENANCE_TITLES[OPERATION], TITLE)
|
||||
self.pair("a cat", category="portrait", tags=["a"])
|
||||
plan = await self.preview(OPERATION)
|
||||
self.assertEqual(plan["operation"], OPERATION)
|
||||
self.assertEqual(plan["scope"]["count"], 1)
|
||||
self.assertEqual(plan["backup"]["policy"], "on")
|
||||
refused = await self.preview(OPERATION, 400, options={"all": True})
|
||||
self.assertEqual(refused["code"], "invalid-request")
|
||||
|
||||
async def test_apply_fills_with_a_backup_and_names_it(self):
|
||||
_pair, prompt = self.pair("a cat", "neg", category="portrait", tags=["a", "b"])
|
||||
plan, summary, job = await self.run_apply(OPERATION)
|
||||
self.assertEqual(summary["title"], TITLE)
|
||||
self.assertEqual(summary["admission"], "records")
|
||||
self.assertEqual(job.state, "finished", job.log)
|
||||
self.assertEqual(self.category(prompt), "portrait")
|
||||
[backup] = self.backups()
|
||||
self.assertEqual(
|
||||
job.status_line,
|
||||
"Filled 1 prompt from its saved pairs (1 category, 1 tag set) · "
|
||||
f"Safety backup {backup}",
|
||||
)
|
||||
self.assertEqual(
|
||||
job.outcomes["changed"][0]["detail"],
|
||||
f"Prompt {prompt}: category “portrait”; tags a, b",
|
||||
)
|
||||
self.assertEqual(job.counters["changed"], 1)
|
||||
|
||||
async def test_protected_prompts_are_counted(self):
|
||||
_a, prompt = self.pair("conflict", "n1", category="x")
|
||||
self.pair("conflict", "n2", category="y")
|
||||
self.pair("fill me", category="portrait")
|
||||
plan, _summary, job = await self.run_apply(OPERATION)
|
||||
self.assertEqual(plan["protected"]["reasons"], ["1 where pairs disagree"])
|
||||
self.assertEqual(job.counters["protected"], 1)
|
||||
self.assertIsNone(self.category(prompt))
|
||||
# Live test (#550): the count comes with its rows, in the review's wording.
|
||||
self.assertEqual(
|
||||
job.outcomes["protected"],
|
||||
[
|
||||
{
|
||||
"path": "",
|
||||
"root": "",
|
||||
"root_index": None,
|
||||
"detail": (
|
||||
f"Prompt {prompt}: kept, pairs disagree on category: x, y"
|
||||
),
|
||||
}
|
||||
],
|
||||
)
|
||||
report = job.report()["outcomes"]["protected"]
|
||||
self.assertEqual((report["count"], report["truncated"]), (1, False))
|
||||
|
||||
async def test_report_lists_every_kept_prompt(self):
|
||||
PromptManagerConfig.SAFETY_BACKUP = False
|
||||
count = MAX_OUTCOME_ROWS + 5
|
||||
for n in range(count):
|
||||
_pair, prompt = self.pair(f"kept {n:03d}", category="portrait")
|
||||
with self.api.db.model.get_connection() as conn:
|
||||
conn.execute(
|
||||
"UPDATE prompts SET updated_at='2099-01-01T00:00:00+00:00' "
|
||||
"WHERE id=?",
|
||||
(prompt,),
|
||||
)
|
||||
_plan, _summary, job = await self.run_apply(OPERATION)
|
||||
self.assertEqual(job.counters["protected"], count)
|
||||
self.assertEqual(len(job.outcomes["protected"]), MAX_OUTCOME_ROWS)
|
||||
protected = job.report()["outcomes"]["protected"]
|
||||
self.assertEqual(
|
||||
(len(protected["rows"]), protected["truncated"]), (count, False)
|
||||
)
|
||||
self.assertTrue(
|
||||
protected["rows"][0]["detail"].endswith(
|
||||
": kept, edited after it was saved; its empty category or tags "
|
||||
"may be intentional"
|
||||
)
|
||||
)
|
||||
|
||||
async def test_policy_off_writes_without_a_backup(self):
|
||||
PromptManagerConfig.SAFETY_BACKUP = False
|
||||
_pair, prompt = self.pair("no backup", category="portrait")
|
||||
plan, _summary, job = await self.run_apply(OPERATION)
|
||||
self.assertEqual(plan["backup"]["policy"], "off")
|
||||
self.assertEqual(job.state, "finished", job.log)
|
||||
self.assertIn("No safety backup (the policy is off)", "\n".join(job.log))
|
||||
self.assertTrue(job.status_line.endswith("· No safety backup"))
|
||||
self.assertEqual(self.backups(), [])
|
||||
self.assertEqual(self.category(prompt), "portrait")
|
||||
|
||||
async def test_reviewed_with_the_policy_on_keeps_the_backup(self):
|
||||
self.pair("promise", category="portrait")
|
||||
plan = await self.preview(OPERATION)
|
||||
PromptManagerConfig.SAFETY_BACKUP = False
|
||||
body = await self.apply(plan["token"])
|
||||
job = self.api._jobs.get(body["job"]["id"])
|
||||
await asyncio.wait_for(asyncio.shield(job.task), WAIT)
|
||||
self.assertEqual(len(self.backups()), 1)
|
||||
|
||||
async def test_plan_changed_writes_nothing(self):
|
||||
pair_id, prompt = self.pair("drift", category="portrait")
|
||||
plan = await self.preview(OPERATION)
|
||||
self.api.db.update_prompt_pair_metadata(pair_id, category="landscape")
|
||||
body = await self.apply(plan["token"])
|
||||
job = self.api._jobs.get(body["job"]["id"])
|
||||
await asyncio.wait_for(asyncio.shield(job.task), WAIT)
|
||||
self.assertEqual(job.state, "failed")
|
||||
self.assertEqual(job.last_event["code"], "plan-changed")
|
||||
self.assertIsNone(self.category(prompt))
|
||||
|
||||
async def test_report_lists_every_filled_prompt(self):
|
||||
PromptManagerConfig.SAFETY_BACKUP = False
|
||||
count = MAX_OUTCOME_ROWS + 5
|
||||
for n in range(count):
|
||||
self.pair(f"bulk {n:03d}", category="portrait")
|
||||
_plan, _summary, job = await self.run_apply(OPERATION)
|
||||
self.assertEqual(job.state, "finished", job.log)
|
||||
self.assertEqual(len(job.outcomes["changed"]), MAX_OUTCOME_ROWS)
|
||||
response = await self.client.request(
|
||||
"GET", f"/prompt_manager/jobs/{job.id}/report"
|
||||
)
|
||||
self.assertEqual(response.status, 200)
|
||||
report = await response.json()
|
||||
changed = report["outcomes"]["changed"]
|
||||
self.assertEqual(changed["count"], count)
|
||||
self.assertEqual(len(changed["rows"]), count)
|
||||
self.assertFalse(changed["truncated"])
|
||||
self.assertTrue(
|
||||
all(
|
||||
row["detail"].endswith("category “portrait”") for row in changed["rows"]
|
||||
)
|
||||
)
|
||||
@@ -158,7 +158,30 @@ class TestLegacyBatch(LegacyTestCase):
|
||||
self.image("b.png", (2, 2, 2))
|
||||
await self.relist()
|
||||
await self.legacy("model_type=gguf&prompt=%20")
|
||||
self.assertEqual(self.tagger.calls[-1]["prompt"], AutotagConfig.TEMPLATE)
|
||||
self.assertEqual(
|
||||
self.tagger.calls[-1]["prompt"],
|
||||
AutotagConfig.default_instructions_text("gguf"),
|
||||
)
|
||||
# #431: a blank prompt falls back to the request engine's own default.
|
||||
saved = (AutotagConfig.INSTRUCTIONS, AutotagConfig.DEFAULT_INSTRUCTIONS)
|
||||
self.addCleanup(
|
||||
lambda: (
|
||||
setattr(AutotagConfig, "INSTRUCTIONS", saved[0]),
|
||||
setattr(AutotagConfig, "DEFAULT_INSTRUCTIONS", saved[1]),
|
||||
)
|
||||
)
|
||||
AutotagConfig.INSTRUCTIONS = (
|
||||
{"id": "0123456789ab", "name": "Photo", "text": "Photo tags."},
|
||||
)
|
||||
AutotagConfig.DEFAULT_INSTRUCTIONS = {"gguf": "builtin", "hf": "0123456789ab"}
|
||||
for model_type, expected in (
|
||||
("hf", "Photo tags."),
|
||||
("gguf", AutotagConfig.default_instructions_text("gguf")),
|
||||
):
|
||||
self.image(f"{model_type}.png", (3, 3, len(model_type)))
|
||||
await self.relist()
|
||||
await self.legacy(f"model_type={model_type}&prompt=%20&skip_tagged=false")
|
||||
self.assertEqual(self.tagger.calls[-1]["prompt"], expected, model_type)
|
||||
self.assertEqual(self.tagger.custom_prompt, "legacy prompt")
|
||||
|
||||
async def test_a_missing_backend_package_is_one_error_frame(self):
|
||||
|
||||
+13
-11
@@ -442,9 +442,11 @@ class TestAutotagConfig(unittest.TestCase):
|
||||
"EXISTING",
|
||||
"GENERAL_THRESHOLD",
|
||||
"CHARACTER_THRESHOLD",
|
||||
"TEMPLATE",
|
||||
"SUGGESTION_LIMIT",
|
||||
"SKIPPED_TAGS",
|
||||
"INSTRUCTIONS",
|
||||
"DEFAULT_INSTRUCTIONS",
|
||||
"_LEGACY_TEMPLATE",
|
||||
)
|
||||
}
|
||||
|
||||
@@ -489,16 +491,13 @@ class TestAutotagConfig(unittest.TestCase):
|
||||
"character_threshold": 0.85,
|
||||
"suggestion_limit": 10,
|
||||
"skipped_tags": [],
|
||||
"instructions": [],
|
||||
"default_instructions": {"gguf": "builtin", "hf": "builtin"},
|
||||
}
|
||||
},
|
||||
)
|
||||
# The built-in template is not written; a saved one is.
|
||||
self.config_class.TEMPLATE = "Saved."
|
||||
self.assertEqual(
|
||||
PromptManagerConfig.get_config()["autotag"]["defaults"]["template"],
|
||||
"Saved.",
|
||||
)
|
||||
self.config_class.TEMPLATE = config_module.DEFAULT_TAG_TEMPLATE
|
||||
# #431: the old template key is written only as the GGUF default's
|
||||
# mirror (test_saved_instructions covers it).
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
path = os.path.join(tmpdir, "config.json")
|
||||
PromptManagerConfig.update_config(
|
||||
@@ -526,7 +525,6 @@ class TestAutotagConfig(unittest.TestCase):
|
||||
"existing": "merge",
|
||||
"general_threshold": 0.4,
|
||||
"character_threshold": 0.7,
|
||||
"template": "Tags only.",
|
||||
"suggestion_limit": 15,
|
||||
"skipped_tags": ("blurry", "closed eyes"),
|
||||
}
|
||||
@@ -535,7 +533,6 @@ class TestAutotagConfig(unittest.TestCase):
|
||||
"existing": "skip",
|
||||
"general_threshold": 0.35,
|
||||
"character_threshold": 0.85,
|
||||
"template": config_module.DEFAULT_TAG_TEMPLATE,
|
||||
"suggestion_limit": 10,
|
||||
"skipped_tags": (),
|
||||
}
|
||||
@@ -569,7 +566,12 @@ class TestAutotagConfig(unittest.TestCase):
|
||||
k: getattr(self.config_class, attr)
|
||||
for k, (attr, _v, _d) in self.config_class._KEYS.items()
|
||||
}
|
||||
self.assertEqual(loaded, dict(good, **{key: fallback[key]}))
|
||||
# The old template is no attribute any more (#431): a bad
|
||||
# one is only warned about and imports nothing.
|
||||
expected = (
|
||||
dict(good, **{key: fallback[key]}) if key in fallback else good
|
||||
)
|
||||
self.assertEqual(loaded, expected)
|
||||
|
||||
def test_skipped_tags_load_trimmed_and_warn_once_when_damaged(self):
|
||||
"""#431: the list is kept as typed (trimmed); a damaged list is
|
||||
|
||||
@@ -490,6 +490,22 @@ class TestJobOutcomesAndLog(unittest.TestCase):
|
||||
with self.assertRaises(ValueError):
|
||||
job.record_counted("bogus", 1)
|
||||
|
||||
def test_record_counted_complete_keeps_every_row_for_the_report(self):
|
||||
# #550: a repair's record of each change is never cut at the cap.
|
||||
job = Job("maintenance", "Pair category and tags", groups=("changed", "errors"))
|
||||
rows = [
|
||||
{"path": "", "detail": f"Prompt {i}"} for i in range(MAX_OUTCOME_ROWS + 5)
|
||||
]
|
||||
job.record_counted("changed", len(rows), rows, complete=True)
|
||||
self.assertEqual(len(job.outcomes["changed"]), MAX_OUTCOME_ROWS)
|
||||
report = job.report()["outcomes"]
|
||||
self.assertEqual(len(report["changed"]["rows"]), MAX_OUTCOME_ROWS + 5)
|
||||
self.assertEqual(
|
||||
report["changed"]["rows"][-1]["detail"], f"Prompt {MAX_OUTCOME_ROWS + 4}"
|
||||
)
|
||||
self.assertFalse(report["changed"]["truncated"])
|
||||
self.assertEqual(report["errors"], {"count": 0, "rows": [], "truncated": False})
|
||||
|
||||
def test_thumbnail_job_groups(self):
|
||||
self.assertEqual(
|
||||
jobs_module.THUMBNAIL_GROUP_NAMES, ("generated", "skipped", "errors")
|
||||
|
||||
@@ -0,0 +1,539 @@
|
||||
"""Pair category and tags repair (#550): the ``pair_metadata`` maintenance plan.
|
||||
|
||||
Before #546 a Pair node run saved its category and tags on the saved pair
|
||||
only, so the positive prompt it created stayed Uncategorized and untagged.
|
||||
``pair_metadata`` reviews those prompts and fills only their empty fields
|
||||
from the prompt's non-archived pairs when every pair with a value agrees.
|
||||
It never touches a negative, a saved pair or ``updated_at``, and a prompt
|
||||
the Pair node did not create, that was edited, archived, or whose pairs
|
||||
disagree, is kept (``550-design.md``).
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from database import maintenance # noqa: E402
|
||||
from database import pair_metadata_repair as repair # noqa: E402
|
||||
|
||||
try:
|
||||
from tests.test_maintenance_plans import MaintenanceDatabaseCase # noqa: E402
|
||||
except ImportError: # discovered with tests/ as the top-level directory
|
||||
from test_maintenance_plans import MaintenanceDatabaseCase # noqa: E402
|
||||
|
||||
OPERATION = "pair_metadata"
|
||||
|
||||
|
||||
class PairMetadataCase(MaintenanceDatabaseCase):
|
||||
def pair(self, positive, negative="", **metadata):
|
||||
"""A pre-#546 Pair save: the pair gets the metadata, the prompt none."""
|
||||
pair_id = self.db.save_prompt_pair(positive, negative, **metadata)
|
||||
return pair_id, self.positive_of(pair_id)
|
||||
|
||||
def positive_of(self, pair_id):
|
||||
return self.sql(
|
||||
"SELECT positive_prompt_id FROM prompt_pairs WHERE id=?", pair_id
|
||||
)[0][0]
|
||||
|
||||
def sql(self, statement, *args):
|
||||
with self.db.model.get_connection() as conn:
|
||||
return [tuple(row) for row in conn.execute(statement, args)]
|
||||
|
||||
def run_sql(self, statement, *args):
|
||||
with self.db.model.get_connection() as conn:
|
||||
conn.execute(statement, args)
|
||||
|
||||
def category(self, prompt_id):
|
||||
return self.sql("SELECT category FROM prompts WHERE id=?", prompt_id)[0][0]
|
||||
|
||||
def tags(self, prompt_id):
|
||||
return sorted(
|
||||
row[0]
|
||||
for row in self.sql(
|
||||
"SELECT t.name FROM prompt_tags pt JOIN tags t ON t.id=pt.tag_id "
|
||||
"WHERE pt.prompt_id=?",
|
||||
prompt_id,
|
||||
)
|
||||
)
|
||||
|
||||
def preview(self, operation=OPERATION, roots=None):
|
||||
return self.db.preview_maintenance(operation, [])
|
||||
|
||||
def apply(self, draft):
|
||||
return self.db.apply_maintenance(
|
||||
draft.operation, draft.fingerprint, draft.targets, []
|
||||
)
|
||||
|
||||
def edit_stamp(self, prompt_id, updated_at="2099-01-01T00:00:00+00:00"):
|
||||
self.run_sql(
|
||||
"UPDATE prompts SET updated_at=? WHERE id=?", updated_at, prompt_id
|
||||
)
|
||||
|
||||
def details(self, draft):
|
||||
return [(row["label"], row["detail"]) for row in draft.details]
|
||||
|
||||
|
||||
class TestClassification(PairMetadataCase):
|
||||
def test_fills_empty_category_and_tags_from_the_pair(self):
|
||||
pair_id, prompt = self.pair(
|
||||
"a cat on a sofa", "blurry", category="portrait", tags=["b", "a"]
|
||||
)
|
||||
draft = self.preview()
|
||||
self.assertEqual(draft.operation, OPERATION)
|
||||
self.assertEqual(draft.targets, [[prompt, True, True]])
|
||||
self.assertEqual(draft.scope["count"], 1)
|
||||
self.assertEqual(
|
||||
draft.scope["label"], "1 prompt to fill: 1 category, 1 tag set"
|
||||
)
|
||||
self.assertEqual(draft.protected, {"count": 0, "reasons": []})
|
||||
self.assertEqual(
|
||||
draft.media,
|
||||
{
|
||||
"deletes_files": False,
|
||||
"label": "No image file or saved pair is changed.",
|
||||
},
|
||||
)
|
||||
self.assertIn(
|
||||
(
|
||||
"a cat on a sofa",
|
||||
f"Fills category “portrait” and 2 tags: a, b (pair {pair_id})",
|
||||
),
|
||||
self.details(draft),
|
||||
)
|
||||
self.assertEqual(draft.details[-1]["label"], "Record")
|
||||
self.assertEqual(draft.details[-1]["detail"], repair.RECORD_NOTE)
|
||||
|
||||
def test_empty_plan(self):
|
||||
self.save("plain prompt")
|
||||
draft = self.preview()
|
||||
self.assertEqual(draft.targets, [])
|
||||
self.assertEqual(draft.scope["count"], 0)
|
||||
self.assertEqual(
|
||||
draft.scope["label"], "No Pair prompts are missing a category or tags"
|
||||
)
|
||||
self.assertEqual(draft.details, [])
|
||||
|
||||
def test_prompt_already_complete_is_not_counted(self):
|
||||
_pair, prompt = self.pair("done", category="portrait", tags=["a"])
|
||||
self.run_sql("UPDATE prompts SET category='portrait' WHERE id=?", prompt)
|
||||
self.db.set_prompt_tags(prompt, ["a"])
|
||||
self.edit_stamp(
|
||||
prompt, self.sql("SELECT created_at FROM prompts WHERE id=?", prompt)[0][0]
|
||||
)
|
||||
self.assertEqual(self.preview().scope["count"], 0)
|
||||
|
||||
def test_whitespace_category_is_blank_on_prompt_and_pair(self):
|
||||
_p1, blank_prompt = self.pair("ws prompt", category=" portrait ")
|
||||
self.run_sql("UPDATE prompts SET category=' ' WHERE id=?", blank_prompt)
|
||||
_p2, blank_pair = self.pair("ws pair", category=" ")
|
||||
draft = self.preview()
|
||||
self.assertEqual(draft.targets, [[blank_prompt, True, False]])
|
||||
self.assertIn("“portrait”", self.details(draft)[0][1])
|
||||
self.assertNotIn(blank_pair, [t[0] for t in draft.targets])
|
||||
|
||||
def test_mixed_fields_category_fills_while_tags_conflict(self):
|
||||
_a, prompt = self.pair("mixed", "n1", category="portrait", tags=["a"])
|
||||
self.pair("mixed", "n2", category="portrait", tags=["b"])
|
||||
draft = self.preview()
|
||||
self.assertEqual(draft.targets, [[prompt, True, False]])
|
||||
self.assertEqual(draft.scope["count"], 1)
|
||||
self.assertEqual(draft.protected["count"], 1)
|
||||
self.assertEqual(draft.protected["reasons"], ["1 where pairs disagree"])
|
||||
self.assertIn(("mixed", "Kept: pairs disagree on tags"), self.details(draft))
|
||||
|
||||
def test_mixed_fields_tags_fill_while_category_conflicts(self):
|
||||
_a, prompt = self.pair("mixed2", "n1", category="portrait", tags=["a"])
|
||||
self.pair("mixed2", "n2", category="landscape", tags=["a"])
|
||||
draft = self.preview()
|
||||
self.assertEqual(draft.targets, [[prompt, False, True]])
|
||||
self.assertIn(
|
||||
("mixed2", "Kept: pairs disagree on category: landscape, portrait"),
|
||||
self.details(draft),
|
||||
)
|
||||
|
||||
def test_category_set_and_tags_empty_fills_tags_only(self):
|
||||
_a, prompt = self.pair("has cat", category="portrait", tags=["a"])
|
||||
self.run_sql("UPDATE prompts SET category='mine' WHERE id=?", prompt)
|
||||
draft = self.preview()
|
||||
self.assertEqual(draft.targets, [[prompt, False, True]])
|
||||
|
||||
def test_pairs_without_a_value_are_ignored(self):
|
||||
_a, prompt = self.pair("partial", "n1", category="portrait", tags=["a"])
|
||||
self.pair("partial", "n2") # no category, no tags
|
||||
self.assertEqual(self.preview().targets, [[prompt, True, True]])
|
||||
|
||||
def test_equal_values_across_pairs_fill(self):
|
||||
_a, prompt = self.pair("same", "n1", category="portrait", tags=["a", "b"])
|
||||
self.pair("same", "n2", category="portrait", tags=["b", "a"])
|
||||
self.assertEqual(self.preview().targets, [[prompt, True, True]])
|
||||
|
||||
def test_category_is_case_sensitive(self):
|
||||
_a, prompt = self.pair("case", "n1", category="Portrait")
|
||||
self.pair("case", "n2", category="portrait")
|
||||
draft = self.preview()
|
||||
self.assertEqual(draft.targets, [])
|
||||
self.assertEqual(draft.protected["count"], 1)
|
||||
|
||||
def test_edited_prompt_is_protected(self):
|
||||
cases = {
|
||||
"later": "2099-01-01T00:00:00+00:00",
|
||||
"null": None,
|
||||
"legacy": "2020-01-01 10:00:00",
|
||||
"garbage": "yesterday",
|
||||
}
|
||||
for name, stamp in cases.items():
|
||||
with self.subTest(name=name):
|
||||
_a, prompt = self.pair(f"edited {name}", category="portrait")
|
||||
self.edit_stamp(prompt, stamp)
|
||||
draft = self.preview()
|
||||
self.assertNotIn(prompt, [t[0] for t in draft.targets])
|
||||
self.assertIn(
|
||||
(
|
||||
f"edited {name}",
|
||||
"Kept: edited after it was saved; its empty category or "
|
||||
"tags may be intentional",
|
||||
),
|
||||
self.details(draft),
|
||||
)
|
||||
self.run_sql(
|
||||
"DELETE FROM prompt_pairs WHERE positive_prompt_id=?", prompt
|
||||
)
|
||||
|
||||
def test_first_release_stamps_microseconds_apart_are_never_edited(self):
|
||||
_a, prompt = self.pair("caa9649f", category="portrait")
|
||||
self.run_sql(
|
||||
"UPDATE prompts SET created_at=?, updated_at=? WHERE id=?",
|
||||
"2026-10-18T10:00:00.000100+00:00",
|
||||
"2026-10-18T10:00:00.000180+00:00",
|
||||
prompt,
|
||||
)
|
||||
self.run_sql(
|
||||
"UPDATE prompt_pairs SET created_at=? WHERE positive_prompt_id=?",
|
||||
"2026-10-18T10:00:00.000400+00:00",
|
||||
prompt,
|
||||
)
|
||||
self.assertEqual(self.preview().targets, [[prompt, True, False]])
|
||||
|
||||
def test_edited_beats_conflict(self):
|
||||
_a, prompt = self.pair("both", "n1", category="portrait")
|
||||
self.pair("both", "n2", category="landscape")
|
||||
self.edit_stamp(prompt)
|
||||
draft = self.preview()
|
||||
self.assertEqual(draft.protected["reasons"], ["1 edited after it was saved"])
|
||||
|
||||
def test_archived_prompt_is_protected(self):
|
||||
_a, prompt = self.pair("archived", category="portrait")
|
||||
self.run_sql(
|
||||
"INSERT INTO prompt_archive(prompt_id, archived_at) VALUES(?, ?)",
|
||||
prompt,
|
||||
"2026-10-31T00:00:00+00:00",
|
||||
)
|
||||
draft = self.preview()
|
||||
self.assertEqual(draft.targets, [])
|
||||
self.assertEqual(draft.protected["reasons"], ["1 archived in the Library"])
|
||||
self.assertIn(
|
||||
("archived", "Kept: archived in the Library"), self.details(draft)
|
||||
)
|
||||
|
||||
def test_archived_pairs_are_no_source(self):
|
||||
pair_id, prompt = self.pair("old pair", "n1", category="portrait")
|
||||
self.db.archive_prompt_pair(pair_id)
|
||||
draft = self.preview()
|
||||
self.assertEqual(draft.targets, [])
|
||||
self.assertEqual(draft.protected["count"], 0)
|
||||
# An archived pair also never disagrees with a live one.
|
||||
self.pair("old pair", "n2", category="landscape")
|
||||
self.assertEqual(self.preview().targets, [[prompt, True, False]])
|
||||
self.assertIn("landscape", self.details(self.preview())[0][1])
|
||||
|
||||
def test_prompt_not_created_by_its_pair_is_out_of_scope(self):
|
||||
prompt = self.save("saved by the node first")
|
||||
self.run_sql(
|
||||
"UPDATE prompts SET created_at=?, updated_at=? WHERE id=?",
|
||||
"2026-01-01T00:00:00+00:00",
|
||||
"2026-01-01T00:00:00+00:00",
|
||||
prompt,
|
||||
)
|
||||
self.db.save_prompt_pair("saved by the node first", category="portrait")
|
||||
draft = self.preview()
|
||||
self.assertEqual(draft.targets, [])
|
||||
self.assertEqual(draft.protected["count"], 0)
|
||||
|
||||
def test_negative_is_never_a_target_and_positive_role_sources_only(self):
|
||||
_a, both = self.pair("dual", "shared negative", category="portrait")
|
||||
neg = self.sql("SELECT id FROM prompts WHERE text='shared negative'")[0][0]
|
||||
# The negative of pair A is the positive of pair B, made later: not
|
||||
# Pair-created as a positive, so out of scope.
|
||||
self.db.save_prompt_pair("shared negative", category="landscape")
|
||||
draft = self.preview()
|
||||
self.assertEqual(draft.targets, [[both, True, False]])
|
||||
self.assertNotIn(neg, [t[0] for t in draft.targets])
|
||||
|
||||
def test_positive_first_then_negative_is_filled_from_positive_pairs_only(self):
|
||||
_a, prompt = self.pair("pos first", category="portrait")
|
||||
self.db.save_prompt_pair("another", "pos first", category="landscape")
|
||||
draft = self.preview()
|
||||
self.assertIn([prompt, True, False], draft.targets)
|
||||
self.assertIn("“portrait”", dict(self.details(draft))["pos first"])
|
||||
|
||||
def test_more_than_twenty_tags_is_protected(self):
|
||||
tags = [f"t{n:02d}" for n in range(21)]
|
||||
_a, prompt = self.pair("many tags", category="portrait", tags=tags)
|
||||
draft = self.preview()
|
||||
self.assertEqual(draft.targets, [[prompt, True, False]])
|
||||
self.assertEqual(
|
||||
draft.protected["reasons"], ["1 whose pair has more than 20 tags"]
|
||||
)
|
||||
self.assertIn(
|
||||
("many tags", "Kept: its pair has more than 20 tags"), self.details(draft)
|
||||
)
|
||||
|
||||
def test_protected_rows_come_first_within_fifty(self):
|
||||
_c, conflict = self.pair("zz conflict", "n1", category="x")
|
||||
self.pair("zz conflict", "n2", category="y")
|
||||
for n in range(60):
|
||||
self.pair(f"fill {n:02d}", category="portrait")
|
||||
draft = self.preview()
|
||||
self.assertEqual(len(draft.details), maintenance.MAX_DETAILS)
|
||||
self.assertEqual(draft.details[0]["label"], "zz conflict")
|
||||
self.assertEqual(draft.details[-1]["label"], "Record")
|
||||
self.assertEqual(draft.scope["count"], 60)
|
||||
|
||||
def test_detail_rows_list_conflicts_before_other_kept_prompts(self):
|
||||
_a, archived = self.pair("aa archived", category="portrait")
|
||||
self.run_sql(
|
||||
"INSERT INTO prompt_archive(prompt_id, archived_at) VALUES(?, ?)",
|
||||
archived,
|
||||
"2026-10-31T00:00:00+00:00",
|
||||
)
|
||||
_b, edited = self.pair("bb edited", category="portrait")
|
||||
self.edit_stamp(edited)
|
||||
self.pair("zz conflict", "n1", category="x")
|
||||
self.pair("zz conflict", "n2", category="y")
|
||||
draft = self.preview()
|
||||
self.assertEqual(
|
||||
[label for label, _detail in self.details(draft)],
|
||||
["zz conflict", "bb edited", "aa archived"],
|
||||
)
|
||||
# The reasons keep the precedence order.
|
||||
self.assertEqual(
|
||||
draft.protected["reasons"],
|
||||
[
|
||||
"1 archived in the Library",
|
||||
"1 edited after it was saved",
|
||||
"1 where pairs disagree",
|
||||
],
|
||||
)
|
||||
|
||||
def test_same_library_same_fingerprint_and_pair_use_does_not_change_it(self):
|
||||
pair_id, _prompt = self.pair("fp", category="portrait", tags=["a"])
|
||||
first = self.preview().fingerprint
|
||||
self.db.record_prompt_pair_use(pair_id)
|
||||
self.db.update_prompt_pair_metadata(pair_id, notes="a note", title="t")
|
||||
self.assertEqual(self.preview().fingerprint, first)
|
||||
|
||||
|
||||
class TestPreviewWritesNothing(PairMetadataCase):
|
||||
def test_preview_reads_one_read_only_snapshot(self):
|
||||
self.pair("cat", category="portrait", tags=["a"])
|
||||
before = self.sql("SELECT * FROM prompts ORDER BY id")
|
||||
tags = self.sql("SELECT * FROM prompt_tags ORDER BY prompt_id, tag_id")
|
||||
connect = maintenance.sqlite3.connect
|
||||
opened = []
|
||||
|
||||
def spy(*args, **kwargs):
|
||||
opened.append(args[0])
|
||||
return connect(*args, **kwargs)
|
||||
|
||||
with patch.object(maintenance.sqlite3, "connect", side_effect=spy):
|
||||
self.preview()
|
||||
self.assertEqual(len(opened), 1)
|
||||
self.assertTrue(str(opened[0]).endswith("?mode=ro"))
|
||||
self.assertEqual(self.sql("SELECT * FROM prompts ORDER BY id"), before)
|
||||
self.assertEqual(
|
||||
self.sql("SELECT * FROM prompt_tags ORDER BY prompt_id, tag_id"), tags
|
||||
)
|
||||
|
||||
|
||||
class TestApply(PairMetadataCase):
|
||||
def snapshot_pairs(self):
|
||||
return (
|
||||
self.sql("SELECT * FROM prompt_pairs ORDER BY id"),
|
||||
self.sql("SELECT * FROM prompt_pair_tags ORDER BY pair_id, tag_id"),
|
||||
)
|
||||
|
||||
def test_apply_fills_only_empty_fields_and_leaves_pairs_alone(self):
|
||||
_a, prompt = self.pair("apply me", "neg", category="portrait", tags=["a", "b"])
|
||||
_b, partial = self.pair("partial", "n1", category="portrait", tags=["a"])
|
||||
self.pair("partial", "n2", category="portrait", tags=["c"])
|
||||
neg = self.sql("SELECT id FROM prompts WHERE text='neg'")[0][0]
|
||||
pairs = self.snapshot_pairs()
|
||||
stamps = self.sql("SELECT id, created_at, updated_at FROM prompts ORDER BY id")
|
||||
state = self.sql("SELECT * FROM prompt_library_state ORDER BY prompt_id")
|
||||
tag_rows = self.sql("SELECT id, name FROM tags ORDER BY id")
|
||||
draft = self.preview()
|
||||
result = self.apply(draft)
|
||||
self.assertEqual(result["changed"], 2)
|
||||
self.assertEqual(
|
||||
result["rows"],
|
||||
[
|
||||
{"prompt_id": prompt, "category": "portrait", "tags": ["a", "b"]},
|
||||
{"prompt_id": partial, "category": "portrait"},
|
||||
],
|
||||
)
|
||||
self.assertEqual(result["categories"], 2)
|
||||
self.assertEqual(result["tag_sets"], 1)
|
||||
self.assertEqual(
|
||||
(self.category(prompt), self.tags(prompt)), ("portrait", ["a", "b"])
|
||||
)
|
||||
self.assertEqual((self.category(partial), self.tags(partial)), ("portrait", []))
|
||||
self.assertEqual((self.category(neg), self.tags(neg)), (None, []))
|
||||
self.assertEqual(self.snapshot_pairs(), pairs)
|
||||
self.assertEqual(
|
||||
self.sql("SELECT id, created_at, updated_at FROM prompts ORDER BY id"),
|
||||
stamps,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.sql("SELECT * FROM prompt_library_state ORDER BY prompt_id"), state
|
||||
)
|
||||
self.assertEqual(self.sql("SELECT id, name FROM tags ORDER BY id"), tag_rows)
|
||||
|
||||
def test_apply_returns_one_kept_row_per_protected_prompt(self):
|
||||
_a, conflict = self.pair("conflict", "n1", category="asian")
|
||||
self.pair("conflict", "n2", category="Adult")
|
||||
_b, edited = self.pair("edited", category="portrait")
|
||||
self.edit_stamp(edited)
|
||||
_c, both = self.pair(
|
||||
"both", "n1", category="x", tags=[f"t{n}" for n in range(21)]
|
||||
)
|
||||
self.pair("both", "n2", category="y", tags=[f"t{n}" for n in range(21)])
|
||||
_d, filled = self.pair("fill", category="portrait")
|
||||
draft = self.preview()
|
||||
result = self.apply(draft)
|
||||
self.assertEqual(
|
||||
result["kept"],
|
||||
[
|
||||
{
|
||||
"prompt_id": conflict,
|
||||
"reasons": ["pairs disagree on category: Adult, asian"],
|
||||
},
|
||||
{
|
||||
"prompt_id": edited,
|
||||
"reasons": [
|
||||
"edited after it was saved; its empty category or tags "
|
||||
"may be intentional"
|
||||
],
|
||||
},
|
||||
{
|
||||
"prompt_id": both,
|
||||
"reasons": [
|
||||
"pairs disagree on category: x, y",
|
||||
"its pair has more than 20 tags",
|
||||
],
|
||||
},
|
||||
],
|
||||
)
|
||||
self.assertEqual(len(result["kept"]), draft.protected["count"])
|
||||
self.assertEqual(self.category(filled), "portrait")
|
||||
|
||||
def test_kept_rows_are_never_capped(self):
|
||||
for n in range(205):
|
||||
_p, prompt = self.pair(f"kept {n:03d}", category="portrait")
|
||||
self.edit_stamp(prompt)
|
||||
result = self.apply(self.preview())
|
||||
self.assertEqual(result["changed"], 0)
|
||||
self.assertEqual(len(result["kept"]), 205)
|
||||
|
||||
def test_second_run_finds_nothing_and_a_partial_fill_completes_later(self):
|
||||
_a, prompt = self.pair("partial", "n1", category="portrait", tags=["a"])
|
||||
p2, _same = self.pair("partial", "n2", category="portrait", tags=["c"])
|
||||
self.apply(self.preview())
|
||||
again = self.preview()
|
||||
self.assertEqual(again.scope["count"], 0)
|
||||
self.assertEqual(again.protected["count"], 1) # tags still disagree
|
||||
self.db.update_prompt_pair_metadata(p2, tags=["a"])
|
||||
self.apply(self.preview())
|
||||
self.assertEqual(self.tags(prompt), ["a"])
|
||||
self.assertEqual(self.preview().protected["count"], 0)
|
||||
|
||||
def test_drift_raises_plan_changed_and_writes_nothing(self):
|
||||
def pair_category(ids):
|
||||
self.db.update_prompt_pair_metadata(ids[0], category="landscape")
|
||||
|
||||
def prompt_category(ids):
|
||||
self.run_sql("UPDATE prompts SET category='mine' WHERE id=?", ids[1])
|
||||
|
||||
def new_pair(ids):
|
||||
self.db.save_prompt_pair("drift", "other neg", category="landscape")
|
||||
|
||||
def tag_rename(ids):
|
||||
tag = self.sql("SELECT id FROM tags WHERE name='a'")[0][0]
|
||||
self.run_sql("UPDATE tags SET name='renamed' WHERE id=?", tag)
|
||||
|
||||
def edited(ids):
|
||||
self.edit_stamp(ids[1])
|
||||
|
||||
for change in (pair_category, prompt_category, new_pair, tag_rename, edited):
|
||||
with self.subTest(change=change.__name__):
|
||||
ids = self.pair("drift", "n", category="portrait", tags=["a"])
|
||||
draft = self.preview()
|
||||
change(ids)
|
||||
before = self.sql("SELECT id, category FROM prompts ORDER BY id")
|
||||
tags = self.sql("SELECT * FROM prompt_tags ORDER BY prompt_id")
|
||||
with self.assertRaises(maintenance.PlanChanged):
|
||||
self.apply(draft)
|
||||
self.assertEqual(
|
||||
self.sql("SELECT id, category FROM prompts ORDER BY id"), before
|
||||
)
|
||||
self.assertEqual(
|
||||
self.sql("SELECT * FROM prompt_tags ORDER BY prompt_id"), tags
|
||||
)
|
||||
self.run_sql("DELETE FROM prompt_pairs")
|
||||
self.run_sql("DELETE FROM prompts")
|
||||
self.run_sql("DELETE FROM tags")
|
||||
|
||||
def test_pair_run_between_preview_and_apply_still_applies(self):
|
||||
pair_id, prompt = self.pair("busy", category="portrait")
|
||||
draft = self.preview()
|
||||
self.db.record_prompt_pair_use(pair_id)
|
||||
self.apply(draft)
|
||||
self.assertEqual(self.category(prompt), "portrait")
|
||||
|
||||
def test_guard_failure_rolls_back_everything(self):
|
||||
_a, first = self.pair("first", category="portrait")
|
||||
_b, second = self.pair("second", category="portrait")
|
||||
draft = self.preview()
|
||||
real = repair._fill_categories
|
||||
|
||||
def racing(conn, fills):
|
||||
# Another writer's value appears inside the transaction.
|
||||
conn.execute("UPDATE prompts SET category='x' WHERE id=?", (second,))
|
||||
return real(conn, fills)
|
||||
|
||||
with patch.object(repair, "_fill_categories", side_effect=racing):
|
||||
with self.assertRaises(maintenance.PlanChanged):
|
||||
self.apply(draft)
|
||||
self.assertIsNone(self.category(first))
|
||||
self.assertIsNone(self.category(second))
|
||||
|
||||
def test_more_than_two_hundred_fills_are_all_returned(self):
|
||||
for n in range(205):
|
||||
self.pair(f"bulk {n:03d}", category="portrait")
|
||||
result = self.apply(self.preview())
|
||||
self.assertEqual(result["changed"], 205)
|
||||
self.assertEqual(len(result["rows"]), 205)
|
||||
|
||||
def test_batches_bind_few_ids(self):
|
||||
for n in range(5):
|
||||
self.pair(f"batch {n}", category="portrait", tags=["a"])
|
||||
with patch.object(maintenance, "ID_BATCH", 2):
|
||||
draft = self.preview()
|
||||
result = self.apply(draft)
|
||||
self.assertEqual(result["changed"], 5)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,379 @@
|
||||
"""Saved instructions with a default per language-model engine (#431, PR 2).
|
||||
|
||||
The entry and default validation, the in-memory migration of the old single
|
||||
``autotag.defaults.template`` at load (first upgrade and re-upgrade), the
|
||||
load fallbacks, and ``AutotagConfig.default_instructions_text``.
|
||||
"""
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
# config.py imports ComfyUI's server module; stand it in (as test_config does).
|
||||
_mock_server = MagicMock()
|
||||
_mock_server.PromptServer.instance.routes = MagicMock()
|
||||
sys.modules.setdefault("server", _mock_server)
|
||||
|
||||
from py.config import AutotagConfig, PromptManagerConfig # noqa: E402
|
||||
from py.tagging_defaults import ( # noqa: E402
|
||||
BUILTIN_INSTRUCTIONS_ID,
|
||||
DEFAULT_TAG_TEMPLATE,
|
||||
MAX_SAVED_INSTRUCTIONS,
|
||||
validate_default_instructions,
|
||||
validate_instructions,
|
||||
)
|
||||
|
||||
ATTRS = ("MODEL_TYPE", "INSTRUCTIONS", "DEFAULT_INSTRUCTIONS", "_LEGACY_TEMPLATE")
|
||||
|
||||
|
||||
def entry(id_, name, text="Tags only."):
|
||||
return {"id": id_, "name": name, "text": text}
|
||||
|
||||
|
||||
def sha12(text):
|
||||
return hashlib.sha256(text.encode("utf-8")).hexdigest()[:12]
|
||||
|
||||
|
||||
class TestValidation(unittest.TestCase):
|
||||
def test_entries_are_kept_with_trimmed_names(self):
|
||||
self.assertEqual(
|
||||
validate_instructions([entry("0123456789ab", " Photo ", "Tags.")]),
|
||||
[entry("0123456789ab", "Photo", "Tags.")],
|
||||
)
|
||||
self.assertEqual(validate_instructions([]), [])
|
||||
|
||||
def test_every_refusal_sentence(self):
|
||||
ok = entry("0123456789ab", "One")
|
||||
cases = (
|
||||
("x", "Saved instructions must be a list."),
|
||||
(
|
||||
[
|
||||
entry(f"{i:012x}", f"n{i}")
|
||||
for i in range(MAX_SAVED_INSTRUCTIONS + 1)
|
||||
],
|
||||
"Saved instructions: at most 20 entries besides Built-in.",
|
||||
),
|
||||
([ok, "x"], "Saved instructions: entry 2 has an invalid id."),
|
||||
(
|
||||
[entry("ABCDEF012345", "A")],
|
||||
"Saved instructions: entry 1 has an invalid id.",
|
||||
),
|
||||
([entry("builtin", "A")], "Saved instructions: entry 1 has an invalid id."),
|
||||
(
|
||||
[{"id": "0123456789ab", "name": "A"}],
|
||||
"Saved instructions: entry 1 has an invalid id.",
|
||||
),
|
||||
(
|
||||
[ok, entry("0123456789ab", "Two")],
|
||||
"Saved instructions: entry 2 repeats the id of entry 1.",
|
||||
),
|
||||
(
|
||||
[entry("0123456789ab", " ")],
|
||||
"Saved instructions: entry 1 needs a name.",
|
||||
),
|
||||
(
|
||||
[entry("0123456789ab", "x" * 61)],
|
||||
"Saved instructions: entry 1 name must be at most 60 characters.",
|
||||
),
|
||||
(
|
||||
[entry("0123456789ab", "a\nb")],
|
||||
"Saved instructions: entry 1 name must not contain a line break.",
|
||||
),
|
||||
(
|
||||
[ok, entry("0123456789ac", "one")],
|
||||
"Saved instructions: entry 2 name “one” is already used.",
|
||||
),
|
||||
(
|
||||
[entry("0123456789ab", "BUILT-IN")],
|
||||
"Saved instructions: the name “Built-in” is reserved.",
|
||||
),
|
||||
(
|
||||
[entry("0123456789ab", "A", " ")],
|
||||
"Saved instructions: entry 1 text must be non-empty text.",
|
||||
),
|
||||
(
|
||||
[entry("0123456789ab", "A", "x" * 4001)],
|
||||
"Saved instructions: entry 1 text must be at most 4000 characters.",
|
||||
),
|
||||
)
|
||||
for value, message in cases:
|
||||
with self.subTest(message=message):
|
||||
with self.assertRaises(ValueError) as caught:
|
||||
validate_instructions(value)
|
||||
self.assertEqual(str(caught.exception), message)
|
||||
|
||||
def test_defaults_name_built_in_or_a_saved_entry(self):
|
||||
saved = [entry("0123456789ab", "One")]
|
||||
self.assertEqual(
|
||||
validate_default_instructions(
|
||||
{"gguf": "builtin", "hf": "0123456789ab"}, saved
|
||||
),
|
||||
{"gguf": "builtin", "hf": "0123456789ab"},
|
||||
)
|
||||
for value, message in (
|
||||
(
|
||||
{"gguf": "ffffffffffff", "hf": "builtin"},
|
||||
"Default for GGUF JoyCaption must be one of the saved instructions.",
|
||||
),
|
||||
(
|
||||
{"gguf": "builtin", "hf": "nope"},
|
||||
"Default for HuggingFace JoyCaption must be one of the saved "
|
||||
"instructions.",
|
||||
),
|
||||
(
|
||||
{"gguf": "builtin"},
|
||||
"Default instructions must name GGUF and HuggingFace.",
|
||||
),
|
||||
(
|
||||
{"gguf": [], "hf": "builtin"},
|
||||
"Default for GGUF JoyCaption must be one of the saved instructions.",
|
||||
),
|
||||
("builtin", "Default instructions must name GGUF and HuggingFace."),
|
||||
):
|
||||
with self.subTest(value=value):
|
||||
with self.assertRaises(ValueError) as caught:
|
||||
validate_default_instructions(value, saved)
|
||||
self.assertEqual(str(caught.exception), message)
|
||||
|
||||
|
||||
class ConfigTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
saved = {attr: getattr(AutotagConfig, attr) for attr in ATTRS}
|
||||
self.addCleanup(
|
||||
lambda: [setattr(AutotagConfig, a, v) for a, v in saved.items()]
|
||||
)
|
||||
AutotagConfig.update_config({"defaults": {}})
|
||||
|
||||
def load(self, defaults, warnings=0):
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
path = os.path.join(tmpdir, "config.json")
|
||||
with open(path, "w", encoding="utf-8") as handle:
|
||||
json.dump({"autotag": {"defaults": defaults}}, handle)
|
||||
before = os.stat(path).st_mtime_ns, open(path, "rb").read()
|
||||
if warnings:
|
||||
with self.assertLogs("prompt_manager.config", level="WARNING") as logs:
|
||||
PromptManagerConfig.load_from_file(path)
|
||||
self.assertEqual(len(logs.records), warnings, logs.output)
|
||||
self.logs = [record.getMessage() for record in logs.records]
|
||||
else:
|
||||
PromptManagerConfig.load_from_file(path)
|
||||
# Nothing is written at load.
|
||||
self.assertEqual(
|
||||
(os.stat(path).st_mtime_ns, open(path, "rb").read()), before
|
||||
)
|
||||
|
||||
def state(self):
|
||||
return [dict(e) for e in AutotagConfig.INSTRUCTIONS], dict(
|
||||
AutotagConfig.DEFAULT_INSTRUCTIONS
|
||||
)
|
||||
|
||||
|
||||
class TestLoad(ConfigTestCase):
|
||||
def test_a_new_install_has_built_in_only_for_both_engines(self):
|
||||
self.load({})
|
||||
self.assertEqual(self.state(), ([], {"gguf": "builtin", "hf": "builtin"}))
|
||||
self.assertEqual(
|
||||
AutotagConfig.default_instructions_text("gguf"), DEFAULT_TAG_TEMPLATE
|
||||
)
|
||||
self.assertEqual(
|
||||
AutotagConfig.all_instructions()[0],
|
||||
{
|
||||
"id": BUILTIN_INSTRUCTIONS_ID,
|
||||
"name": "Built-in",
|
||||
"text": DEFAULT_TAG_TEMPLATE,
|
||||
},
|
||||
)
|
||||
|
||||
def test_saved_entries_and_per_engine_defaults_load(self):
|
||||
saved = [
|
||||
entry("0123456789ab", "Photo", "Photo tags."),
|
||||
entry("0123456789ac", "Anime", "Anime tags."),
|
||||
]
|
||||
self.load(
|
||||
{
|
||||
"instructions": saved,
|
||||
"default_instructions": {"gguf": "0123456789ac", "hf": "0123456789ab"},
|
||||
}
|
||||
)
|
||||
self.assertEqual(
|
||||
self.state(), (saved, {"gguf": "0123456789ac", "hf": "0123456789ab"})
|
||||
)
|
||||
self.assertEqual(AutotagConfig.default_instructions_text("gguf"), "Anime tags.")
|
||||
self.assertEqual(AutotagConfig.default_instructions_text("hf"), "Photo tags.")
|
||||
with self.assertRaises(ValueError):
|
||||
AutotagConfig.default_instructions_text("wd14-vit")
|
||||
|
||||
def test_a_dangling_default_falls_back_to_built_in_with_a_warning(self):
|
||||
saved = [entry("0123456789ab", "Photo")]
|
||||
self.load(
|
||||
{
|
||||
"instructions": saved,
|
||||
"default_instructions": {"gguf": "ffffffffffff", "hf": "0123456789ab"},
|
||||
},
|
||||
warnings=1,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.state(), (saved, {"gguf": "builtin", "hf": "0123456789ab"})
|
||||
)
|
||||
self.assertEqual(
|
||||
self.logs,
|
||||
[
|
||||
"autotag.defaults.default_instructions.gguf names no saved "
|
||||
"instructions; using Built-in"
|
||||
],
|
||||
)
|
||||
|
||||
def test_a_damaged_list_falls_back_to_built_in_only_and_the_defaults_follow(self):
|
||||
self.load(
|
||||
{
|
||||
"instructions": [
|
||||
entry("0123456789ab", "A"),
|
||||
entry("0123456789ab", "B"),
|
||||
],
|
||||
"default_instructions": {"gguf": "0123456789ab", "hf": "builtin"},
|
||||
},
|
||||
warnings=1, # one warning; the defaults follow the list silently
|
||||
)
|
||||
self.assertEqual(self.state(), ([], {"gguf": "builtin", "hf": "builtin"}))
|
||||
self.assertEqual(
|
||||
self.logs[0],
|
||||
"autotag.defaults.instructions entry 2 repeats the id of entry 1; "
|
||||
"using Built-in only",
|
||||
)
|
||||
|
||||
def test_damaged_defaults_fall_back_with_a_warning(self):
|
||||
self.load({"default_instructions": "builtin"}, warnings=1)
|
||||
self.assertEqual(self.state(), ([], {"gguf": "builtin", "hf": "builtin"}))
|
||||
|
||||
|
||||
class TestMigration(ConfigTestCase):
|
||||
CUSTOM = "My own tags, please."
|
||||
|
||||
def test_first_upgrade_makes_custom_template_the_default_for_both(self):
|
||||
self.load({"template": self.CUSTOM})
|
||||
self.assertEqual(
|
||||
self.state(),
|
||||
(
|
||||
[entry(sha12(self.CUSTOM), "Custom template", self.CUSTOM)],
|
||||
{"gguf": sha12(self.CUSTOM), "hf": sha12(self.CUSTOM)},
|
||||
),
|
||||
)
|
||||
# Deterministic: a second load gives the same ids.
|
||||
first = self.state()
|
||||
self.load({"template": self.CUSTOM})
|
||||
self.assertEqual(self.state(), first)
|
||||
|
||||
def test_a_template_equal_to_built_in_creates_no_entry(self):
|
||||
self.load({"template": DEFAULT_TAG_TEMPLATE})
|
||||
self.assertEqual(self.state(), ([], {"gguf": "builtin", "hf": "builtin"}))
|
||||
|
||||
def test_an_invalid_template_is_ignored_with_todays_warning(self):
|
||||
self.load({"template": " "}, warnings=1)
|
||||
self.assertEqual(self.state(), ([], {"gguf": "builtin", "hf": "builtin"}))
|
||||
self.assertIn("autotag.defaults.template", self.logs[0])
|
||||
|
||||
def test_a_stored_default_is_never_overwritten_by_the_first_upgrade(self):
|
||||
self.load(
|
||||
{
|
||||
"template": self.CUSTOM,
|
||||
"default_instructions": {"gguf": "builtin", "hf": "builtin"},
|
||||
},
|
||||
warnings=0,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.state(),
|
||||
(
|
||||
[entry(sha12(self.CUSTOM), "Custom template", self.CUSTOM)],
|
||||
{"gguf": "builtin", "hf": "builtin"},
|
||||
),
|
||||
)
|
||||
|
||||
def test_the_mirror_of_a_saved_default_imports_nothing(self):
|
||||
saved = [entry(sha12(self.CUSTOM), "Custom template", self.CUSTOM)]
|
||||
both = {"gguf": sha12(self.CUSTOM), "hf": "builtin"}
|
||||
self.load(
|
||||
{
|
||||
"template": self.CUSTOM,
|
||||
"instructions": saved,
|
||||
"default_instructions": both,
|
||||
}
|
||||
)
|
||||
self.assertEqual(self.state(), (saved, both))
|
||||
|
||||
def test_re_upgrade_imports_an_edit_made_on_an_older_build(self):
|
||||
saved = [entry(sha12(self.CUSTOM), "Custom template", "Edited since.")]
|
||||
both = {"gguf": sha12(self.CUSTOM), "hf": "builtin"}
|
||||
with self.assertLogs("prompt_manager.config", level="INFO") as logs:
|
||||
self.load(
|
||||
{
|
||||
"template": self.CUSTOM,
|
||||
"instructions": saved,
|
||||
"default_instructions": both,
|
||||
}
|
||||
)
|
||||
instructions, defaults = self.state()
|
||||
# The text's own hash is taken by the edited entry: the next free one.
|
||||
imported = hashlib.sha256((self.CUSTOM + "\n2").encode()).hexdigest()[:12]
|
||||
self.assertEqual(
|
||||
instructions, saved + [entry(imported, "Custom template (2)", self.CUSTOM)]
|
||||
)
|
||||
self.assertEqual(defaults, both)
|
||||
self.assertIn(
|
||||
"Imported the tagging template edited on an older version as "
|
||||
"'Custom template (2)'",
|
||||
"\n".join(logs.output),
|
||||
)
|
||||
|
||||
def test_re_upgrade_into_a_full_list_imports_nothing_and_warns(self):
|
||||
saved = [entry(f"{i:012x}", f"n{i}") for i in range(MAX_SAVED_INSTRUCTIONS)]
|
||||
self.load({"template": self.CUSTOM, "instructions": saved}, warnings=1)
|
||||
self.assertEqual(self.state()[0], saved)
|
||||
self.assertIn("full", self.logs[0])
|
||||
|
||||
|
||||
class TestGetConfig(ConfigTestCase):
|
||||
def test_get_config_writes_the_lists_and_the_gguf_mirror(self):
|
||||
AutotagConfig.INSTRUCTIONS = (entry("0123456789ab", "Photo", "Photo tags."),)
|
||||
AutotagConfig.DEFAULT_INSTRUCTIONS = {"gguf": "0123456789ab", "hf": "builtin"}
|
||||
defaults = AutotagConfig.get_config()["defaults"]
|
||||
self.assertEqual(
|
||||
defaults["instructions"], [entry("0123456789ab", "Photo", "Photo tags.")]
|
||||
)
|
||||
self.assertEqual(
|
||||
defaults["default_instructions"], {"gguf": "0123456789ab", "hf": "builtin"}
|
||||
)
|
||||
self.assertEqual(defaults["template"], "Photo tags.")
|
||||
AutotagConfig.DEFAULT_INSTRUCTIONS = {"gguf": "builtin", "hf": "0123456789ab"}
|
||||
self.assertNotIn("template", AutotagConfig.get_config()["defaults"])
|
||||
|
||||
|
||||
class TestDamagedValues(ConfigTestCase):
|
||||
def test_a_non_text_default_falls_back_with_a_warning(self):
|
||||
self.load({"default_instructions": {"gguf": [], "hf": "builtin"}}, warnings=1)
|
||||
self.assertEqual(self.state(), ([], {"gguf": "builtin", "hf": "builtin"}))
|
||||
|
||||
def test_a_damaged_list_warns_once_and_migrates_the_template_afresh(self):
|
||||
custom = "My own tags, please."
|
||||
self.load(
|
||||
{
|
||||
"template": custom,
|
||||
"instructions": "broken",
|
||||
"default_instructions": {"gguf": "0123456789ab", "hf": "builtin"},
|
||||
},
|
||||
warnings=1,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.state(),
|
||||
(
|
||||
[entry(sha12(custom), "Custom template", custom)],
|
||||
{"gguf": sha12(custom), "hf": sha12(custom)},
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,224 @@
|
||||
"""Saved instructions through POST /settings and the legacy fallbacks (#431).
|
||||
|
||||
Server-side pairing (a save carrying either key writes both, the missing one
|
||||
from the running, migrated configuration), the downgrade mirror in the old
|
||||
``template`` key, the refusals, GET /settings, and the legacy single route's
|
||||
blank-prompt fallback per request engine. The "restart" is a reload of the
|
||||
isolated config.json (``PROMPT_MANAGER_CONFIG_PATH`` in the temp tree).
|
||||
"""
|
||||
|
||||
import hashlib
|
||||
|
||||
try:
|
||||
from test_settings_sources import SourcesTestCase
|
||||
from test_autotag_legacy import LegacyTestCase
|
||||
except ImportError:
|
||||
from tests.test_settings_sources import SourcesTestCase
|
||||
from tests.test_autotag_legacy import LegacyTestCase
|
||||
|
||||
from py.config import AutotagConfig, PromptManagerConfig
|
||||
from py.tagging_defaults import DEFAULT_TAG_TEMPLATE
|
||||
|
||||
ATTRS = ("MODEL_TYPE", "INSTRUCTIONS", "DEFAULT_INSTRUCTIONS", "_LEGACY_TEMPLATE")
|
||||
SETTINGS = "/prompt_manager/settings"
|
||||
CUSTOM = "My own tags, please."
|
||||
CUSTOM_ID = hashlib.sha256(CUSTOM.encode()).hexdigest()[:12]
|
||||
|
||||
|
||||
def entry(id_, name, text):
|
||||
return {"id": id_, "name": name, "text": text}
|
||||
|
||||
|
||||
class InstructionsTestCase(SourcesTestCase):
|
||||
async def asyncSetUp(self):
|
||||
await super().asyncSetUp()
|
||||
saved = {attr: getattr(AutotagConfig, attr) for attr in ATTRS}
|
||||
self.addCleanup(
|
||||
lambda: [setattr(AutotagConfig, a, v) for a, v in saved.items()]
|
||||
)
|
||||
|
||||
def restart(self):
|
||||
PromptManagerConfig.load_from_file(self.config_path)
|
||||
|
||||
async def save(self, payload, revision=True):
|
||||
if revision:
|
||||
payload = {"expected_revision": (await self._get())["revision"], **payload}
|
||||
return await self._post(SETTINGS, payload)
|
||||
|
||||
def disk(self):
|
||||
return self._read_config()["autotag"]["defaults"]
|
||||
|
||||
|
||||
class TestMigrationPersists(InstructionsTestCase):
|
||||
async def asyncSetUp(self):
|
||||
await super().asyncSetUp()
|
||||
self._write_config(
|
||||
{"custom": {"keep": True}, "autotag": {"defaults": {"template": CUSTOM}}}
|
||||
)
|
||||
self.restart()
|
||||
|
||||
async def test_get_shows_the_migrated_entry_and_writes_nothing(self):
|
||||
before = self._config_bytes()
|
||||
settings = (await self._get())["settings"]
|
||||
self.assertEqual(
|
||||
settings["autotag_instructions"],
|
||||
[entry(CUSTOM_ID, "Custom template", CUSTOM)],
|
||||
)
|
||||
self.assertEqual(
|
||||
settings["autotag_default_instructions"],
|
||||
{"gguf": CUSTOM_ID, "hf": CUSTOM_ID},
|
||||
)
|
||||
self.assertEqual(settings["autotag_template"], CUSTOM)
|
||||
self.assertEqual(settings["autotag_builtin_instructions"], DEFAULT_TAG_TEMPLATE)
|
||||
self.assertEqual(self._config_bytes(), before)
|
||||
|
||||
async def test_a_save_of_only_the_hf_default_writes_the_migrated_list_too(self):
|
||||
status, data = await self.save(
|
||||
{"autotag_default_instructions": {"gguf": CUSTOM_ID, "hf": "builtin"}}
|
||||
)
|
||||
self.assertEqual(status, 200, data)
|
||||
disk = self.disk()
|
||||
self.assertEqual(
|
||||
disk["instructions"], [entry(CUSTOM_ID, "Custom template", CUSTOM)]
|
||||
)
|
||||
self.assertEqual(
|
||||
disk["default_instructions"], {"gguf": CUSTOM_ID, "hf": "builtin"}
|
||||
)
|
||||
self.assertEqual(disk["template"], CUSTOM)
|
||||
self.assertEqual(self._read_config()["custom"], {"keep": True})
|
||||
self.restart()
|
||||
self.assertEqual(
|
||||
(list(AutotagConfig.INSTRUCTIONS), AutotagConfig.DEFAULT_INSTRUCTIONS),
|
||||
(
|
||||
[entry(CUSTOM_ID, "Custom template", CUSTOM)],
|
||||
{"gguf": CUSTOM_ID, "hf": "builtin"},
|
||||
),
|
||||
)
|
||||
|
||||
async def test_gguf_built_in_deletes_the_mirror_and_keeps_custom_template(self):
|
||||
status, data = await self.save(
|
||||
{"autotag_default_instructions": {"gguf": "builtin", "hf": CUSTOM_ID}}
|
||||
)
|
||||
self.assertEqual(status, 200, data)
|
||||
self.assertNotIn("template", self.disk())
|
||||
self.restart()
|
||||
self.assertEqual(
|
||||
list(AutotagConfig.INSTRUCTIONS),
|
||||
[entry(CUSTOM_ID, "Custom template", CUSTOM)],
|
||||
)
|
||||
self.assertEqual(
|
||||
AutotagConfig.DEFAULT_INSTRUCTIONS, {"gguf": "builtin", "hf": CUSTOM_ID}
|
||||
)
|
||||
self.assertEqual(
|
||||
AutotagConfig.default_instructions_text("gguf"), DEFAULT_TAG_TEMPLATE
|
||||
)
|
||||
|
||||
async def test_a_save_of_only_the_list_keeps_the_defaults_that_still_exist(self):
|
||||
photo = entry("0123456789ab", "Photo", "Photo tags.")
|
||||
status, data = await self.save(
|
||||
{
|
||||
"autotag_instructions": [
|
||||
entry(CUSTOM_ID, "Custom template", CUSTOM),
|
||||
photo,
|
||||
]
|
||||
}
|
||||
)
|
||||
self.assertEqual(status, 200, data)
|
||||
self.assertEqual(
|
||||
self.disk()["default_instructions"], {"gguf": CUSTOM_ID, "hf": CUSTOM_ID}
|
||||
)
|
||||
self.assertEqual(AutotagConfig.INSTRUCTIONS[1], photo)
|
||||
# A list that drops a default without moving it is refused, nothing written.
|
||||
before = self._config_bytes()
|
||||
status, data = await self.save({"autotag_instructions": [photo]})
|
||||
self.assertEqual(status, 400, data)
|
||||
self.assertEqual(
|
||||
data["error"],
|
||||
"Default for GGUF JoyCaption must be one of the saved instructions.",
|
||||
)
|
||||
self.assertEqual(self._config_bytes(), before)
|
||||
|
||||
|
||||
class TestRefusals(InstructionsTestCase):
|
||||
async def test_the_old_template_key_is_refused(self):
|
||||
before = self._config_bytes()
|
||||
status, data = await self.save({"autotag_template": "Tags only."})
|
||||
self.assertEqual(
|
||||
(status, data["error"]),
|
||||
(
|
||||
400,
|
||||
"LLM tag template was replaced by Saved instructions. Reload the page.",
|
||||
),
|
||||
)
|
||||
self.assertEqual(self._config_bytes(), before)
|
||||
|
||||
async def test_instruction_keys_without_a_revision_are_refused(self):
|
||||
for payload in (
|
||||
{"autotag_instructions": []},
|
||||
{"autotag_default_instructions": {"gguf": "builtin", "hf": "builtin"}},
|
||||
):
|
||||
with self.subTest(payload=payload):
|
||||
status, data = await self.save(payload, revision=False)
|
||||
self.assertEqual(
|
||||
(status, data["error"]), (400, "Reload the page and try again.")
|
||||
)
|
||||
|
||||
async def test_entry_refusals_name_the_entry(self):
|
||||
status, data = await self.save(
|
||||
{
|
||||
"autotag_instructions": [
|
||||
entry("0123456789ab", "Photo", "Photo tags."),
|
||||
entry("0123456789ac", "photo", "x"),
|
||||
]
|
||||
}
|
||||
)
|
||||
self.assertEqual(
|
||||
(status, data["error"]),
|
||||
(400, "Saved instructions: entry 2 name “photo” is already used."),
|
||||
)
|
||||
|
||||
async def test_a_new_entry_can_become_a_default_in_one_save(self):
|
||||
photo = entry("0123456789ab", "Photo", "Photo tags.")
|
||||
status, data = await self.save(
|
||||
{
|
||||
"autotag_instructions": [photo],
|
||||
"autotag_default_instructions": {
|
||||
"gguf": "0123456789ab",
|
||||
"hf": "builtin",
|
||||
},
|
||||
}
|
||||
)
|
||||
self.assertEqual(status, 200, data)
|
||||
self.assertEqual(self.disk()["template"], "Photo tags.")
|
||||
settings = (await self._get())["settings"]
|
||||
self.assertEqual(settings["autotag_template"], "Photo tags.")
|
||||
self.assertEqual(AutotagConfig.default_instructions_text("gguf"), "Photo tags.")
|
||||
self.assertEqual(AutotagConfig._LEGACY_TEMPLATE, "Photo tags.")
|
||||
|
||||
|
||||
class TestLegacySingleFallback(LegacyTestCase):
|
||||
async def test_a_blank_prompt_uses_the_request_engines_default(self):
|
||||
saved = {attr: getattr(AutotagConfig, attr) for attr in ATTRS}
|
||||
self.addCleanup(
|
||||
lambda: [setattr(AutotagConfig, a, v) for a, v in saved.items()]
|
||||
)
|
||||
AutotagConfig.INSTRUCTIONS = (entry("0123456789ab", "Photo", "Photo tags."),)
|
||||
AutotagConfig.DEFAULT_INSTRUCTIONS = {"gguf": "builtin", "hf": "0123456789ab"}
|
||||
prompt = self.prompt("a prompt")
|
||||
image = self.link(prompt, self.image("a.png", (1, 2, 3), sees=["bird"]))
|
||||
for model_type, expected in (
|
||||
("hf", "Photo tags."),
|
||||
("gguf", DEFAULT_TAG_TEMPLATE),
|
||||
):
|
||||
with self.subTest(model_type=model_type):
|
||||
status, data = await self.single(
|
||||
{"image_id": image, "model_type": model_type, "prompt": " "}
|
||||
)
|
||||
self.assertEqual(status, 200, data)
|
||||
self.assertEqual(self.tagger.calls[-1]["prompt"], expected)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import unittest
|
||||
|
||||
unittest.main()
|
||||
@@ -1023,16 +1023,17 @@ class TestAutotagDefaultsSettings(SourcesTestCase):
|
||||
"EXISTING",
|
||||
"GENERAL_THRESHOLD",
|
||||
"CHARACTER_THRESHOLD",
|
||||
"TEMPLATE",
|
||||
"SUGGESTION_LIMIT",
|
||||
"SKIPPED_TAGS",
|
||||
"INSTRUCTIONS",
|
||||
"DEFAULT_INSTRUCTIONS",
|
||||
"_LEGACY_TEMPLATE",
|
||||
)
|
||||
SAVED = {
|
||||
"autotag_model_type": "gguf",
|
||||
"autotag_existing": "merge",
|
||||
"autotag_general_threshold": 0.4,
|
||||
"autotag_character_threshold": 0.8,
|
||||
"autotag_template": "Tags only.",
|
||||
"autotag_suggestion_limit": 15,
|
||||
}
|
||||
|
||||
@@ -1068,9 +1069,11 @@ class TestAutotagDefaultsSettings(SourcesTestCase):
|
||||
"EXISTING": "merge",
|
||||
"GENERAL_THRESHOLD": 0.4,
|
||||
"CHARACTER_THRESHOLD": 0.8,
|
||||
"TEMPLATE": "Tags only.",
|
||||
"SUGGESTION_LIMIT": 15,
|
||||
"SKIPPED_TAGS": (),
|
||||
"INSTRUCTIONS": (),
|
||||
"DEFAULT_INSTRUCTIONS": {"gguf": "builtin", "hf": "builtin"},
|
||||
"_LEGACY_TEMPLATE": None,
|
||||
},
|
||||
)
|
||||
settings = (await self._get())["settings"]
|
||||
@@ -1141,11 +1144,11 @@ class TestAutotagDefaultsSettings(SourcesTestCase):
|
||||
"replace",
|
||||
"Existing tags must be skip, merge or replace_after_review.",
|
||||
),
|
||||
("autotag_template", " ", "LLM tag template must be non-empty text."),
|
||||
# #431: the old template is refused whatever it holds.
|
||||
(
|
||||
"autotag_template",
|
||||
"x" * 4001,
|
||||
"LLM tag template must be at most 4000 characters.",
|
||||
"Tags only.",
|
||||
"LLM tag template was replaced by Saved instructions. Reload the page.",
|
||||
),
|
||||
(
|
||||
"autotag_suggestion_limit",
|
||||
|
||||
Reference in New Issue
Block a user