Skip to content

Commit 816ba11

Browse files
eric-zahariaclaude
andauthored
fix(javascript): add warning in replaceAllObjects for empty objects (#6608)
Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6e83cca commit 816ba11

15 files changed

Lines changed: 126 additions & 42 deletions

File tree

โ€Žclients/algoliasearch-client-javascript/package.jsonโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
},
6464
{
6565
"path": "packages/client-search/dist/builds/browser.umd.js",
66-
"maxSize": "10KB"
66+
"maxSize": "11KB"
6767
},
6868
{
6969
"path": "packages/ingestion/dist/builds/browser.umd.js",

โ€Žclients/algoliasearch-client-javascript/packages/algoliasearch/__tests__/algoliasearch.browser.test.tsโ€Ž

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@ test('sets the ua', () => {
1515
});
1616
});
1717

18-
test('with logger', () => {
19-
vi.spyOn(console, 'debug');
20-
vi.spyOn(console, 'info');
21-
vi.spyOn(console, 'error');
18+
test('with logger', async () => {
19+
const consoleInfo = vi.spyOn(console, 'info').mockImplementation(() => {});
2220

2321
const client = algoliasearch('APP_ID', 'API_KEY', {
2422
logger: createConsoleLogger(LogLevelEnum.Debug),
23+
// every host fails, exercising the retry logging path without a real request that could outlive the test
24+
requester: {
25+
send: () => Promise.resolve({ content: 'internal error', isTimedOut: false, status: 500 }),
26+
},
2527
});
2628

27-
expect(async () => {
28-
await client.setSettings({ indexName: 'foo', indexSettings: {} });
29-
expect(console.debug).toHaveBeenCalledTimes(1);
30-
expect(console.info).toHaveBeenCalledTimes(1);
31-
expect(console.error).toHaveBeenCalledTimes(1);
32-
}).not.toThrow();
29+
await expect(client.setSettings({ indexName: 'foo', indexSettings: {} })).rejects.toThrow();
30+
31+
expect(consoleInfo).toHaveBeenCalledWith('Retryable failure', expect.any(Object));
3332
});

โ€Žclients/algoliasearch-client-javascript/packages/algoliasearch/__tests__/algoliasearch.fetch.test.tsโ€Ž

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,18 @@ test('forwards node search helpers', () => {
2222
}).not.toThrow();
2323
});
2424

25-
test('with logger', () => {
26-
vi.spyOn(console, 'debug');
27-
vi.spyOn(console, 'info');
28-
vi.spyOn(console, 'error');
25+
test('with logger', async () => {
26+
const consoleInfo = vi.spyOn(console, 'info').mockImplementation(() => {});
2927

3028
const client = algoliasearch('APP_ID', 'API_KEY', {
3129
logger: createConsoleLogger(LogLevelEnum.Debug),
30+
// every host fails, exercising the retry logging path without a real request that could outlive the test
31+
requester: {
32+
send: () => Promise.resolve({ content: 'internal error', isTimedOut: false, status: 500 }),
33+
},
3234
});
3335

34-
expect(async () => {
35-
await client.setSettings({ indexName: 'foo', indexSettings: {} });
36-
expect(console.debug).toHaveBeenCalledTimes(1);
37-
expect(console.info).toHaveBeenCalledTimes(1);
38-
expect(console.error).toHaveBeenCalledTimes(1);
39-
}).not.toThrow();
36+
await expect(client.setSettings({ indexName: 'foo', indexSettings: {} })).rejects.toThrow();
37+
38+
expect(consoleInfo).toHaveBeenCalledWith('Retryable failure', expect.any(Object));
4039
});

โ€Žclients/algoliasearch-client-javascript/packages/algoliasearch/__tests__/algoliasearch.node.test.tsโ€Ž

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,18 @@ test('forwards node search helpers', () => {
2424
}).not.toThrow();
2525
});
2626

27-
test('with logger', () => {
28-
vi.spyOn(console, 'debug');
29-
vi.spyOn(console, 'info');
30-
vi.spyOn(console, 'error');
27+
test('with logger', async () => {
28+
const consoleInfo = vi.spyOn(console, 'info').mockImplementation(() => {});
3129

3230
const client = algoliasearch('APP_ID', 'API_KEY', {
3331
logger: createConsoleLogger(LogLevelEnum.Debug),
32+
// every host fails, exercising the retry logging path without a real request that could outlive the test
33+
requester: {
34+
send: () => Promise.resolve({ content: 'internal error', isTimedOut: false, status: 500 }),
35+
},
3436
});
3537

36-
expect(async () => {
37-
await client.setSettings({ indexName: 'foo', indexSettings: {} });
38-
expect(console.debug).toHaveBeenCalledTimes(1);
39-
expect(console.info).toHaveBeenCalledTimes(1);
40-
expect(console.error).toHaveBeenCalledTimes(1);
41-
}).not.toThrow();
38+
await expect(client.setSettings({ indexName: 'foo', indexSettings: {} })).rejects.toThrow();
39+
40+
expect(consoleInfo).toHaveBeenCalledWith('Retryable failure', expect.any(Object));
4241
});

โ€Žclients/algoliasearch-client-javascript/packages/algoliasearch/__tests__/algoliasearch.worker.test.tsโ€Ž

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,18 @@ test('web crypto implementation gives the same result as node crypto', async ()
3737
expect(resp).toEqual(nodeResp);
3838
});
3939

40-
test('with logger', () => {
41-
vi.spyOn(console, 'debug');
42-
vi.spyOn(console, 'info');
43-
vi.spyOn(console, 'error');
40+
test('with logger', async () => {
41+
const consoleInfo = vi.spyOn(console, 'info').mockImplementation(() => {});
4442

4543
const client = algoliasearch('APP_ID', 'API_KEY', {
4644
logger: createConsoleLogger(LogLevelEnum.Debug),
45+
// every host fails, exercising the retry logging path without a real request that could outlive the test
46+
requester: {
47+
send: () => Promise.resolve({ content: 'internal error', isTimedOut: false, status: 500 }),
48+
},
4749
});
4850

49-
expect(async () => {
50-
await client.setSettings({ indexName: 'foo', indexSettings: {} });
51-
expect(console.debug).toHaveBeenCalledTimes(1);
52-
expect(console.info).toHaveBeenCalledTimes(1);
53-
expect(console.error).toHaveBeenCalledTimes(1);
54-
}).not.toThrow();
51+
await expect(client.setSettings({ indexName: 'foo', indexSettings: {} })).rejects.toThrow();
52+
53+
expect(consoleInfo).toHaveBeenCalledWith('Retryable failure', expect.any(Object));
5554
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, test, vi } from 'vitest';
2+
3+
import { createNullLogger, logWarning } from '../../logger';
4+
5+
describe('logWarning', () => {
6+
test('prefers the logger warn method when implemented', () => {
7+
vi.resetAllMocks();
8+
vi.spyOn(console, 'warn');
9+
10+
const warn = vi.fn().mockResolvedValue(undefined);
11+
12+
logWarning({ ...createNullLogger(), warn }, 'foo');
13+
14+
expect(warn).toHaveBeenCalledWith('foo');
15+
expect(console.warn).toHaveBeenCalledTimes(0);
16+
});
17+
18+
test('falls back to console.warn when the logger does not implement warn', () => {
19+
vi.resetAllMocks();
20+
vi.spyOn(console, 'warn');
21+
22+
logWarning(createNullLogger(), 'foo');
23+
24+
expect(console.warn).toHaveBeenCalledWith('foo');
25+
});
26+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './createNullLogger';
2+
export * from './logWarning';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { Logger } from '../types';
2+
3+
/**
4+
* Prefers the logger's optional `warn` method and falls back to `console.warn`, so warnings stay visible with the default no-op logger.
5+
*/
6+
export function logWarning(logger: Logger, message: string): void {
7+
if (logger.warn) {
8+
// a rejecting custom `warn` must not become a fatal unhandled rejection
9+
void Promise.resolve(logger.warn(message)).catch(() => {});
10+
} else {
11+
console.warn(message);
12+
}
13+
}

โ€Žclients/algoliasearch-client-javascript/packages/client-common/src/types/logger.tsโ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ export type Logger = {
1717
*/
1818
info: (message: string, args?: any | undefined) => Promise<void>;
1919

20+
/**
21+
* Logs warning messages. When not implemented, warnings fall back to `console.warn` so they stay visible with the default no-op logger.
22+
*/
23+
warn?: (message: string, args?: any | undefined) => Promise<void>;
24+
2025
/**
2126
* Logs error messages.
2227
*/

โ€Žclients/algoliasearch-client-javascript/packages/client-common/vitest.config.tsโ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default defineConfig({
99
'src/__tests__/cache/null-cache.test.ts',
1010
'src/__tests__/cache/memory-cache.test.ts',
1111
'src/__tests__/create-iterable-promise.test.ts',
12+
'src/__tests__/logger/logWarning.test.ts',
1213
'src/__tests__/logger/null-logger.test.ts',
1314
'src/__tests__/sse.test.ts',
1415
'src/__tests__/transporter/cache.test.ts',
@@ -25,6 +26,7 @@ export default defineConfig({
2526
'src/__tests__/cache/fallbackable-cache.test.ts',
2627
'src/__tests__/cache/null-cache.test.ts',
2728
'src/__tests__/create-iterable-promise.test.ts',
29+
'src/__tests__/logger/logWarning.test.ts',
2830
'src/__tests__/logger/null-logger.test.ts',
2931
'src/__tests__/transporter/cache.test.ts',
3032
],

0 commit comments

Comments
ย (0)