Commit c0a42580 authored by Filipa Lacerda's avatar Filipa Lacerda

Creates findAndReplaceOffset funtion

Creates a function to find the last offset and
remove it if repeated
parent 09020aea
......@@ -55,6 +55,39 @@ export const logLinesParser = (lines = [], lineNumberStart) =>
return acc;
}, []);
/**
* Finds the repeated offset, removes the old one
*
* Returns a new object with the updated log without
* the repeated offset and the last line number.
*
* @param Array newLog
* @param Array oldParsed
* @returns Object
*
*/
export const findOffsetAndRemove = (newLog, oldParsed) => {
const cloneOldLog = [...oldParsed];
const lastIndex = cloneOldLog.length - 1;
const last = cloneOldLog[lastIndex];
const firstNew = newLog[0];
const parsed = {};
if (last.offset === firstNew.offset || (last.line && last.line.offset === firstNew.offset)) {
cloneOldLog.splice(lastIndex);
parsed.lastLine = last.lineNumber;
} 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);
parsed.lastLine = lastNested.lineNumber;
}
}
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,59 @@ describe('Jobs Store Utils', () => {
});
});
describe('findOffsetAndRemove', () => {
describe('when last item is header', () => {
describe('when last item matches the offset', () => {
it('returns an object with the item removed and the lastLine', () => {
const newData = [{ offset: 10, content: [{ text: 'foobar' }] }];
const existingLog = [{ line: { content: [{ text: 'bar' }], offset: 10, lineNumber: 1 } }];
const result = findOffsetAndRemove(newData, existingLog);
expect(result).toEqual([]);
});
});
});
describe('when last item is a regular line', () => {
describe('and matches the offset', () => {
it('returns an object with the item removed and the lastLine', () => {
const newData = [{ offset: 10, content: [{ text: 'foobar' }] }];
const existingLog = [{ content: [{ text: 'bar' }], offset: 10, lineNumber: 1 }];
const result = findOffsetAndRemove(newData, existingLog);
expect(result).toEqual([]);
});
});
});
describe('when last collaspible line item matches the offset', () => {
it('returns an object with the last nested line item removed and the lastLine', () => {
const newData = [{ offset: 101, content: [{ text: 'foobar' }] }];
const existingLog = [
{
isHeader: true,
isClosed: true,
lines: [{ offset: 101, content: [{ text: 'foobar' }], lineNumber: 2 }],
line: {
offset: 10,
lineNumber: 1,
section_duration: '10:00',
},
},
];
const result = findOffsetAndRemove(newData, existingLog);
expect(result[0].lines).toEqual([]);
});
});
describe('when it does not match the offset', () => {
it('returns an object with the complete old log and the last line number', () => {
const newData = [{ offset: 101, content: [{ text: 'foobar' }] }];
const existingLog = [{ line: { content: [{ text: 'bar' }], offset: 10, lineNumber: 1 } }];
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