mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 09:20:43 +00:00
test: share synology chat fixtures
This commit is contained in:
@@ -158,22 +158,15 @@ describe("sendFileUrl", () => {
|
||||
});
|
||||
|
||||
// Helper to mock the user_list API response for fetchChatUsers / resolveLegacyWebhookNameToChatUserId
|
||||
function mockUserListResponse(
|
||||
users: Array<{ user_id: number; username: string; nickname: string }>,
|
||||
) {
|
||||
function mockUserListResponse(users: Array<Record<string, unknown>>) {
|
||||
mockUserListResponseImpl(users, false);
|
||||
}
|
||||
|
||||
function mockUserListResponseOnce(
|
||||
users: Array<{ user_id: number; username: string; nickname: string }>,
|
||||
) {
|
||||
function mockUserListResponseOnce(users: Array<Record<string, unknown>>) {
|
||||
mockUserListResponseImpl(users, true);
|
||||
}
|
||||
|
||||
function mockUserListResponseImpl(
|
||||
users: Array<{ user_id: number; username: string; nickname: string }>,
|
||||
once: boolean,
|
||||
) {
|
||||
function mockUserListResponseImpl(users: Array<Record<string, unknown>>, once: boolean) {
|
||||
const httpsGet = vi.mocked(https.get);
|
||||
const impl: MockRequestHandler = (_url, _opts, callback) => {
|
||||
const res = createMockResponseEmitter(200);
|
||||
@@ -298,29 +291,10 @@ describe("fetchChatUsers", () => {
|
||||
installFakeTimerHarness();
|
||||
|
||||
it("filters malformed user entries while keeping valid ones", async () => {
|
||||
const httpsGet = vi.mocked(https.get);
|
||||
httpsGet.mockImplementation(((_url, _opts, callback) => {
|
||||
const res = createMockResponseEmitter(200);
|
||||
process.nextTick(() => {
|
||||
callback?.(res);
|
||||
res.emit(
|
||||
"data",
|
||||
Buffer.from(
|
||||
JSON.stringify({
|
||||
success: true,
|
||||
data: {
|
||||
users: [
|
||||
{ user_id: 4, username: "jmn67", nickname: "jmn" },
|
||||
{ user_id: "bad", username: "broken" },
|
||||
],
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
res.emit("end");
|
||||
});
|
||||
return createMockRequestEmitter();
|
||||
}) as MockRequestHandler);
|
||||
mockUserListResponse([
|
||||
{ user_id: 4, username: "jmn67", nickname: "jmn" },
|
||||
{ user_id: "bad", username: "broken" },
|
||||
]);
|
||||
|
||||
const users = await fetchChatUsers(
|
||||
"https://nas.example.com/webapi/entry.cgi?api=SYNO.Chat.External&method=chatbot&version=2&token=%22test%22",
|
||||
|
||||
@@ -33,6 +33,26 @@ const synologyChatSetupPlugin = {
|
||||
const synologyChatConfigure = createPluginSetupWizardConfigure(synologyChatSetupPlugin);
|
||||
const originalEnv = { ...process.env };
|
||||
|
||||
function createSynologySetupPrompter(params: { allowedUserIds?: string } = {}) {
|
||||
return createTestWizardPrompter({
|
||||
text: vi.fn(async ({ message }: { message: string }) => {
|
||||
if (message === "Enter Synology Chat outgoing webhook token") {
|
||||
return "synology-token";
|
||||
}
|
||||
if (message === "Incoming webhook URL") {
|
||||
return "https://nas.example.com/webapi/entry.cgi?token=incoming";
|
||||
}
|
||||
if (message === "Outgoing webhook path (optional)") {
|
||||
return "";
|
||||
}
|
||||
if (params.allowedUserIds && message === "Allowed Synology Chat user ids") {
|
||||
return params.allowedUserIds;
|
||||
}
|
||||
throw new Error(`Unexpected prompt: ${message}`);
|
||||
}) as WizardPrompter["text"],
|
||||
});
|
||||
}
|
||||
|
||||
describe("synology-chat core", () => {
|
||||
beforeEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
@@ -83,20 +103,7 @@ describe("synology-chat core", () => {
|
||||
});
|
||||
|
||||
it("configures token and incoming webhook for the default account", async () => {
|
||||
const prompter = createTestWizardPrompter({
|
||||
text: vi.fn(async ({ message }: { message: string }) => {
|
||||
if (message === "Enter Synology Chat outgoing webhook token") {
|
||||
return "synology-token";
|
||||
}
|
||||
if (message === "Incoming webhook URL") {
|
||||
return "https://nas.example.com/webapi/entry.cgi?token=incoming";
|
||||
}
|
||||
if (message === "Outgoing webhook path (optional)") {
|
||||
return "";
|
||||
}
|
||||
throw new Error(`Unexpected prompt: ${message}`);
|
||||
}) as WizardPrompter["text"],
|
||||
});
|
||||
const prompter = createSynologySetupPrompter();
|
||||
|
||||
const result = await runSetupWizardConfigure({
|
||||
configure: synologyChatConfigure,
|
||||
@@ -114,22 +121,8 @@ describe("synology-chat core", () => {
|
||||
});
|
||||
|
||||
it("records allowed user ids when setup forces allowFrom", async () => {
|
||||
const prompter = createTestWizardPrompter({
|
||||
text: vi.fn(async ({ message }: { message: string }) => {
|
||||
if (message === "Enter Synology Chat outgoing webhook token") {
|
||||
return "synology-token";
|
||||
}
|
||||
if (message === "Incoming webhook URL") {
|
||||
return "https://nas.example.com/webapi/entry.cgi?token=incoming";
|
||||
}
|
||||
if (message === "Outgoing webhook path (optional)") {
|
||||
return "";
|
||||
}
|
||||
if (message === "Allowed Synology Chat user ids") {
|
||||
return "123456, synology-chat:789012";
|
||||
}
|
||||
throw new Error(`Unexpected prompt: ${message}`);
|
||||
}) as WizardPrompter["text"],
|
||||
const prompter = createSynologySetupPrompter({
|
||||
allowedUserIds: "123456, synology-chat:789012",
|
||||
});
|
||||
|
||||
const result = await runSetupWizardConfigure({
|
||||
|
||||
Reference in New Issue
Block a user