Skip to content

Commit f91fe10

Browse files
committed
add test for html validation
1 parent d85274d commit f91fe10

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

tests/unit/ChatGPTTranslatorTest.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ describe('ChatGPTTranslator.performTranslation', () => {
1818
const validTranslation = '[it] Hello ${name}!';
1919
const invalidTranslation = '[it] Hello name!'; // missing ${...}
2020

21+
const originalHTML = '<img src="photo.jpg" alt="A dog" class="photo">';
22+
const validHTMLTranslation = '<img src="photo.jpg" alt="Un chien" class="photo">';
23+
const invalidHTMLTranslation = '<img src="different.jpg" alt="Un chien" class="photo">';
24+
2125
let translator: ChatGPTTranslator;
2226

2327
beforeEach(() => {
@@ -50,4 +54,16 @@ describe('ChatGPTTranslator.performTranslation', () => {
5054
expect(MockedOpenAIUtils.prototype.promptChatCompletions).toHaveBeenCalledTimes(ChatGPTTranslator.MAX_RETRIES + 1);
5155
expect(result).toBe(original);
5256
});
57+
58+
it('retries if translated HTML has incorrect attributes, then succeeds', async () => {
59+
// First attempt returns invalid HTML format, second returns valid
60+
(MockedOpenAIUtils.prototype.promptChatCompletions as jest.Mock).mockResolvedValueOnce(invalidHTMLTranslation).mockResolvedValueOnce(validHTMLTranslation);
61+
62+
// @ts-expect-error TS2445
63+
const result = await translator.performTranslation(targetLang, originalHTML);
64+
65+
// eslint-disable-next-line @typescript-eslint/unbound-method
66+
expect(MockedOpenAIUtils.prototype.promptChatCompletions).toHaveBeenCalledTimes(2);
67+
expect(result).toBe(validHTMLTranslation);
68+
});
5369
});

0 commit comments

Comments
 (0)