feat(editor): Add workflow evaluation run views (no-changelog) (#12258)

This commit is contained in:
oleg
2025-01-07 12:52:44 +01:00
committed by GitHub
parent ecabe34705
commit 3d990eb555
41 changed files with 3811 additions and 579 deletions

View File

@@ -12,18 +12,21 @@ const TEST_DEF_A: TestDefinitionRecord = {
evaluationWorkflowId: '456',
workflowId: '123',
annotationTagId: '789',
annotationTag: null,
};
const TEST_DEF_B: TestDefinitionRecord = {
id: '2',
name: 'Test Definition B',
workflowId: '123',
description: 'Description B',
annotationTag: null,
};
const TEST_DEF_NEW: TestDefinitionRecord = {
id: '3',
workflowId: '123',
name: 'New Test Definition',
description: 'New Description',
annotationTag: null,
};
beforeEach(() => {
@@ -35,44 +38,78 @@ afterEach(() => {
vi.clearAllMocks();
});
describe('useTestDefinitionForm', async () => {
it('should initialize with default props', async () => {
describe('useTestDefinitionForm', () => {
it('should initialize with default props', () => {
const { state } = useTestDefinitionForm();
expect(state.value.description).toEqual('');
expect(state.value.description).toBe('');
expect(state.value.name.value).toContain('My Test');
expect(state.value.tags.appliedTagIds).toEqual([]);
expect(state.value.metrics).toEqual(['']);
expect(state.value.evaluationWorkflow.value).toEqual('');
expect(state.value.tags.value).toEqual([]);
expect(state.value.metrics).toEqual([]);
expect(state.value.evaluationWorkflow.value).toBe('');
});
it('should load test data', async () => {
const { loadTestData, state } = useTestDefinitionForm();
const fetchSpy = vi.fn();
const fetchSpy = vi.spyOn(useTestDefinitionStore(), 'fetchAll');
const fetchMetricsSpy = vi.spyOn(useTestDefinitionStore(), 'fetchMetrics').mockResolvedValue([
{
id: 'metric1',
name: 'Metric 1',
testDefinitionId: TEST_DEF_A.id,
},
]);
const evaluationsStore = mockedStore(useTestDefinitionStore);
expect(state.value.description).toEqual('');
expect(state.value.name.value).toContain('My Test');
evaluationsStore.testDefinitionsById = {
[TEST_DEF_A.id]: TEST_DEF_A,
[TEST_DEF_B.id]: TEST_DEF_B,
};
evaluationsStore.fetchAll = fetchSpy;
await loadTestData(TEST_DEF_A.id);
expect(fetchSpy).toBeCalled();
expect(fetchMetricsSpy).toBeCalledWith(TEST_DEF_A.id);
expect(state.value.name.value).toEqual(TEST_DEF_A.name);
expect(state.value.description).toEqual(TEST_DEF_A.description);
expect(state.value.tags.appliedTagIds).toEqual([TEST_DEF_A.annotationTagId]);
expect(state.value.tags.value).toEqual([TEST_DEF_A.annotationTagId]);
expect(state.value.evaluationWorkflow.value).toEqual(TEST_DEF_A.evaluationWorkflowId);
expect(state.value.metrics).toEqual([
{ id: 'metric1', name: 'Metric 1', testDefinitionId: TEST_DEF_A.id },
]);
});
it('should gracefully handle loadTestData when no test definition found', async () => {
const { loadTestData, state } = useTestDefinitionForm();
const fetchSpy = vi.spyOn(useTestDefinitionStore(), 'fetchAll');
const evaluationsStore = mockedStore(useTestDefinitionStore);
evaluationsStore.testDefinitionsById = {};
await loadTestData('unknown-id');
expect(fetchSpy).toBeCalled();
// Should remain unchanged since no definition found
expect(state.value.description).toBe('');
expect(state.value.name.value).toContain('My Test');
expect(state.value.tags.value).toEqual([]);
expect(state.value.metrics).toEqual([]);
});
it('should handle errors while loading test data', async () => {
const { loadTestData } = useTestDefinitionForm();
const fetchSpy = vi
.spyOn(useTestDefinitionStore(), 'fetchAll')
.mockRejectedValue(new Error('Fetch Failed'));
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
await loadTestData(TEST_DEF_A.id);
expect(fetchSpy).toBeCalled();
expect(consoleErrorSpy).toBeCalledWith('Failed to load test data', expect.any(Error));
consoleErrorSpy.mockRestore();
});
it('should save a new test', async () => {
const { createTest, state } = useTestDefinitionForm();
const createSpy = vi.fn().mockResolvedValue(TEST_DEF_NEW);
const evaluationsStore = mockedStore(useTestDefinitionStore);
evaluationsStore.create = createSpy;
const createSpy = vi.spyOn(useTestDefinitionStore(), 'create').mockResolvedValue(TEST_DEF_NEW);
state.value.name.value = TEST_DEF_NEW.name;
state.value.description = TEST_DEF_NEW.description ?? '';
@@ -86,12 +123,24 @@ describe('useTestDefinitionForm', async () => {
expect(newTest).toEqual(TEST_DEF_NEW);
});
it('should handle errors when creating a new test', async () => {
const { createTest } = useTestDefinitionForm();
const createSpy = vi
.spyOn(useTestDefinitionStore(), 'create')
.mockRejectedValue(new Error('Create Failed'));
await expect(createTest('123')).rejects.toThrow('Create Failed');
expect(createSpy).toBeCalled();
});
it('should update an existing test', async () => {
const { updateTest, state } = useTestDefinitionForm();
const updateSpy = vi.fn().mockResolvedValue(TEST_DEF_B);
const evaluationsStore = mockedStore(useTestDefinitionStore);
evaluationsStore.update = updateSpy;
const updatedBTest = {
...TEST_DEF_B,
updatedAt: '2022-01-01T00:00:00.000Z',
createdAt: '2022-01-01T00:00:00.000Z',
};
const updateSpy = vi.spyOn(useTestDefinitionStore(), 'update').mockResolvedValue(updatedBTest);
state.value.name.value = TEST_DEF_B.name;
state.value.description = TEST_DEF_B.description ?? '';
@@ -102,75 +151,183 @@ describe('useTestDefinitionForm', async () => {
name: TEST_DEF_B.name,
description: TEST_DEF_B.description,
});
expect(updatedTest).toEqual(TEST_DEF_B);
expect(updatedTest).toEqual(updatedBTest);
});
it('should start editing a field', async () => {
it('should throw an error if no testId is provided when updating a test', async () => {
const { updateTest } = useTestDefinitionForm();
await expect(updateTest('')).rejects.toThrow('Test ID is required for updating a test');
});
it('should handle errors when updating a test', async () => {
const { updateTest, state } = useTestDefinitionForm();
const updateSpy = vi
.spyOn(useTestDefinitionStore(), 'update')
.mockRejectedValue(new Error('Update Failed'));
state.value.name.value = 'Test';
state.value.description = 'Some description';
await expect(updateTest(TEST_DEF_A.id)).rejects.toThrow('Update Failed');
expect(updateSpy).toBeCalled();
});
it('should delete a metric', async () => {
const { state, deleteMetric } = useTestDefinitionForm();
const evaluationsStore = mockedStore(useTestDefinitionStore);
const deleteMetricSpy = vi.spyOn(evaluationsStore, 'deleteMetric');
state.value.metrics = [
{
id: 'metric1',
name: 'Metric 1',
testDefinitionId: '1',
},
{
id: 'metric2',
name: 'Metric 2',
testDefinitionId: '1',
},
];
await deleteMetric('metric1', TEST_DEF_A.id);
expect(deleteMetricSpy).toBeCalledWith({ id: 'metric1', testDefinitionId: TEST_DEF_A.id });
expect(state.value.metrics).toEqual([
{ id: 'metric2', name: 'Metric 2', testDefinitionId: '1' },
]);
});
it('should update metrics', async () => {
const { state, updateMetrics } = useTestDefinitionForm();
const evaluationsStore = mockedStore(useTestDefinitionStore);
const updateMetricSpy = vi.spyOn(evaluationsStore, 'updateMetric');
const createMetricSpy = vi
.spyOn(evaluationsStore, 'createMetric')
.mockResolvedValue({ id: 'metric_new', name: 'Metric 2', testDefinitionId: TEST_DEF_A.id });
state.value.metrics = [
{
id: 'metric1',
name: 'Metric 1',
testDefinitionId: TEST_DEF_A.id,
},
{
id: '',
name: 'Metric 2',
testDefinitionId: TEST_DEF_A.id,
}, // New metric that needs creation
];
await updateMetrics(TEST_DEF_A.id);
expect(createMetricSpy).toHaveBeenCalledWith({
name: 'Metric 2',
testDefinitionId: TEST_DEF_A.id,
});
expect(updateMetricSpy).toHaveBeenCalledWith({
name: 'Metric 1',
id: 'metric1',
testDefinitionId: TEST_DEF_A.id,
});
expect(state.value.metrics).toEqual([
{ id: 'metric1', name: 'Metric 1', testDefinitionId: TEST_DEF_A.id },
{ id: 'metric_new', name: 'Metric 2', testDefinitionId: TEST_DEF_A.id },
]);
});
it('should start editing a field', () => {
const { state, startEditing } = useTestDefinitionForm();
await startEditing('name');
startEditing('name');
expect(state.value.name.isEditing).toBe(true);
expect(state.value.name.tempValue).toBe(state.value.name.value);
await startEditing('tags');
startEditing('tags');
expect(state.value.tags.isEditing).toBe(true);
expect(state.value.tags.tempValue).toEqual(state.value.tags.value);
});
it('should save changes to a field', async () => {
it('should do nothing if startEditing is called while already editing', () => {
const { state, startEditing } = useTestDefinitionForm();
state.value.name.isEditing = true;
state.value.name.tempValue = 'Original Name';
startEditing('name');
// Should remain unchanged because it was already editing
expect(state.value.name.isEditing).toBe(true);
expect(state.value.name.tempValue).toBe('Original Name');
});
it('should save changes to a field', () => {
const { state, startEditing, saveChanges } = useTestDefinitionForm();
await startEditing('name');
// Name
startEditing('name');
state.value.name.tempValue = 'New Name';
saveChanges('name');
expect(state.value.name.isEditing).toBe(false);
expect(state.value.name.value).toBe('New Name');
await startEditing('tags');
state.value.tags.appliedTagIds = ['123'];
// Tags
startEditing('tags');
state.value.tags.tempValue = ['123'];
saveChanges('tags');
expect(state.value.tags.isEditing).toBe(false);
expect(state.value.tags.appliedTagIds).toEqual(['123']);
expect(state.value.tags.value).toEqual(['123']);
});
it('should cancel editing a field', async () => {
it('should cancel editing a field', () => {
const { state, startEditing, cancelEditing } = useTestDefinitionForm();
await startEditing('name');
const originalName = state.value.name.value;
startEditing('name');
state.value.name.tempValue = 'New Name';
cancelEditing('name');
expect(state.value.name.isEditing).toBe(false);
expect(state.value.name.tempValue).toBe('');
expect(state.value.name.tempValue).toBe(originalName);
await startEditing('tags');
state.value.tags.appliedTagIds = ['123'];
const originalTags = [...state.value.tags.value];
startEditing('tags');
state.value.tags.tempValue = ['123'];
cancelEditing('tags');
expect(state.value.tags.isEditing).toBe(false);
expect(state.value.tags.tempValue).toEqual(originalTags);
});
it('should handle keydown - Escape', async () => {
it('should handle keydown - Escape', () => {
const { state, startEditing, handleKeydown } = useTestDefinitionForm();
await startEditing('name');
startEditing('name');
handleKeydown(new KeyboardEvent('keydown', { key: 'Escape' }), 'name');
expect(state.value.name.isEditing).toBe(false);
await startEditing('tags');
startEditing('tags');
handleKeydown(new KeyboardEvent('keydown', { key: 'Escape' }), 'tags');
expect(state.value.tags.isEditing).toBe(false);
});
it('should handle keydown - Enter', async () => {
it('should handle keydown - Enter', () => {
const { state, startEditing, handleKeydown } = useTestDefinitionForm();
await startEditing('name');
startEditing('name');
state.value.name.tempValue = 'New Name';
handleKeydown(new KeyboardEvent('keydown', { key: 'Enter' }), 'name');
expect(state.value.name.isEditing).toBe(false);
expect(state.value.name.value).toBe('New Name');
await startEditing('tags');
state.value.tags.appliedTagIds = ['123'];
startEditing('tags');
state.value.tags.tempValue = ['123'];
handleKeydown(new KeyboardEvent('keydown', { key: 'Enter' }), 'tags');
expect(state.value.tags.isEditing).toBe(false);
expect(state.value.tags.value).toEqual(['123']);
});
it('should not save changes when shift+Enter is pressed', () => {
const { state, startEditing, handleKeydown } = useTestDefinitionForm();
startEditing('name');
state.value.name.tempValue = 'New Name With Shift';
handleKeydown(new KeyboardEvent('keydown', { key: 'Enter', shiftKey: true }), 'name');
expect(state.value.name.isEditing).toBe(true);
expect(state.value.name.value).not.toBe('New Name With Shift');
});
});