Skip to content

Commit bf549f7

Browse files
committed
test: cover chatPrefillFromUser hook handler
Direct-import test of static/js/index.js's chatPrefillFromUser export with a stubbed window.clientVars. Five cases: - AI author + configured trigger -> trigger string returned - AI author + custom trigger -> custom string returned - AI author with trigger unset -> falls back to "@ai " - Non-AI author -> returns undefined so core uses its default - clientVars.ep_ai_chat missing -> returns undefined (graceful) The browser-side behavior (chat opens, input is filled, hover style on the AI row) is covered by the matching Playwright spec in core PR #7660 — this test fills the unit-level gap so we can verify the substitution logic without standing up a full browser.
1 parent f3c8702 commit bf549f7

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict';
2+
3+
import {strict as assert} from 'assert';
4+
5+
/**
6+
* The chatPrefillFromUser client hook — exercised here as a plain
7+
* function (it's a CommonJS export from static/js/index.js) with a
8+
* stubbed `window.clientVars`. Lets us cover the AI-vs-human branch
9+
* without standing up a browser.
10+
*
11+
* The companion frontend test in Etherpad core (#7660) covers what
12+
* the browser actually does with the returned string.
13+
*/
14+
15+
const Module = require('module');
16+
17+
// The static/js client bundle requires nothing Node-specific, so we
18+
// can require it directly. But it reads `window.clientVars`; install
19+
// a global shim before requiring.
20+
const installWindowShim = (clientVars: any) => {
21+
// @ts-ignore
22+
global.window = {clientVars};
23+
};
24+
25+
const clearWindowShim = () => {
26+
// @ts-ignore
27+
delete global.window;
28+
// Drop the client module from the require cache so a follow-up
29+
// require() re-reads it under the new shim.
30+
const path = require.resolve('../../../../static/js/index');
31+
delete Module._cache[path];
32+
};
33+
34+
describe('ep_ai_chat - chatPrefillFromUser hook handler', function () {
35+
afterEach(() => clearWindowShim());
36+
37+
it('returns the configured trigger when the clicked user is the AI', function (done) {
38+
installWindowShim({ep_ai_chat: {trigger: '@ai', authorId: 'a.ai_42'}});
39+
const {chatPrefillFromUser} = require('../../../../static/js/index');
40+
chatPrefillFromUser('chatPrefillFromUser', {
41+
authorId: 'a.ai_42',
42+
name: 'AI Assistant',
43+
prefill: '@AI_Assistant ',
44+
}, (out: any) => {
45+
assert.equal(out, '@ai ');
46+
done();
47+
});
48+
});
49+
50+
it('respects a custom trigger string', function (done) {
51+
installWindowShim({ep_ai_chat: {trigger: '@bot', authorId: 'a.ai_99'}});
52+
const {chatPrefillFromUser} = require('../../../../static/js/index');
53+
chatPrefillFromUser('chatPrefillFromUser', {
54+
authorId: 'a.ai_99',
55+
name: 'AI Assistant',
56+
prefill: '@AI_Assistant ',
57+
}, (out: any) => {
58+
assert.equal(out, '@bot ');
59+
done();
60+
});
61+
});
62+
63+
it('falls back to default ("@ai ") if trigger is missing', function (done) {
64+
installWindowShim({ep_ai_chat: {authorId: 'a.ai_42'}});
65+
const {chatPrefillFromUser} = require('../../../../static/js/index');
66+
chatPrefillFromUser('chatPrefillFromUser', {
67+
authorId: 'a.ai_42', name: 'AI Assistant', prefill: '@AI_Assistant ',
68+
}, (out: any) => {
69+
assert.equal(out, '@ai ');
70+
done();
71+
});
72+
});
73+
74+
it('returns nothing for a non-AI author so core uses its default', function (done) {
75+
installWindowShim({ep_ai_chat: {trigger: '@ai', authorId: 'a.ai_42'}});
76+
const {chatPrefillFromUser} = require('../../../../static/js/index');
77+
chatPrefillFromUser('chatPrefillFromUser', {
78+
authorId: 'a.alice_1', name: 'Alice', prefill: '@Alice ',
79+
}, (out: any) => {
80+
assert.equal(out, undefined);
81+
done();
82+
});
83+
});
84+
85+
it('returns nothing when clientVars.ep_ai_chat is missing', function (done) {
86+
installWindowShim({});
87+
const {chatPrefillFromUser} = require('../../../../static/js/index');
88+
chatPrefillFromUser('chatPrefillFromUser', {
89+
authorId: 'a.ai_42', name: 'AI Assistant', prefill: '@AI_Assistant ',
90+
}, (out: any) => {
91+
assert.equal(out, undefined);
92+
done();
93+
});
94+
});
95+
});

0 commit comments

Comments
 (0)