Skip to content

Commit

Permalink
[Obs AI Assistant] Update and add tests (elastic#185022)
Browse files Browse the repository at this point in the history
  • Loading branch information
viduni94 committed Nov 4, 2024
1 parent 91d19ac commit 042f20b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,27 @@ describe('date time formatters', () => {

it('should add a leading plus for timezones with positive UTC offset', () => {
moment.tz.setDefault('Europe/Copenhagen');
expect(asAbsoluteDateTime(1728000000000, 'minutes')).toBe('Oct 5, 2024, 14:00 (UTC+2)');
expect(asAbsoluteDateTime(1559390400000, 'minutes')).toBe('Jun 1, 2019, 14:00 (UTC+2)');
});

it('should add a leading minus for timezones with negative UTC offset', () => {
moment.tz.setDefault('America/Los_Angeles');
expect(asAbsoluteDateTime(1728000000000, 'minutes')).toBe('Oct 5, 2024, 05:00 (UTC-7)');
expect(asAbsoluteDateTime(1559390400000, 'minutes')).toBe('Jun 1, 2019, 05:00 (UTC-7)');
});

it('should use default UTC offset formatting when offset contains minutes', () => {
moment.tz.setDefault('Canada/Newfoundland');
expect(asAbsoluteDateTime(1728000000000, 'minutes')).toBe('Oct 5, 2024, 09:30 (UTC-02:30)');
expect(asAbsoluteDateTime(1559390400000, 'minutes')).toBe('Jun 1, 2019, 09:30 (UTC-02:30)');
});

it('should respect DST', () => {
moment.tz.setDefault('Europe/Copenhagen');
const timeWithDST = 1728000000000; // Oct 5, 2024
const timeWithoutDST = 1733030400000; // Oct 31, 2024
const timeWithDST = 1559390400000; // Jun 1, 2019
const timeWithoutDST = 1575201600000; // Dec 1, 2019

expect(asAbsoluteDateTime(timeWithDST)).toBe('Oct 5, 2024, 14:00:00.000 (UTC+2)');
expect(asAbsoluteDateTime(timeWithoutDST)).toBe('Oct 31, 2024, 14:00:00.000 (UTC+1)');
expect(asAbsoluteDateTime(timeWithDST)).toBe('Jun 1, 2019, 14:00:00.000 (UTC+2)');

expect(asAbsoluteDateTime(timeWithoutDST)).toBe('Dec 1, 2019, 13:00:00.000 (UTC+1)');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { ChatEvent } from '../../../../common/conversation_complete';
import { LangTracer } from '../instrumentation/lang_tracer';
import { TITLE_CONVERSATION_FUNCTION_NAME, getGeneratedTitle } from './get_generated_title';
import * as dateTimeUtils from '../../../../common/utils/formatters/datetime';

describe('getGeneratedTitle', () => {
const messages: Message[] = [
Expand Down Expand Up @@ -77,6 +78,28 @@ describe('getGeneratedTitle', () => {
expect(title).toEqual('My title');
});

it('appends timestamp when isPublic is true', async () => {
jest.spyOn(dateTimeUtils, 'asAbsoluteDateTime').mockReturnValue('Oct 10, 2024, 14:00 (UTC-4)');

const { title$ } = callGenerateTitle({ isPublic: true }, [
createChatCompletionChunk({
function_call: {
name: 'title_conversation',
arguments: JSON.stringify({ title: 'My title' }),
},
}),
]);

const title = await lastValueFrom(
title$.pipe(filter((event): event is string => typeof event === 'string'))
);

expect(title).toEqual('My title - Oct 10, 2024, 14:00 (UTC-4)');

// Restore original implementation of asAbsoluteDateTime
jest.restoreAllMocks();
});

it('calls chat with the user message', async () => {
const { chatSpy, title$ } = callGenerateTitle([
createChatCompletionChunk({
Expand Down

0 comments on commit 042f20b

Please sign in to comment.