Skip to content

Commit

Permalink
[Obs AI Assistant] unskip and update knowledge_base.spec.ts (#204795)
Browse files Browse the repository at this point in the history
unskip and update knowledge_base.spec.ts for serverless
  • Loading branch information
neptunian authored Dec 18, 2024
1 parent 3a4fe6f commit 5228722
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,38 @@ import {
createKnowledgeBaseModel,
deleteInferenceEndpoint,
deleteKnowledgeBaseModel,
TINY_ELSER,
} from '@kbn/test-suites-xpack/observability_ai_assistant_api_integration/tests/knowledge_base/helpers';
import { type KnowledgeBaseEntry } from '@kbn/observability-ai-assistant-plugin/common';
import { FtrProviderContext } from '../../common/ftr_provider_context';

export default function ApiTest({ getService }: FtrProviderContext) {
const ml = getService('ml');
const es = getService('es');

const observabilityAIAssistantAPIClient = getService('observabilityAIAssistantAPIClient');

// TODO: https://github.com/elastic/kibana/issues/192886
describe.skip('Knowledge base', function () {
describe('Knowledge base', function () {
// TODO: https://github.com/elastic/kibana/issues/192886 kb/setup error
this.tags(['skipMKI']);

before(async () => {
await createKnowledgeBaseModel(ml);

await observabilityAIAssistantAPIClient
.slsAdmin({
endpoint: 'POST /internal/observability_ai_assistant/kb/setup',
params: {
query: {
model_id: TINY_ELSER.id,
},
},
})
.expect(200);
});

after(async () => {
await deleteKnowledgeBaseModel(ml);
await deleteInferenceEndpoint({ es });
});

it('returns 200 on knowledge base setup', async () => {
const res = await observabilityAIAssistantAPIClient
.slsEditor({
endpoint: 'POST /internal/observability_ai_assistant/kb/setup',
})
.expect(200);
expect(res.body).to.eql({});
await clearKnowledgeBase(es);
});

describe('when managing a single entry', () => {
Expand All @@ -48,27 +51,26 @@ export default function ApiTest({ getService }: FtrProviderContext) {
title: 'My title',
text: 'My content',
};

it('returns 200 on create', async () => {
await observabilityAIAssistantAPIClient
.slsEditor({
endpoint: 'POST /internal/observability_ai_assistant/kb/entries/save',
params: { body: knowledgeBaseEntry },
})
.expect(200);

const res = await observabilityAIAssistantAPIClient.slsEditor({
endpoint: 'GET /internal/observability_ai_assistant/kb/entries',
params: {
query: {
query: '',
sortBy: 'doc_id',
sortBy: 'title',
sortDirection: 'asc',
},
},
});
const entry = res.body.entries[0];
expect(entry.id).to.equal(knowledgeBaseEntry.id);
expect(entry.title).to.equal(knowledgeBaseEntry.title);
expect(entry.text).to.equal(knowledgeBaseEntry.text);
});

Expand All @@ -79,14 +81,15 @@ export default function ApiTest({ getService }: FtrProviderContext) {
params: {
query: {
query: '',
sortBy: 'doc_id',
sortBy: 'title',
sortDirection: 'asc',
},
},
})
.expect(200);
const entry = res.body.entries[0];
expect(entry.id).to.equal(knowledgeBaseEntry.id);
expect(entry.title).to.equal(knowledgeBaseEntry.title);
expect(entry.text).to.equal(knowledgeBaseEntry.text);
});

Expand All @@ -107,7 +110,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
params: {
query: {
query: '',
sortBy: 'doc_id',
sortBy: 'title',
sortDirection: 'asc',
},
},
Expand All @@ -132,125 +135,86 @@ export default function ApiTest({ getService }: FtrProviderContext) {
});

describe('when managing multiple entries', () => {
before(async () => {
await clearKnowledgeBase(es);
});

afterEach(async () => {
await clearKnowledgeBase(es);
});

const knowledgeBaseEntries = [
{
id: 'my_doc_a',
title: 'My title a',
text: 'My content a',
},
{
id: 'my_doc_b',
title: 'My title b',
text: 'My content b',
},
{
id: 'my_doc_c',
title: 'My title c',
text: 'My content c',
},
];

it('returns 200 on create', async () => {
await observabilityAIAssistantAPIClient
.slsEditor({
endpoint: 'POST /internal/observability_ai_assistant/kb/entries/import',
params: { body: { entries: knowledgeBaseEntries } },
})
.expect(200);

async function getEntries({
query = '',
sortBy = 'title',
sortDirection = 'asc',
}: { query?: string; sortBy?: string; sortDirection?: 'asc' | 'desc' } = {}) {
const res = await observabilityAIAssistantAPIClient
.slsEditor({
endpoint: 'GET /internal/observability_ai_assistant/kb/entries',
params: {
query: {
query: '',
sortBy: 'doc_id',
sortDirection: 'asc',
},
query: { query, sortBy, sortDirection },
},
})
.expect(200);
expect(res.body.entries.filter((entry) => entry.id.startsWith('my_doc')).length).to.eql(3);
});

it('allows sorting', async () => {
return omitCategories(res.body.entries);
}

beforeEach(async () => {
await clearKnowledgeBase(es);

await observabilityAIAssistantAPIClient
.slsEditor({
endpoint: 'POST /internal/observability_ai_assistant/kb/entries/import',
params: { body: { entries: knowledgeBaseEntries } },
})
.expect(200);

const res = await observabilityAIAssistantAPIClient
.slsEditor({
endpoint: 'GET /internal/observability_ai_assistant/kb/entries',
params: {
query: {
query: '',
sortBy: 'doc_id',
sortDirection: 'desc',
body: {
entries: [
{
id: 'my_doc_a',
title: 'My title a',
text: 'My content a',
},
{
id: 'my_doc_b',
title: 'My title b',
text: 'My content b',
},
{
id: 'my_doc_c',
title: 'My title c',
text: 'My content c',
},
],
},
},
})
.expect(200);
});

const entries = res.body.entries.filter((entry) => entry.id.startsWith('my_doc'));
expect(entries[0].id).to.eql('my_doc_c');
expect(entries[1].id).to.eql('my_doc_b');
expect(entries[2].id).to.eql('my_doc_a');

// asc
const resAsc = await observabilityAIAssistantAPIClient
.slsEditor({
endpoint: 'GET /internal/observability_ai_assistant/kb/entries',
params: {
query: {
query: '',
sortBy: 'doc_id',
sortDirection: 'asc',
},
},
})
.expect(200);
afterEach(async () => {
await clearKnowledgeBase(es);
});

const entriesAsc = resAsc.body.entries.filter((entry) => entry.id.startsWith('my_doc'));
expect(entriesAsc[0].id).to.eql('my_doc_a');
expect(entriesAsc[1].id).to.eql('my_doc_b');
expect(entriesAsc[2].id).to.eql('my_doc_c');
it('returns 200 on create', async () => {
const entries = await getEntries();
expect(omitCategories(entries).length).to.eql(3);
});

it('allows searching', async () => {
await observabilityAIAssistantAPIClient
.slsEditor({
endpoint: 'POST /internal/observability_ai_assistant/kb/entries/import',
params: { body: { entries: knowledgeBaseEntries } },
})
.expect(200);
describe('when sorting ', () => {
const ascendingOrder = ['my_doc_a', 'my_doc_b', 'my_doc_c'];

const res = await observabilityAIAssistantAPIClient
.slsEditor({
endpoint: 'GET /internal/observability_ai_assistant/kb/entries',
params: {
query: {
query: 'my_doc_a',
sortBy: 'doc_id',
sortDirection: 'asc',
},
},
})
.expect(200);
it('allows sorting ascending', async () => {
const entries = await getEntries({ sortBy: 'title', sortDirection: 'asc' });
expect(entries.map(({ id }) => id)).to.eql(ascendingOrder);
});

expect(res.body.entries.length).to.eql(1);
expect(res.body.entries[0].id).to.eql('my_doc_a');
it('allows sorting descending', async () => {
const entries = await getEntries({ sortBy: 'title', sortDirection: 'desc' });
expect(entries.map(({ id }) => id)).to.eql([...ascendingOrder].reverse());
});
});

it('allows searching by title', async () => {
const entries = await getEntries({ query: 'b' });
expect(entries.length).to.eql(1);
expect(entries[0].title).to.eql('My title b');
});
});
});
}

function omitCategories(entries: KnowledgeBaseEntry[]) {
return entries.filter((entry) => entry.labels?.category === undefined);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
const observabilityAIAssistantAPIClient = getService('observabilityAIAssistantAPIClient');

describe('/internal/observability_ai_assistant/kb/setup', function () {
// TODO: https://github.com/elastic/kibana/issues/192886 kb/setup error
this.tags(['skipMKI']);

before(async () => {
Expand Down

0 comments on commit 5228722

Please sign in to comment.