Commit bf5947a6 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch '31162-incremental-parser' into 'master'

Creates findAndReplaceOffset funtion

See merge request gitlab-org/gitlab!17614
parents 0151f26f 77b559fd
......@@ -55,6 +55,37 @@ export const logLinesParser = (lines = [], lineNumberStart) =>
return acc;
}, []);
/**
* Finds the repeated offset, removes the old one
*
* Returns a new array with the updated log without
* the repeated offset
*
* @param Array newLog
* @param Array oldParsed
* @returns Array
*
*/
export const findOffsetAndRemove = (newLog, oldParsed) => {
const cloneOldLog = [...oldParsed];
const lastIndex = cloneOldLog.length - 1;
const last = cloneOldLog[lastIndex];
const firstNew = newLog[0];
if (last.offset === firstNew.offset || (last.line && last.line.offset === firstNew.offset)) {
cloneOldLog.splice(lastIndex);
} else if (last.lines && last.lines.length) {
const lastNestedIndex = last.lines.length - 1;
const lastNested = last.lines[lastNestedIndex];
if (lastNested.offset === firstNew.offset) {
last.lines.splice(lastNestedIndex);
}
}
return cloneOldLog;
};
/**
* When the trace is not complete, backend may send the last received line
* in the new response.
......
......@@ -3,6 +3,7 @@ import {
updateIncrementalTrace,
parseHeaderLine,
parseLine,
findOffsetAndRemove,
} from '~/jobs/store/utils';
import {
utilsMockData,
......@@ -83,6 +84,91 @@ describe('Jobs Store Utils', () => {
});
});
describe('findOffsetAndRemove', () => {
describe('when last item is header', () => {
const existingLog = [
{
isHeader: true,
isClosed: true,
line: { content: [{ text: 'bar' }], offset: 10, lineNumber: 1 },
},
];
describe('and matches the offset', () => {
it('returns an array with the item removed', () => {
const newData = [{ offset: 10, content: [{ text: 'foobar' }] }];
const result = findOffsetAndRemove(newData, existingLog);
expect(result).toEqual([]);
});
});
describe('and does not match the offset', () => {
it('returns the provided existing log', () => {
const newData = [{ offset: 110, content: [{ text: 'foobar' }] }];
const result = findOffsetAndRemove(newData, existingLog);
expect(result).toEqual(existingLog);
});
});
});
describe('when last item is a regular line', () => {
const existingLog = [{ content: [{ text: 'bar' }], offset: 10, lineNumber: 1 }];
describe('and matches the offset', () => {
it('returns an array with the item removed', () => {
const newData = [{ offset: 10, content: [{ text: 'foobar' }] }];
const result = findOffsetAndRemove(newData, existingLog);
expect(result).toEqual([]);
});
});
describe('and does not match the fofset', () => {
it('returns the provided old log', () => {
const newData = [{ offset: 101, content: [{ text: 'foobar' }] }];
const result = findOffsetAndRemove(newData, existingLog);
expect(result).toEqual(existingLog);
});
});
});
describe('when last item is nested', () => {
const existingLog = [
{
isHeader: true,
isClosed: true,
lines: [{ offset: 101, content: [{ text: 'foobar' }], lineNumber: 2 }],
line: {
offset: 10,
lineNumber: 1,
section_duration: '10:00',
},
},
];
describe('and matches the offset', () => {
it('returns an array with the last nested line item removed', () => {
const newData = [{ offset: 101, content: [{ text: 'foobar' }] }];
const result = findOffsetAndRemove(newData, existingLog);
expect(result[0].lines).toEqual([]);
});
});
describe('and does not match the offset', () => {
it('returns the provided old log', () => {
const newData = [{ offset: 120, content: [{ text: 'foobar' }] }];
const result = findOffsetAndRemove(newData, existingLog);
expect(result).toEqual(existingLog);
});
});
});
});
describe('updateIncrementalTrace', () => {
describe('without repeated section', () => {
it('concats and parses both arrays', () => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment