Skip to content

Commit b235160

Browse files
committed
Empty responses, filtering scenario
1 parent 71927f8 commit b235160

File tree

2 files changed

+118
-8
lines changed

2 files changed

+118
-8
lines changed

packages/devextreme/js/__internal/grids/grid_core/ai_column/ai_column.integration.test.ts

Lines changed: 114 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3235,12 +3235,17 @@ describe('DropDownButton', () => {
32353235
});
32363236

32373237
describe('Cache', () => {
3238-
beforeEach(beforeTest);
3238+
const sendRequestSpy = jest.fn();
3239+
3240+
beforeEach(() => {
3241+
beforeTest();
3242+
sendRequestSpy.mockClear();
3243+
});
3244+
32393245
afterEach(afterTest);
32403246

32413247
describe('when use public methods', () => {
32423248
it('should not use cached data with sendAIColumnRequest', async () => {
3243-
const sendRequestSpy = jest.fn();
32443249
const aiIntegration = new AIIntegration({
32453250
sendRequest(prompt): RequestResult {
32463251
sendRequestSpy();
@@ -3295,7 +3300,6 @@ describe('Cache', () => {
32953300
});
32963301

32973302
it('should not use cached data with refreshAIColumn', async () => {
3298-
const sendRequestSpy = jest.fn();
32993303
const aiIntegration = new AIIntegration({
33003304
sendRequest(prompt): RequestResult {
33013305
sendRequestSpy();
@@ -3352,7 +3356,6 @@ describe('Cache', () => {
33523356

33533357
describe('when update column options', () => {
33543358
it('should clear cached data on ai.prompt change', async () => {
3355-
const sendRequestSpy = jest.fn();
33563359
const aiIntegration = new AIIntegration({
33573360
sendRequest(prompt): RequestResult {
33583361
sendRequestSpy();
@@ -3413,10 +3416,9 @@ describe('Cache', () => {
34133416
});
34143417

34153418
it('should use cache with pagination in auto mode', async () => {
3416-
const sendRequestSpy = jest.fn();
34173419
const aiIntegration = new AIIntegration({
34183420
sendRequest(prompt): RequestResult {
3419-
sendRequestSpy();
3421+
sendRequestSpy(prompt.data?.data);
34203422

34213423
return {
34223424
promise: new Promise<string>((resolve) => {
@@ -3457,16 +3459,122 @@ describe('Cache', () => {
34573459

34583460
await Promise.resolve();
34593461
expect(sendRequestSpy).toHaveBeenCalledTimes(1);
3462+
expect(sendRequestSpy).toHaveBeenCalledWith({ 1: { id: 1, name: 'Name 1', value: 10 } });
34603463

34613464
instance.option('paging.pageIndex', 1);
34623465
jest.runAllTimers();
34633466
await Promise.resolve();
34643467
expect(sendRequestSpy).toHaveBeenCalledTimes(2);
3468+
expect(sendRequestSpy).toHaveBeenCalledWith({ 2: { id: 2, name: 'Name 2', value: 20 } });
34653469

34663470
instance.option('paging.pageIndex', 0);
34673471
jest.runAllTimers();
34683472
await Promise.resolve();
34693473
expect(sendRequestSpy).toHaveBeenCalledTimes(2);
34703474
});
3475+
3476+
it('should use cache with filtering in auto mode', async () => {
3477+
const aiIntegration = new AIIntegration({
3478+
sendRequest(prompt): RequestResult {
3479+
sendRequestSpy(prompt.data?.data);
3480+
return {
3481+
promise: new Promise<string>((resolve) => {
3482+
const result = {};
3483+
Object.entries(prompt.data?.data).forEach(([key, value]) => {
3484+
result[key] = `Response ${(value as any).name}`;
3485+
});
3486+
resolve(JSON.stringify(result));
3487+
}),
3488+
abort: (): void => {},
3489+
};
3490+
},
3491+
});
3492+
const { instance } = await createDataGrid({
3493+
dataSource: [
3494+
{ id: 1, name: 'Name 1', value: 10 },
3495+
{ id: 2, name: 'Name 2', value: 20 },
3496+
],
3497+
keyExpr: 'id',
3498+
columns: [
3499+
{ dataField: 'id', caption: 'ID' },
3500+
{ dataField: 'name', caption: 'Name' },
3501+
{ dataField: 'value', caption: 'Value' },
3502+
{
3503+
type: 'ai',
3504+
caption: 'AI Column',
3505+
name: 'myColumn',
3506+
ai: {
3507+
aiIntegration,
3508+
prompt: 'Test prompt',
3509+
},
3510+
},
3511+
],
3512+
filterValue: ['id', '=', 1],
3513+
});
3514+
3515+
await Promise.resolve();
3516+
expect(sendRequestSpy).toHaveBeenCalledTimes(1);
3517+
expect(sendRequestSpy).toHaveBeenCalledWith({ 1: { id: 1, name: 'Name 1', value: 10 } });
3518+
3519+
instance.option('filterValue', undefined);
3520+
jest.runAllTimers();
3521+
await Promise.resolve();
3522+
expect(sendRequestSpy).toHaveBeenCalledTimes(2);
3523+
expect(sendRequestSpy).toHaveBeenCalledWith({ 2: { id: 2, name: 'Name 2', value: 20 } });
3524+
});
3525+
});
3526+
3527+
describe('common behavior', () => {
3528+
it('should not cache empty responses', async () => {
3529+
const aiIntegrationResult = (prompt): RequestResult => ({
3530+
promise: new Promise<string>((resolve) => {
3531+
const result = {};
3532+
Object.entries(prompt.data?.data).forEach(([key]) => {
3533+
result[key] = '';
3534+
});
3535+
3536+
resolve(JSON.stringify(result));
3537+
}),
3538+
abort: (): void => {},
3539+
});
3540+
const columnAIIntegration = new AIIntegration({
3541+
sendRequest(prompt): RequestResult {
3542+
sendRequestSpy();
3543+
return aiIntegrationResult(prompt);
3544+
},
3545+
});
3546+
const { instance } = await createDataGrid({
3547+
dataSource: [
3548+
{ id: 1, name: 'Name 1', value: 10 },
3549+
],
3550+
keyExpr: 'id',
3551+
columns: [
3552+
{ dataField: 'id', caption: 'ID' },
3553+
{ dataField: 'name', caption: 'Name' },
3554+
{ dataField: 'value', caption: 'Value' },
3555+
{
3556+
type: 'ai',
3557+
caption: 'AI Column',
3558+
name: 'myColumn',
3559+
ai: {
3560+
aiIntegration: columnAIIntegration,
3561+
mode: 'manual',
3562+
prompt: 'Test prompt',
3563+
},
3564+
},
3565+
],
3566+
});
3567+
3568+
expect(instance.getAIColumnText('myColumn', 1)).toBeUndefined();
3569+
instance.sendAIColumnRequest('myColumn');
3570+
await Promise.resolve();
3571+
expect(sendRequestSpy).toHaveBeenCalledTimes(1);
3572+
expect(instance.getAIColumnText('myColumn', 1)).toBeUndefined();
3573+
3574+
instance.sendAIColumnRequest('myColumn');
3575+
await Promise.resolve();
3576+
expect(sendRequestSpy).toHaveBeenCalledTimes(2);
3577+
expect(instance.getAIColumnText('myColumn', 1)).toBeUndefined();
3578+
});
34713579
});
34723580
});

packages/devextreme/js/__internal/grids/grid_core/ai_column/m_ai_column_cache_controller.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class AIColumnCacheController extends Controller {
1313
if (!columnCache) return {};
1414
return keys.reduce<Record<PropertyKey, string>>((acc, key) => {
1515
const value = columnCache[key];
16-
if (value !== undefined) {
16+
if (value !== undefined && value !== '') {
1717
acc[key] = value;
1818
}
1919
return acc;
@@ -30,7 +30,9 @@ export class AIColumnCacheController extends Controller {
3030
this.cache[columnName] = columnCache;
3131
}
3232
Object.entries(data).forEach(([key, value]) => {
33-
columnCache[key] = value;
33+
if (value !== '') {
34+
columnCache[key] = value;
35+
}
3436
});
3537
}
3638

0 commit comments

Comments
 (0)