mirror of
https://github.com/agent0ai/agent-zero.git
synced 2026-12-04 09:28:56 +00:00
Use the same file size check for picker, drop and clipboard input. Wait for all pending batches before creating a Welcome chat, sending a message or adding it to the queue. Cancel pending work when the draft changes context, and keep model setup retries on their captured attachments.
196 lines
7.6 KiB
Python
196 lines
7.6 KiB
Python
import base64
|
|
from pathlib import Path
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
|
|
import pytest
|
|
|
|
from test_chat_loading_refresh import run_module
|
|
|
|
|
|
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
|
INPUT_STORE = PROJECT_ROOT / "webui/components/chat/input/input-store.js"
|
|
INDEX_JS = PROJECT_ROOT / "webui/index.js"
|
|
|
|
|
|
@pytest.mark.skipif(not shutil.which("node"), reason="Node.js is required")
|
|
def test_welcome_placeholder_tracks_missing_credentials_without_affecting_chats() -> None:
|
|
run_module("webui/components/chat/input/input-store.js", """
|
|
const createStore = (_name, model) => model;
|
|
let welcome;
|
|
const getStore = name => name === 'welcomeStore' ? welcome : undefined;
|
|
const shortcuts = {}, fileBrowserStore = {}, messageQueueStore = {hasQueue: false};
|
|
const attachmentsStore = {attachments: []};
|
|
const chatsStore = {selected: '', selectedContext: null};
|
|
""", """
|
|
assert.equal(store.inputPlaceholder, 'Ask anything to start a new chat');
|
|
welcome = {banners: [{id: 'system-resources'}]};
|
|
assert.equal(store.inputPlaceholder, 'Ask anything to start a new chat');
|
|
welcome.banners.push({id: 'missing-api-key', dismissible: false});
|
|
assert.equal(store.inputPlaceholder, 'Ask anything to set up your models, or click Settings');
|
|
chatsStore.selected = 'chat-a';
|
|
assert.equal(store.inputPlaceholder, 'Type your message here...');
|
|
messageQueueStore.hasQueue = true;
|
|
assert.equal(store.inputPlaceholder, 'Press Enter to send queued messages');
|
|
chatsStore.selected = '';
|
|
welcome.banners = [];
|
|
assert.equal(store.inputPlaceholder, 'Ask anything to start a new chat');
|
|
""")
|
|
|
|
|
|
@pytest.mark.skipif(not shutil.which("node"), reason="Node.js is required")
|
|
def test_chat_input_keeps_separate_session_drafts() -> None:
|
|
index_source = INDEX_JS.read_text(encoding="utf-8")
|
|
set_context = index_source[index_source.index("export const setContext"):]
|
|
assert set_context.index("inputStore.setDraftContext(id);") < set_context.index("context = id;")
|
|
|
|
source = INPUT_STORE.read_text(encoding="utf-8")
|
|
source = re.sub(r"^import .*?;\n", "", source, flags=re.MULTILINE)
|
|
source = source[: source.index('const store = createStore("chatInput", model);')]
|
|
module_source = r"""
|
|
const shortcuts = {
|
|
getCurrentContextId: () => globalThis.__context,
|
|
callJsonApi: async () => ({}),
|
|
frontendNotification: () => {},
|
|
NotificationType: {},
|
|
NotificationPriority: {},
|
|
};
|
|
const fileBrowserStore = {};
|
|
const messageQueueStore = { hasQueue: false };
|
|
const attachmentsStore = {
|
|
cancelPendingFiles() {},
|
|
attachments: [],
|
|
clearAttachments() { this.attachments = []; },
|
|
};
|
|
const chatsStore = { selected: "", selectedContext: null };
|
|
""" + source + "\nexport { model, chatsStore };\n"
|
|
module_url = "data:text/javascript;base64," + base64.b64encode(
|
|
module_source.encode("utf-8")
|
|
).decode("ascii")
|
|
|
|
script = f"""
|
|
const makeStorage = () => ({{
|
|
values: new Map(),
|
|
getItem(key) {{ return this.values.get(key) ?? null; }},
|
|
setItem(key, value) {{ this.values.set(key, String(value)); }},
|
|
removeItem(key) {{ this.values.delete(key); }},
|
|
}});
|
|
globalThis.sessionStorage = makeStorage();
|
|
globalThis.localStorage = makeStorage();
|
|
globalThis.document = {{ activeElement: null, getElementById: () => null, querySelectorAll: () => [] }};
|
|
globalThis.__context = null;
|
|
|
|
const {{ model, chatsStore }} = await import({module_url!r});
|
|
const assert = (condition, message) => {{ if (!condition) throw new Error(message); }};
|
|
|
|
globalThis.__context = "chat-a";
|
|
model.setDraftContext("chat-a");
|
|
model.message = "alpha draft";
|
|
assert(sessionStorage.getItem("a0:chat-draft:chat-a") === "alpha draft", "chat A was not saved");
|
|
|
|
globalThis.__context = "chat-b";
|
|
model.setDraftContext("chat-b");
|
|
assert(model.message === "", "a new chat inherited another chat's draft");
|
|
model.message = "beta draft";
|
|
|
|
globalThis.__context = "chat-a";
|
|
model.setDraftContext("chat-a");
|
|
assert(model.message === "alpha draft", "chat A was not restored");
|
|
model.message = "";
|
|
assert(sessionStorage.getItem("a0:chat-draft:chat-a") === null, "cleared draft remained stored");
|
|
|
|
globalThis.__context = null;
|
|
model.setDraftContext("");
|
|
model.message = "welcome prompt";
|
|
chatsStore.newChat = async () => {{
|
|
globalThis.__context = "chat-new";
|
|
chatsStore.selected = "chat-new";
|
|
model.setDraftContext("chat-new");
|
|
return "chat-new";
|
|
}};
|
|
let sent = "";
|
|
globalThis.sendMessage = async () => {{ sent = model.message; }};
|
|
await model.sendMessage();
|
|
assert(sent === "welcome prompt", "creating a chat erased the Welcome prompt");
|
|
assert(sessionStorage.getItem("a0:chat-draft:chat-new") === "welcome prompt", "first prompt did not follow its new chat");
|
|
|
|
const editor = {{
|
|
style: {{ height: "240px" }},
|
|
scrollHeight: 96,
|
|
}};
|
|
model._editorEl = editor;
|
|
model.adjustTextareaHeight();
|
|
assert(editor.style.height === "96px", "composer must grow with its content");
|
|
model.unmountEditor(editor);
|
|
"""
|
|
|
|
subprocess.run(["node", "--input-type=module", "-e", script], check=True, text=True)
|
|
|
|
|
|
@pytest.mark.skipif(not shutil.which("node"), reason="Node.js is required")
|
|
def test_welcome_send_serializes_chat_creation_and_dispatch() -> None:
|
|
run_module("webui/components/chat/input/input-store.js", """
|
|
const createStore = (_name, model) => model;
|
|
const shortcuts = {}, fileBrowserStore = {}, messageQueueStore = {hasQueue: false};
|
|
const attachmentsStore = {attachments: []};
|
|
const chatsStore = {selected: '', selectedContext: null};
|
|
const document = {activeElement: null, getElementById: () => null};
|
|
""", """
|
|
store._saveDraft = () => {};
|
|
const history = [];
|
|
store._pushHistory = text => history.push(text);
|
|
store.message = 'First Welcome prompt';
|
|
let finishCreate, finishSend, creates = 0, sends = 0, stops = 0;
|
|
chatsStore.newChat = () => {
|
|
creates++;
|
|
return new Promise(resolve => { finishCreate = resolve; });
|
|
};
|
|
globalThis.sendMessage = () => {
|
|
sends++;
|
|
assert.equal(store.message, 'First Welcome prompt');
|
|
store.message = '';
|
|
chatsStore.selectedContext = {running: true};
|
|
return new Promise(resolve => { finishSend = resolve; });
|
|
};
|
|
store.stopAgent = async () => { stops++; };
|
|
let finishFiles;
|
|
attachmentsStore.pendingFiles = {ready: new Promise(resolve => { finishFiles = resolve; })};
|
|
const first = store.sendMessage();
|
|
assert.equal(creates, 0, 'Welcome must finish checking its files before creating the chat');
|
|
finishFiles(true);
|
|
attachmentsStore.pendingFiles = null;
|
|
await Promise.all([store.sendMessage(), store.activateSendButton(), store.sendMessage()]);
|
|
assert.equal(creates, 1, 'repeated sends created multiple Welcome chats');
|
|
assert.equal(sends, 0);
|
|
chatsStore.selected = 'new-chat';
|
|
store.message = '';
|
|
finishCreate('new-chat');
|
|
await Promise.resolve();
|
|
assert.equal(sends, 1);
|
|
await Promise.all([store.activateSendButton(), store.sendMessage()]);
|
|
assert.equal(stops, 0, 'a repeated send click stopped the new run before dispatch settled');
|
|
assert.equal(sends, 1);
|
|
assert.deepEqual(history, ['First Welcome prompt']);
|
|
finishSend();
|
|
await first;
|
|
await store.activateSendButton();
|
|
assert.equal(stops, 1, 'Stop was not available after sending settled');
|
|
|
|
chatsStore.selected = '';
|
|
chatsStore.selectedContext = null;
|
|
store.message = 'Retry prompt';
|
|
chatsStore.newChat = async () => { throw new Error('creation failed'); };
|
|
await assert.rejects(store.sendMessage(), /creation failed/);
|
|
assert.equal(store.message, 'Retry prompt');
|
|
chatsStore.newChat = async () => null;
|
|
await store.sendMessage();
|
|
assert.equal(sends, 1, 'a failed chat creation dispatched the prompt');
|
|
chatsStore.selected = 'retry-chat';
|
|
globalThis.sendMessage = async () => { sends++; throw new Error('dispatch failed'); };
|
|
await assert.rejects(store.sendMessage(), /dispatch failed/);
|
|
globalThis.sendMessage = async () => { sends++; };
|
|
await store.sendMessage();
|
|
assert.equal(sends, 3, 'a failed create or dispatch left sending locked');
|
|
""")
|