Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions packages/compass-generative-ai/src/atlas-ai-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ describe('AtlasAiService', function () {
sampleValues: [25, 30, 35],
},
},
validationRules: null,
includeSampleValues: false,
requestId: 'test-request-id',
signal: new AbortController().signal,
Expand Down Expand Up @@ -627,6 +628,125 @@ describe('AtlasAiService', function () {
);
}
});

it('includes validation rules in request body when provided', async function () {
const mockResponse = {
fields: [
{
fieldPath: 'email',
mongoType: 'String',
fakerMethod: 'internet.email',
fakerArgs: [],
},
{
fieldPath: 'age',
mongoType: 'Int32',
fakerMethod: 'number.int',
fakerArgs: [{ json: '{"min": 18, "max": 120}' }],
},
],
};
const fetchStub = sandbox
.stub()
.resolves(makeResponse(mockResponse));
global.fetch = fetchStub;

const validationRules = {
$jsonSchema: {
bsonType: 'object',
required: ['email', 'age'],
properties: {
email: {
bsonType: 'string',
pattern:
'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$',
},
age: {
bsonType: 'int',
minimum: 18,
maximum: 120,
},
},
},
};

const inputWithValidationRules = {
...mockSchemaInput,
validationRules,
};

await atlasAiService.getMockDataSchema(
inputWithValidationRules,
mockConnectionInfo
);

const { args } = fetchStub.firstCall;
const requestBody = JSON.parse(args[1].body as string);

expect(requestBody).to.have.property('validationRules');
expect(requestBody.validationRules).to.deep.equal(validationRules);
});

it('includes null validation rules in request body when not provided', async function () {
const mockResponse = {
fields: [
{
fieldPath: 'name',
mongoType: 'String',
fakerMethod: 'person.fullName',
fakerArgs: [],
},
],
};
const fetchStub = sandbox
.stub()
.resolves(makeResponse(mockResponse));
global.fetch = fetchStub;

await atlasAiService.getMockDataSchema(
mockSchemaInput,
mockConnectionInfo
);

const { args } = fetchStub.firstCall;
const requestBody = JSON.parse(args[1].body as string);

expect(requestBody).to.have.property('validationRules');
expect(requestBody.validationRules).to.be.null;
});

it('excludes validation rules from request body when explicitly undefined', async function () {
const mockResponse = {
fields: [
{
fieldPath: 'name',
mongoType: 'String',
fakerMethod: 'person.fullName',
fakerArgs: [],
},
],
};
const fetchStub = sandbox
.stub()
.resolves(makeResponse(mockResponse));
global.fetch = fetchStub;

const inputWithUndefinedValidationRules = {
...mockSchemaInput,
validationRules: undefined,
};

await atlasAiService.getMockDataSchema(
inputWithUndefinedValidationRules,
mockConnectionInfo
);

const { args } = fetchStub.firstCall;
const requestBody = JSON.parse(args[1].body as string);

// When validationRules is undefined, JSON.stringify excludes it from the output
expect(requestBody).to.not.have.property('validationRules');
});
}
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/compass-generative-ai/src/atlas-ai-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ export class AtlasAiService {
collectionName,
databaseName,
schema,
validationRules: input.validationRules,
}),
headers: {
'Content-Type': 'application/json',
Expand Down
Loading