Skip to content

Commit

Permalink
fix status object logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake Holifield committed Jul 12, 2024
1 parent 7ffe8de commit 466f67b
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 5 deletions.
97 changes: 97 additions & 0 deletions src/common/pdfCache.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import PdfCache, { PDFComponent, PdfStatus } from './pdfCache';

describe('Pdf Cache updates', () => {
const pdfCache = PdfCache.getInstance();
const cache: PDFComponent = {
status: PdfStatus.Failed,
filepath: '',
collectionId: '7855a523-fc64-4e51-910a-b1ff3918b440',
componentId: 'fb99bc16-4cdc-4200-afbf-479d904ee987',
numPages: 0,
error: 'oops',
};
const baseId = '7855a523-fc64-4e51-910a-b1ff3918b440';
pdfCache.addToCollection(baseId, cache);

it('should return a valid collection', () => {
pdfCache.verifyCollection(baseId);
const coll = pdfCache.getCollection(baseId);
expect(coll.components.length).toBe(1);
expect(coll.components[0].componentId).toBe(
'fb99bc16-4cdc-4200-afbf-479d904ee987'
);
});

it('should validate a generated collection with all expected components', () => {
const comp: PDFComponent = {
status: PdfStatus.Generated,
filepath: 'blah',
collectionId: '9855a523-fc64-4e51-910a-b1ff3918b440',
componentId: 'fb99bc16-4cdc-4200-afbf-479d904ee987',
numPages: 5,
};
const another: PDFComponent = {
status: PdfStatus.Generated,
filepath: 'blah',
collectionId: '9855a523-fc64-4e51-910a-b1ff3918b440',
componentId: 'aaaaaaa-4cdc-4200-afbf-479d904ee987',
numPages: 5,
};
const compId = comp.collectionId;
pdfCache.addToCollection(compId, comp);
pdfCache.setExpectedLength(compId, 2);
pdfCache.verifyCollection(compId);
const unfinished = pdfCache.getCollection(compId);
expect(unfinished.expectedLength).toBe(2);
expect(unfinished.components.length).toBe(1);
expect(unfinished.status).toBe(PdfStatus.Generating);
pdfCache.addToCollection(compId, another);
pdfCache.verifyCollection(compId);
const finished = pdfCache.getCollection(compId);
expect(finished.expectedLength).toBe(2);
expect(finished.components.length).toBe(2);
expect(finished.status).toBe(PdfStatus.Generated);
});

it('should invalidate a failed collection', () => {
pdfCache.verifyCollection(baseId);
const coll = pdfCache.getCollection(baseId);
expect(coll.components.length).toBe(1);
expect(coll.status).toBe('Failed');
expect(coll.error).toBe('oops');
});

it('should reset a collection with no expectedLength', () => {
const comp: PDFComponent = {
status: PdfStatus.Generating,
filepath: 'blah',
collectionId: '9855a523-fc64-4e51-910a-b1ff3918b440',
componentId: 'fb99bc16-4cdc-4200-afbf-479d904ee987',
numPages: 5,
};
const compId = comp.collectionId;
pdfCache.addToCollection(compId, comp);
pdfCache.verifyCollection(compId);
const unfinished = pdfCache.getCollection(compId);
expect(unfinished.expectedLength).toBe(0);
expect(unfinished.components.length).toBe(1);
expect(unfinished.status).toBe(PdfStatus.Generating);
});

it('should reset a collection with no expectedLength', () => {
const comp: PDFComponent = {
status: PdfStatus.Generating,
filepath: 'blah',
collectionId: '9855a523-fc64-4e51-910a-b1ff3918b440',
componentId: 'fb99bc16-4cdc-4200-afbf-479d904ee987',
numPages: 5,
};
const compId = comp.collectionId;
pdfCache.addToCollection(compId, comp);
pdfCache.verifyCollection(compId);
const unfinished = pdfCache.getCollection(compId);
expect(unfinished.expectedLength).toBe(0);
expect(unfinished.components.length).toBe(1);
expect(unfinished.status).toBe(PdfStatus.Generating);
});
});
20 changes: 15 additions & 5 deletions src/common/pdfCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,24 @@ class PdfCache {
return;
}
const components = this.data[collectionId].components;
if (
!this.data[collectionId].expectedLength ||
this.data[collectionId].expectedLength !== components.length
) {

for (const component of components) {
if (component.status === PdfStatus.Failed) {
this.invalidateCollection(collectionId, component.error || '');
return;
}
}

if (!this.data[collectionId].expectedLength) {
this.data[collectionId].expectedLength = 0;
return;
}

if (
components.every((component) => component.status === PdfStatus.Generated)
components.every(
(component) => component.status === PdfStatus.Generated
) &&
this.data[collectionId].expectedLength === components.length
) {
this.updateCollectionState(collectionId, PdfStatus.Generated);
}
Expand Down

0 comments on commit 466f67b

Please sign in to comment.