Skip to content

Commit 42b05b0

Browse files
committed
fix all FT.SUGGET variations
1 parent 77308ed commit 42b05b0

8 files changed

+218
-162
lines changed

packages/search/lib/commands/SUGGET.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ArrayReply, BlobStringReply, Command, RedisArgument } from '@redis/client/dist/lib/RESP/types';
1+
import { NullReply, ArrayReply, BlobStringReply, Command, RedisArgument } from '@redis/client/dist/lib/RESP/types';
22

33
export interface FtSugGetOptions {
44
FUZZY?: boolean;
@@ -21,5 +21,5 @@ export default {
2121

2222
return args;
2323
},
24-
transformReply: undefined as unknown as () => ArrayReply<BlobStringReply>
24+
transformReply: undefined as unknown as () => NullReply | ArrayReply<BlobStringReply>
2525
} as const satisfies Command;
Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
import { strict as assert } from 'node:assert';
22
import testUtils, { GLOBAL } from '../test-utils';
3-
import { transformArguments } from './SUGGET_WITHPAYLOADS';
3+
import SUGGET_WITHPAYLOADS from './SUGGET_WITHPAYLOADS';
44

5-
describe('SUGGET WITHPAYLOADS', () => {
6-
it('transformArguments', () => {
7-
assert.deepEqual(
8-
transformArguments('key', 'prefix'),
9-
['FT.SUGGET', 'key', 'prefix', 'WITHPAYLOADS']
10-
);
11-
});
5+
describe('FT.SUGGET WITHPAYLOADS', () => {
6+
it('transformArguments', () => {
7+
assert.deepEqual(
8+
SUGGET_WITHPAYLOADS.transformArguments('key', 'prefix'),
9+
['FT.SUGGET', 'key', 'prefix', 'WITHPAYLOADS']
10+
);
11+
});
1212

13-
describe('client.ft.sugGetWithPayloads', () => {
14-
testUtils.testWithClient('null', async client => {
15-
assert.equal(
16-
await client.ft.sugGetWithPayloads('key', 'prefix'),
17-
null
18-
);
19-
}, GLOBAL.SERVERS.OPEN);
13+
describe('client.ft.sugGetWithPayloads', () => {
14+
testUtils.testWithClient('null', async client => {
15+
assert.equal(
16+
await client.ft.sugGetWithPayloads('key', 'prefix'),
17+
null
18+
);
19+
}, GLOBAL.SERVERS.OPEN);
2020

21-
testUtils.testWithClient('with suggestions', async client => {
22-
await client.ft.sugAdd('key', 'string', 1, { PAYLOAD: 'payload' });
21+
testUtils.testWithClient('with suggestions', async client => {
22+
const [, reply] = await Promise.all([
23+
client.ft.sugAdd('key', 'string', 1, {
24+
PAYLOAD: 'payload'
25+
}),
26+
client.ft.sugGetWithPayloads('key', 'string')
27+
]);
2328

24-
assert.deepEqual(
25-
await client.ft.sugGetWithPayloads('key', 'string'),
26-
[{
27-
suggestion: 'string',
28-
payload: 'payload'
29-
}]
30-
);
31-
}, GLOBAL.SERVERS.OPEN);
32-
});
29+
assert.deepEqual(reply, [{
30+
suggestion: 'string',
31+
payload: 'payload'
32+
}]);
33+
}, GLOBAL.SERVERS.OPEN);
34+
});
3335
});
Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
import { SugGetOptions, transformArguments as transformSugGetArguments } from './SUGGET';
1+
import { NullReply, ArrayReply, BlobStringReply, UnwrapReply, Command } from '@redis/client/dist/lib/RESP/types';
2+
import { isNullReply } from '@redis/client/dist/lib/commands/generic-transformers';
3+
import SUGGET from './SUGGET';
24

3-
export { IS_READ_ONLY } from './SUGGET';
5+
export default {
6+
FIRST_KEY_INDEX: SUGGET.FIRST_KEY_INDEX,
7+
IS_READ_ONLY: SUGGET.IS_READ_ONLY,
8+
transformArguments(...args: Parameters<typeof SUGGET.transformArguments>) {
9+
const transformedArguments = SUGGET.transformArguments(...args);
10+
transformedArguments.push('WITHPAYLOADS');
11+
return transformedArguments;
12+
},
13+
transformReply(reply: NullReply | UnwrapReply<ArrayReply<BlobStringReply>>) {
14+
if (isNullReply(reply)) return null;
415

5-
export function transformArguments(key: string, prefix: string, options?: SugGetOptions): Array<string> {
6-
return [
7-
...transformSugGetArguments(key, prefix, options),
8-
'WITHPAYLOADS'
9-
];
10-
}
11-
12-
export interface SuggestionWithPayload {
13-
suggestion: string;
14-
payload: string | null;
15-
}
16-
17-
export function transformReply(rawReply: Array<string | null> | null): Array<SuggestionWithPayload> | null {
18-
if (rawReply === null) return null;
19-
20-
const transformedReply = [];
21-
for (let i = 0; i < rawReply.length; i += 2) {
22-
transformedReply.push({
23-
suggestion: rawReply[i]!,
24-
payload: rawReply[i + 1]
25-
});
16+
const transformedReply: Array<{
17+
suggestion: BlobStringReply;
18+
payload: BlobStringReply;
19+
}> = new Array(reply.length / 2);
20+
let replyIndex = 0,
21+
arrIndex = 0;
22+
while (replyIndex < reply.length) {
23+
transformedReply[arrIndex++] = {
24+
suggestion: reply[replyIndex++],
25+
payload: reply[replyIndex++]
26+
};
2627
}
2728

2829
return transformedReply;
29-
}
30+
}
31+
} as const satisfies Command;
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
import { strict as assert } from 'node:assert';
22
import testUtils, { GLOBAL } from '../test-utils';
3-
import { transformArguments } from './SUGGET_WITHSCORES';
3+
import SUGGET_WITHSCORES from './SUGGET_WITHSCORES';
44

5-
describe('SUGGET WITHSCORES', () => {
6-
it('transformArguments', () => {
7-
assert.deepEqual(
8-
transformArguments('key', 'prefix'),
9-
['FT.SUGGET', 'key', 'prefix', 'WITHSCORES']
10-
);
11-
});
5+
describe('FT.SUGGET WITHSCORES', () => {
6+
it('transformArguments', () => {
7+
assert.deepEqual(
8+
SUGGET_WITHSCORES.transformArguments('key', 'prefix'),
9+
['FT.SUGGET', 'key', 'prefix', 'WITHSCORES']
10+
);
11+
});
1212

13-
describe('client.ft.sugGetWithScores', () => {
14-
testUtils.testWithClient('null', async client => {
15-
assert.equal(
16-
await client.ft.sugGetWithScores('key', 'prefix'),
17-
null
18-
);
19-
}, GLOBAL.SERVERS.OPEN);
13+
describe('client.ft.sugGetWithScores', () => {
14+
testUtils.testWithClient('null', async client => {
15+
assert.equal(
16+
await client.ft.sugGetWithScores('key', 'prefix'),
17+
null
18+
);
19+
}, GLOBAL.SERVERS.OPEN);
2020

21-
testUtils.testWithClient('with suggestions', async client => {
22-
await client.ft.sugAdd('key', 'string', 1);
21+
testUtils.testWithClient('with suggestions', async client => {
22+
const [, reply] = await Promise.all([
23+
client.ft.sugAdd('key', 'string', 1),
24+
client.ft.sugGetWithScores('key', 's')
25+
]);
2326

24-
assert.deepEqual(
25-
await client.ft.sugGetWithScores('key', 'string'),
26-
[{
27-
suggestion: 'string',
28-
score: 2147483648
29-
}]
30-
);
31-
}, GLOBAL.SERVERS.OPEN);
32-
});
27+
assert.ok(Array.isArray(reply));
28+
assert.equal(reply.length, 1);
29+
assert.equal(reply[0].suggestion, 'string');
30+
assert.equal(typeof reply[0].score, 'number');
31+
}, GLOBAL.SERVERS.OPEN);
32+
});
3333
});
Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,51 @@
1-
import { SugGetOptions, transformArguments as transformSugGetArguments } from './SUGGET';
1+
import { NullReply, ArrayReply, BlobStringReply, DoubleReply, UnwrapReply, Command } from '@redis/client/dist/lib/RESP/types';
2+
import { isNullReply } from '@redis/client/dist/lib/commands/generic-transformers';
3+
import SUGGET from './SUGGET';
24

3-
export { IS_READ_ONLY } from './SUGGET';
5+
export default {
6+
FIRST_KEY_INDEX: SUGGET.FIRST_KEY_INDEX,
7+
IS_READ_ONLY: SUGGET.IS_READ_ONLY,
8+
transformArguments(...args: Parameters<typeof SUGGET.transformArguments>) {
9+
const transformedArguments = SUGGET.transformArguments(...args);
10+
transformedArguments.push('WITHSCORES');
11+
return transformedArguments;
12+
},
13+
transformReply: {
14+
2(reply: NullReply | UnwrapReply<ArrayReply<BlobStringReply>>) {
15+
if (isNullReply(reply)) return null;
416

5-
export function transformArguments(key: string, prefix: string, options?: SugGetOptions): Array<string> {
6-
return [
7-
...transformSugGetArguments(key, prefix, options),
8-
'WITHSCORES'
9-
];
10-
}
17+
const transformedReply: Array<{
18+
suggestion: BlobStringReply;
19+
score: number;
20+
}> = new Array(reply.length / 2);
21+
let replyIndex = 0,
22+
arrIndex = 0;
23+
while (replyIndex < reply.length) {
24+
transformedReply[arrIndex++] = {
25+
suggestion: reply[replyIndex++],
26+
score: Number(reply[replyIndex++])
27+
};
28+
}
1129

12-
export interface SuggestionWithScores {
13-
suggestion: string;
14-
score: number;
15-
}
30+
return transformedReply;
31+
},
32+
3(reply: UnwrapReply<ArrayReply<BlobStringReply | DoubleReply>>) {
33+
if (isNullReply(reply)) return null;
34+
35+
const transformedReply: Array<{
36+
suggestion: BlobStringReply;
37+
score: DoubleReply;
38+
}> = new Array(reply.length / 2);
39+
let replyIndex = 0,
40+
arrIndex = 0;
41+
while (replyIndex < reply.length) {
42+
transformedReply[arrIndex++] = {
43+
suggestion: reply[replyIndex++] as BlobStringReply,
44+
score: reply[replyIndex++] as DoubleReply
45+
};
46+
}
1647

17-
export function transformReply(rawReply: Array<string> | null): Array<SuggestionWithScores> | null {
18-
if (rawReply === null) return null;
19-
20-
const transformedReply = [];
21-
for (let i = 0; i < rawReply.length; i += 2) {
22-
transformedReply.push({
23-
suggestion: rawReply[i],
24-
score: Number(rawReply[i + 1])
25-
});
48+
return transformedReply;
2649
}
27-
28-
return transformedReply;
29-
}
50+
}
51+
} as const satisfies Command;
Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
import { strict as assert } from 'node:assert';
22
import testUtils, { GLOBAL } from '../test-utils';
3-
import { transformArguments } from './SUGGET_WITHSCORES_WITHPAYLOADS';
3+
import SUGGET_WITHSCORES_WITHPAYLOADS from './SUGGET_WITHSCORES_WITHPAYLOADS';
44

5-
describe('SUGGET WITHSCORES WITHPAYLOADS', () => {
6-
it('transformArguments', () => {
7-
assert.deepEqual(
8-
transformArguments('key', 'prefix'),
9-
['FT.SUGGET', 'key', 'prefix', 'WITHSCORES', 'WITHPAYLOADS']
10-
);
11-
});
5+
describe('FT.SUGGET WITHSCORES WITHPAYLOADS', () => {
6+
it('transformArguments', () => {
7+
assert.deepEqual(
8+
SUGGET_WITHSCORES_WITHPAYLOADS.transformArguments('key', 'prefix'),
9+
['FT.SUGGET', 'key', 'prefix', 'WITHSCORES', 'WITHPAYLOADS']
10+
);
11+
});
1212

13-
describe('client.ft.sugGetWithScoresWithPayloads', () => {
14-
testUtils.testWithClient('null', async client => {
15-
assert.equal(
16-
await client.ft.sugGetWithScoresWithPayloads('key', 'prefix'),
17-
null
18-
);
19-
}, GLOBAL.SERVERS.OPEN);
13+
describe('client.ft.sugGetWithScoresWithPayloads', () => {
14+
testUtils.testWithClient('null', async client => {
15+
assert.equal(
16+
await client.ft.sugGetWithScoresWithPayloads('key', 'prefix'),
17+
null
18+
);
19+
}, GLOBAL.SERVERS.OPEN);
2020

21-
testUtils.testWithClient('with suggestions', async client => {
22-
await client.ft.sugAdd('key', 'string', 1, { PAYLOAD: 'payload' });
21+
testUtils.testWithClient('with suggestions', async client => {
22+
const [, reply] = await Promise.all([
23+
client.ft.sugAdd('key', 'string', 1, {
24+
PAYLOAD: 'payload'
25+
}),
26+
client.ft.sugGetWithScoresWithPayloads('key', 'string')
27+
]);
2328

24-
assert.deepEqual(
25-
await client.ft.sugGetWithScoresWithPayloads('key', 'string'),
26-
[{
27-
suggestion: 'string',
28-
score: 2147483648,
29-
payload: 'payload'
30-
}]
31-
);
32-
}, GLOBAL.SERVERS.OPEN);
33-
});
29+
assert.ok(Array.isArray(reply));
30+
assert.equal(reply.length, 1);
31+
assert.equal(reply[0].suggestion, 'string');
32+
assert.equal(typeof reply[0].score, 'number');
33+
assert.equal(reply[0].payload, 'payload');
34+
}, GLOBAL.SERVERS.OPEN);
35+
});
3436
});

0 commit comments

Comments
 (0)