Skip to content

Commit

Permalink
Add fallback to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cheran-senthil committed Aug 27, 2024
1 parent f6634a4 commit e41c246
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 18 deletions.
63 changes: 48 additions & 15 deletions cypress/e2e/manifest-failure.cy.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,57 @@
import {TestHarness} from './harness';
import {ContentNode, TopologyNode} from './nodes';

it('manifest fetch on content nodes failure', () => {
const projectId = 'test-proj';
const content0 = new ContentNode('content0', projectId, 'manifest-does-not-exist');
const topology0 = new TopologyNode('topology0', [content0]);
describe('Manifest fetch tests', () => {
it('manifest fetch on content nodes failure', () => {
const projectId = 'test-proj';
const content0 = new ContentNode('content0', projectId);
const topology0 = new TopologyNode('topology0', [content0]);

const harness = new TestHarness(projectId, [topology0], [content0]);
harness.intercept();
harness.spyOnMessages(window);
const harness = new TestHarness(projectId, [topology0], [content0]);
harness.intercept();
harness.spyOnMessages(window);

content0.failResource('earthfast.json');
// Fail both primary and fallback manifest files
content0.failResource('earthfast.json');
content0.failResource('armada.json');

cy.visit(harness.domainNode.url);
cy.visit(harness.domainNode.url);

cy.wrap(harness).should(harness => {
// a single node failed
expect(harness.messageTypes).to.include('MANIFEST_FETCH_ERROR');
cy.wrap(harness).should(harness => {
// We expect two MANIFEST_FETCH_ERROR messages now, one for each file
expect(harness.messageTypes.filter(type => type === 'MANIFEST_FETCH_ERROR').length)
.to.equal(2);

// all nodes failed
expect(harness.messageTypes).to.include('MANIFEST_FETCH_FAILURE_NO_CONSENSUS');
// all nodes failed for both files
expect(harness.messageTypes).to.include('MANIFEST_FETCH_FAILURE_NO_CONSENSUS');
});
});
});

it('manifest fetch fallback succeeds', () => {
const projectId = 'test-proj';
const content0 = new ContentNode('content0', projectId);
const topology0 = new TopologyNode('topology0', [content0]);

const harness = new TestHarness(projectId, [topology0], [content0]);
harness.intercept();
harness.spyOnMessages(window);

// Fail the primary manifest file, but allow the fallback to succeed
content0.failResource('earthfast.json');
content0.addResource('armada.json', '{"version": "1"}');

cy.visit(harness.domainNode.url);

cy.wrap(harness).should(harness => {
// We expect one MANIFEST_FETCH_ERROR message for the primary file
expect(harness.messageTypes.filter(type => type === 'MANIFEST_FETCH_ERROR').length)
.to.equal(1);

// We don't expect a MANIFEST_FETCH_FAILURE_NO_CONSENSUS message
expect(harness.messageTypes).not.to.include('MANIFEST_FETCH_FAILURE_NO_CONSENSUS');

// We might expect a successful initialization message, depending on your implementation
expect(harness.messageTypes).to.include('INITIALIZED');
});
});
});
23 changes: 20 additions & 3 deletions cypress/e2e/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export abstract class FakeNode {

export class ContentNode extends FakeNode {
protected failingResources: Set<string> = new Set();
protected customResources: Map<string, string> = new Map();

constructor(hostname: string, public projectId: string, public site?: string) {
super(hostname);
Expand All @@ -27,7 +28,7 @@ export class ContentNode extends FakeNode {
},
},
(req) => {
if (!this.site) {
if (!this.site && !this.customResources.size) {
req.reply(410);
return;
}
Expand All @@ -38,8 +39,20 @@ export class ContentNode extends FakeNode {
return;
}

const fixture = join(this.site, 'e2e', resource);
req.reply({fixture: fixture});
if (this.customResources.has(resource)) {
req.reply({
statusCode: 200,
body: this.customResources.get(resource),
});
return;
}

if (this.site) {
const fixture = join(this.site, 'e2e', resource);
req.reply({fixture: fixture});
} else {
req.reply(404);
}
},
);
}
Expand All @@ -51,6 +64,10 @@ export class ContentNode extends FakeNode {
public failResource(resource: string) {
this.failingResources.add(resource);
}

public addResource(resource: string, content: string) {
this.customResources.set(resource, content);
}
}

export class OfflineContentNode extends ContentNode {
Expand Down

0 comments on commit e41c246

Please sign in to comment.