diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 2ac422705..78018cc5f 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -38,6 +38,7 @@ import { newAssistantChat, online_status, reloadCurrentChat, + refreshSwipeButtons, removeMacros, renameCharacter, renameChat, @@ -57,11 +58,11 @@ import { swipe, stopGeneration, substituteParams, - syncMesToSwipe, system_avatar, system_message_types, this_chid, updateMessageElement, + updateSwipeCounter, } from '../script.js'; import { SlashCommandParser } from './slash-commands/SlashCommandParser.js'; import { SlashCommandParserError } from './slash-commands/SlashCommandParserError.js'; @@ -4616,6 +4617,7 @@ async function echoCallback(args, value) { * @param {string} value - The swipe text to add (unnamed argument) */ async function addSwipeCallback(args, value) { + const lastMessageId = chat.length - 1; const lastMessage = chat[chat.length - 1]; if (!lastMessage) { @@ -4663,15 +4665,13 @@ async function addSwipeCallback(args, value) { const newSwipeId = lastMessage.swipes.length - 1; if (isTrueBoolean(args.switch)) { - // Make sure ad-hoc changes to extras are saved before swiping away - syncMesToSwipe(); - lastMessage.swipe_id = newSwipeId; - lastMessage.mes = lastMessage.swipes[newSwipeId]; - lastMessage.extra = structuredClone(lastMessage.swipe_info?.[newSwipeId]?.extra ?? lastMessage.extra ?? {}); + await swipe(null, SWIPE_DIRECTION.RIGHT, { source: SWIPE_SOURCE.SLASH_COMMAND, repeated: false, forceMesId: lastMessageId, forceSwipeId: newSwipeId }); + } else { + await updateSwipeCounter(lastMessageId, { message: lastMessage }); + refreshSwipeButtons(); } await saveChatConditional(); - await reloadCurrentChat(); return String(newSwipeId); } diff --git a/tests/frontend/AddSwipeNoReload.e2e.js b/tests/frontend/AddSwipeNoReload.e2e.js new file mode 100644 index 000000000..b74df32bd --- /dev/null +++ b/tests/frontend/AddSwipeNoReload.e2e.js @@ -0,0 +1,106 @@ +import { test, expect } from '@playwright/test'; +import { testSetup } from './frontent-test-utils.js'; + +test.describe('/addswipe without chat reload', () => { + test.beforeEach(testSetup.awaitST); + + test('adds swipes in place without reloading the chat', async ({ page }) => { + const result = await page.evaluate(async () => { + const { newAssistantChat } = await import('./script.js'); + const ctx = window.SillyTavern.getContext(); + const commands = ctx.SlashCommandParser.commands; + const args = { quiet: 'true' }; + const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); + + async function waitFor(condition, timeoutMs = 5000, intervalMs = 50) { + const start = Date.now(); + while (Date.now() - start < timeoutMs) { + if (condition()) return true; + await delay(intervalMs); + } + throw new Error('Timed out waiting for condition'); + } + + // A fresh account runs onboarding on load: dismiss its popup and wait for the + // welcome messages to finish arriving, so they don't land inside the test chat. + for (let i = 0; i < 10; i++) { + const okButton = document.querySelector('.popup:popover-open .popup-button-ok, dialog[open] .popup-button-ok'); + if (!okButton) break; + okButton.click(); + await delay(300); + } + const chat = ctx.chat; + let settledLength = -1; + await waitFor(() => { + const stable = chat.length === settledLength; + settledLength = chat.length; + return stable; + }, 10000, 700); + + await newAssistantChat({ temporary: true }); + const message = { + name: 'Assistant', + is_user: false, + is_system: false, + send_date: new Date().toISOString(), + mes: 'Original greeting', + extra: {}, + }; + chat.push(message); + await ctx.addOneMessage(message); + const mesId = chat.length - 1; + await waitFor(() => document.querySelector(`.mes[mesid="${mesId}"]`) !== null); + const mesDiv = document.querySelector(`.mes[mesid="${mesId}"]`); + // A full chat reload replaces the message elements, so element identity + // and ad-hoc DOM state prove the chat was updated in place. + mesDiv.dataset.tempUiState = 'alive'; + const domAlive = () => mesDiv.isConnected && mesDiv.dataset.tempUiState === 'alive'; + const counter = () => document.querySelector(`.mes[mesid="${mesId}"] .swipes-counter`) + ?.textContent?.replace(/\u200b/g, '')?.trim(); + + const noSwitch = { returned: await commands['addswipe'].callback({ ...args }, 'Added swipe A') }; + await delay(500); + Object.assign(noSwitch, { + swipes: chat[mesId].swipes.length, + swipeId: chat[mesId].swipe_id, + mes: chat[mesId].mes, + domAlive: domAlive(), + counter: counter(), + }); + + const warnings = []; + const origWarning = window.toastr.warning; + window.toastr.warning = (msg, ...rest) => { warnings.push(String(msg)); return origWarning.call(window.toastr, msg, ...rest); }; + const preSwitchState = { len: chat.length, last: { name: chat[chat.length - 1]?.name, is_user: chat[chat.length - 1]?.is_user, is_system: chat[chat.length - 1]?.is_system, mes: chat[chat.length - 1]?.mes?.slice(0, 30) } }; + const withSwitch = { returned: await commands['addswipe'].callback({ ...args, switch: 'true' }, 'Added swipe B') }; + window.toastr.warning = origWarning; + withSwitch.debug = { preSwitchState, warnings }; + await delay(1000); + Object.assign(withSwitch, { + swipes: chat[mesId].swipes.length, + swipeId: chat[mesId].swipe_id, + mes: chat[mesId].mes, + domAlive: domAlive(), + counter: counter(), + visibleText: document.querySelector(`.mes[mesid="${mesId}"] .mes_text`)?.textContent?.trim(), + }); + + return { noSwitch, withSwitch }; + }); + + expect(result.noSwitch.returned).toBe('1'); + expect(result.noSwitch.swipes).toBe(2); + expect(result.noSwitch.swipeId).toBe(0); + expect(result.noSwitch.mes).toBe('Original greeting'); + expect(result.noSwitch.domAlive).toBe(true); + expect(result.noSwitch.counter).toBe('1/2'); + + expect(result.withSwitch.returned, JSON.stringify(result.withSwitch.debug)).toBe('2'); + expect(result.withSwitch.swipes).toBe(3); + expect(result.withSwitch.swipeId).toBe(2); + expect(result.withSwitch.mes).toBe('Added swipe B'); + expect(result.withSwitch.domAlive).toBe(true); + expect(result.withSwitch.counter).toBe('3/3'); + expect(result.withSwitch.visibleText).toBe('Added swipe B'); + }); +}); diff --git a/tests/frontend/frontent-test-utils.js b/tests/frontend/frontent-test-utils.js index e1d68914d..fe01c848b 100644 --- a/tests/frontend/frontent-test-utils.js +++ b/tests/frontend/frontent-test-utils.js @@ -20,11 +20,21 @@ export const testSetup = { if (await testSetup.isLoginPage({ page })) { // eslint-disable-next-line playwright/no-networkidle await page.waitForLoadState('networkidle'); - const userSelect = page.locator('#userList .userSelect').last(); - if (await userSelect.count()) { - await userSelect.click(); + // Try accounts from last to first: clicking a password-protected account stays on the login page + const userSelects = page.locator('#userList .userSelect'); + const userCount = await userSelects.count(); + for (let i = userCount - 1; i >= 0; i--) { + await userSelects.nth(i).click(); + const loggedIn = await page + .waitForURL(url => url.toString().startsWith(baseURL) && url.pathname !== '/login', { timeout: 3000 }) + .then(() => true, () => false); + if (loggedIn) { + break; + } + } + if (await testSetup.isLoginPage({ page })) { + throw new Error('Could not log into any account without a password.'); } - await page.waitForURL(url => url.toString().startsWith(baseURL) && url.pathname !== '/login'); } await page.waitForFunction('document.getElementById("preloader") === null', { timeout: 0 }); },