Skip to content

Commit dc48441

Browse files
committed
Tests for FormattedMessage
1 parent 2fe311d commit dc48441

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import { render } from '@ember/test-helpers';
2+
3+
import { module, test } from 'qunit';
4+
5+
import FormattedMessage from '@cardstack/host/components/ai-assistant/formatted-message';
6+
7+
import MonacoService from '@cardstack/host/services/monaco-service';
8+
9+
import { setupMockMatrix } from '../../helpers/mock-matrix';
10+
import { setupRenderingTest } from '../../helpers/setup';
11+
12+
module('Integration | Component | FormattedMessage', function (hooks) {
13+
setupRenderingTest(hooks);
14+
15+
let monacoService: MonacoService;
16+
17+
hooks.beforeEach(async function () {
18+
monacoService = this.owner.lookup('service:monaco-service');
19+
});
20+
21+
async function renderFormattedMessage(testScenario: any) {
22+
let monacoSDK = await monacoService.getMonacoContext();
23+
24+
await render(<template>
25+
<FormattedMessage
26+
@renderCodeBlocks={{testScenario.renderCodeBlocks}}
27+
@monacoSDK={{monacoSDK}}
28+
@html={{testScenario.html}}
29+
@isStreaming={{testScenario.isStreaming}}
30+
/>
31+
</template>);
32+
}
33+
34+
test('it renders content without monaco editor when renderCodeBlocks is false', async function (assert) {
35+
await renderFormattedMessage({
36+
renderCodeBlocks: false,
37+
html: `
38+
<p>Hey there, for Valentine's day I made you a code block!</p>
39+
<pre data-code-language="haskell">
40+
import Data.List (intercalate)
41+
main :: IO ()
42+
main = putStrLn "🖤"
43+
</pre>
44+
<p>I hope you like it!</p>
45+
`,
46+
isStreaming: false,
47+
});
48+
49+
let messageElement = this.element.querySelector('.message');
50+
51+
assert.ok(
52+
messageElement?.innerHTML.includes(
53+
`<p>Hey there, for Valentine's day I made you a code block!</p>\n<pre data-code-language="haskell">import Data.List (intercalate)\nmain :: IO ()\nmain = putStrLn "🖤"\n</pre>\n<p>I hope you like it!</p>`,
54+
),
55+
'message should render html without monaco editor',
56+
);
57+
58+
assert.dom('.monaco-editor').doesNotExist();
59+
});
60+
61+
test('it renders content with monaco editor in place of pre tags when renderCodeBlocks is true', async function (assert) {
62+
await renderFormattedMessage({
63+
renderCodeBlocks: true,
64+
html: `
65+
<p>Hey there, for Valentine's day I made you a code block!</p>
66+
<pre data-code-language="c">
67+
print("🖤")
68+
</pre>
69+
<p>I hope you like it! But here is another one!</p>
70+
<pre data-code-language="ruby">
71+
puts "💎"
72+
</pre>
73+
<p>I hope you like this one too!</p>
74+
`,
75+
isStreaming: false,
76+
});
77+
78+
let messageElement = this.element.querySelector('.message');
79+
let directChildren = messageElement?.children;
80+
81+
assert.ok(directChildren[0]?.tagName == 'P');
82+
assert.ok(
83+
directChildren[1]?.tagName == 'DIV' &&
84+
directChildren[1]?.classList.contains('code-block-actions'),
85+
);
86+
assert.ok(
87+
directChildren[2]?.tagName == 'DIV' &&
88+
directChildren[2]?.classList.contains('code-block'),
89+
);
90+
assert.ok(directChildren[3]?.tagName == 'P');
91+
assert.ok(
92+
directChildren[4]?.tagName == 'DIV' &&
93+
directChildren[4]?.classList.contains('code-block-actions'),
94+
);
95+
assert.ok(
96+
directChildren[5]?.tagName == 'DIV' &&
97+
directChildren[5]?.classList.contains('code-block'),
98+
);
99+
assert.ok(directChildren[6]?.tagName == 'P');
100+
assert.dom('.monaco-editor').exists({ count: 2 });
101+
assert.dom('pre').doesNotExist();
102+
});
103+
104+
test('it will render apply code button when code patch block is detected and file url is provided', async function (assert) {
105+
await renderFormattedMessage({
106+
renderCodeBlocks: true,
107+
html: `
108+
<pre data-code-language="css">
109+
// File url: https://my-realm-server.com/my-realm/basketball.gts
110+
<<<<<<< SEARCH
111+
background: #2ecc71;
112+
=======
113+
background: #ff7f24;
114+
>>>>>>> REPLACE
115+
</pre>`,
116+
isStreaming: false,
117+
});
118+
119+
assert.dom('[data-test-apply-code-button]').exists();
120+
});
121+
122+
test('it will not render apply code button when code is streaming, code patch block is detected and file url is provided', async function (assert) {
123+
await renderFormattedMessage({
124+
renderCodeBlocks: true,
125+
html: `
126+
<pre data-code-language="css">
127+
// File url: https://my-realm-server.com/my-realm/basketball.gts
128+
<<<<<<< SEARCH
129+
background: #2ecc71;
130+
=======
131+
background: #ff7f24;
132+
>>>>>>> REPLACE
133+
</pre>`,
134+
isStreaming: true,
135+
});
136+
137+
assert.dom('[data-test-apply-code-button]').doesNotExist();
138+
});
139+
140+
test('it will not render apply code button when code patch block is detected but no file url is provided', async function (assert) {
141+
await renderFormattedMessage({
142+
renderCodeBlocks: true,
143+
html: `
144+
<pre data-code-language="css">
145+
<<<<<<< SEARCH
146+
background: #2ecc71;
147+
=======
148+
background: #ff7f24;
149+
>>>>>>> REPLACE
150+
</pre>`,
151+
isStreaming: false,
152+
});
153+
154+
assert.dom('[data-test-apply-code-button]').doesNotExist();
155+
});
156+
});

0 commit comments

Comments
 (0)