|
| 1 | +// Copyright 2025 DeepL SE (https://www.deepl.com) |
| 2 | +// Use of this source code is governed by an MIT |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import * as deepl from 'deepl-node'; |
| 6 | + |
| 7 | +import { exampleText, makeDeeplClient, makeTranslator } from './core'; |
| 8 | +import log from 'loglevel'; |
| 9 | + |
| 10 | +jest.mock('loglevel', () => ({ |
| 11 | + debug: jest.fn(), |
| 12 | + getLogger: jest.fn().mockReturnValue({ |
| 13 | + setLevel: jest.fn(), |
| 14 | + debug: jest.fn(), |
| 15 | + info: jest.fn(), |
| 16 | + }), |
| 17 | +})); |
| 18 | + |
| 19 | +describe('client tests', () => { |
| 20 | + describe('log debug', () => { |
| 21 | + beforeEach(() => { |
| 22 | + jest.clearAllMocks(); |
| 23 | + log.getLogger('deepl').setLevel('debug'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('with translate text', async () => { |
| 27 | + const translator = makeTranslator({}); |
| 28 | + const result = await translator.translateText(exampleText.en, null, 'de'); |
| 29 | + |
| 30 | + expect(log.getLogger('deepl').debug).toHaveBeenCalledTimes(3); |
| 31 | + expect(log.getLogger('deepl').debug).toHaveBeenNthCalledWith( |
| 32 | + 1, |
| 33 | + expect.stringContaining('Request details:'), |
| 34 | + ); |
| 35 | + expect(log.getLogger('deepl').debug).toHaveBeenNthCalledWith( |
| 36 | + 2, |
| 37 | + expect.stringContaining('Trace details:, xTraceId = '), |
| 38 | + ); |
| 39 | + expect(log.getLogger('deepl').debug).toHaveBeenNthCalledWith( |
| 40 | + 3, |
| 41 | + expect.stringContaining('Response details:, content = '), |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it('with rephrase text', async () => { |
| 46 | + const deeplClient = makeDeeplClient({}); |
| 47 | + const result = await deeplClient.rephraseText(exampleText.de, 'de'); |
| 48 | + |
| 49 | + expect(log.getLogger('deepl').debug).toHaveBeenCalledTimes(3); |
| 50 | + expect(log.getLogger('deepl').debug).toHaveBeenNthCalledWith( |
| 51 | + 1, |
| 52 | + expect.stringContaining('Request details:'), |
| 53 | + ); |
| 54 | + expect(log.getLogger('deepl').debug).toHaveBeenNthCalledWith( |
| 55 | + 2, |
| 56 | + expect.stringContaining('Trace details:, xTraceId = '), |
| 57 | + ); |
| 58 | + expect(log.getLogger('deepl').debug).toHaveBeenNthCalledWith( |
| 59 | + 3, |
| 60 | + expect.stringContaining('Response details:, content = '), |
| 61 | + ); |
| 62 | + }); |
| 63 | + }); |
| 64 | +}); |
0 commit comments