Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: publishReply has incorrect Event in reply #141

Merged
merged 9 commits into from
Sep 11, 2023
12 changes: 10 additions & 2 deletions source/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Event {
constructor(
topic: string,
data: object,
options: { [key: string]: object } = {}
options: { [key: string]: unknown } = {}
gismya marked this conversation as resolved.
Show resolved Hide resolved
) {
this._data = {
topic,
Expand All @@ -41,7 +41,15 @@ export class Event {
return this._data;
}

/** Add source to event data. */
/** Add source to event data, keeping any already avalable source information */
prepareSource(source: object): void {
this._data.source = {
...source,
...this._data.source,
};
}

/** Add source to event data, replacing any previous data. */
addSource(source: any): void {
this._data.source = source;
}
Expand Down
8 changes: 3 additions & 5 deletions source/event_hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export class EventHub {
);
}

event.addSource({
event.prepareSource({
id: this._id,
applicationId: this._applicationId,
user: {
Expand Down Expand Up @@ -613,13 +613,11 @@ export class EventHub {
data: Data,
source: Data | null = null
): Promise<string> {
const replyEvent = new Event("ftrack.meta.reply", {
...data,
const replyEvent = new Event("ftrack.meta.reply", data, {
target: `id=${sourceEventPayload.source.id}`,
inReplyToEvent: sourceEventPayload.id,
source: source ?? data.source,
source: source,
});

return this.publish(replyEvent);
}
}
98 changes: 98 additions & 0 deletions test/event.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// :copyright: Copyright (c) 2023 ftrack
gismya marked this conversation as resolved.
Show resolved Hide resolved

import { Event } from "../source/event";
import { describe, expect, it } from "vitest";

describe("Event class", () => {
it("should initialize with correct topic and data", () => {
const event = new Event("testTopic", { key: "value" });
const data = event.getData();
expect(data.topic).toBe("testTopic");
expect(data.data).toEqual({ key: "value" });
});

it("should have default properties", () => {
const event = new Event("testTopic", { key: "value" });
const data = event.getData();
expect(data.target).toBe("");
expect(data.inReplyToEvent).toBeNull();
});

it("should set properties from options", () => {
const event = new Event(
"testTopic",
{ key: "value" },
{ target: "sampleTarget", customOption: "customValue" }
);
const data = event.getData();
expect(data.target).toBe("sampleTarget");
expect(data.customOption).toBe("customValue");
});

it("should generate unique UUID", () => {
const event1 = new Event("testTopic", { key: "value" });
const event2 = new Event("testTopic", { key: "value" });
const data1 = event1.getData();
const data2 = event2.getData();
const uuidRegexExp =
/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/gi;
expect(data1.id).not.toBe(data2.id);
expect(uuidRegexExp.test(data1.id)).toBe(true);
});

it("should add source to event data", () => {
const event = new Event("testTopic", { key: "value" });
const source = {
id: "testId",
applicationId: "testApplicationId",
user: {
username: "testUser",
},
};
event.addSource(source);
const data = event.getData();
expect(data.source).toBe(source);
});
describe("prepareSource Method", () => {
it("should prepare and add source to event data", () => {
const event = new Event("testTopic", { key: "value" });
event.prepareSource({ newKey: "newValue" });
const data = event.getData();
expect(data.source).toEqual({ newKey: "newValue" });
});

it("should not override existing source when preparing a new source", () => {
const event = new Event(
"testTopic",
{ key: "value" },
{ source: { oldKey: "oldValue" } }
);
event.prepareSource({ oldKey: "newValue", newKey: "newValue" });
const data = event.getData();
expect(data.source).toEqual({
oldKey: "oldValue",
newKey: "newValue",
});
});
it("should handle source undefined", () => {
const event = new Event(
"testTopic",
{ key: "value" },
{ source: undefined }
);
event.prepareSource({ newKey: "newValue" });
const data = event.getData();
expect(data.source).toEqual({
newKey: "newValue",
});
});
it("Prepare should handle source null", () => {
const event = new Event("testTopic", { key: "value" }, { source: null });
event.prepareSource({ newKey: "newValue" });
const data = event.getData();
expect(data.source).toEqual({
newKey: "newValue",
});
});
});
});
32 changes: 30 additions & 2 deletions test/event_hub.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventHub } from "../source/event_hub";
import { vi, describe, expect } from "vitest";

import { Event } from "../source/event";
describe("EventHub", () => {
let eventHub;

Expand Down Expand Up @@ -184,7 +184,6 @@ describe("EventHub", () => {
topic: "ftrack.test",
data: {},
id: "eventId",
source: { id: "sourceId" },
};

const publishReplySpy = vi
Expand All @@ -208,4 +207,33 @@ describe("EventHub", () => {
);
publishReplySpy.mockRestore();
});
it("publishReply published Event with correct shape", async () => {
eventHub.publish = vi.fn();

const sourceEventPayload = {
source: { id: "testId" },
id: "anotherTestId",
};

const data = {
someData: "value",
};

await eventHub.publishReply(sourceEventPayload, data);

const publishedEvent = eventHub.publish.mock.calls[0][0];
expect(publishedEvent).toBeInstanceOf(Event);
const EventData = publishedEvent.getData();
// Ignoring the id field for comparison
delete EventData.id;

const expectedEvent = {
topic: "ftrack.meta.reply",
data: { someData: "value" },
source: null,
target: "id=testId",
inReplyToEvent: "anotherTestId",
};
expect(EventData).toEqual(expectedEvent);
});
});