Skip to content

Commit

Permalink
✅ (dmk) [NO-ISSUE]: Fix open handles in dmk tests (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
valpinkman authored Nov 14, 2024
2 parents df2b2f1 + bed562b commit bbff1e6
Show file tree
Hide file tree
Showing 12 changed files with 283 additions and 203 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"rimraf": "^6.0.1",
"ts-jest": "^29.2.5",
"tsc-alias": "^1.8.10",
"turbo": "^2.2.1",
"turbo": "^2.2.3",
"typescript": "^5.6.3",
"zx": "^8.1.9"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ describe("SendCommandUseCase", () => {
command,
});

deviceSession.close();

expect(response).toStrictEqual({
status: CommandResultStatus.Success,
data: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe("getJSONStringifyReplacer", () => {
const expected = `{"id":"mockedSessionId","connectedDevice":{"deviceModel":${JSON.stringify(
stubDeviceModel,
)},"type":"USB","id":"mockedDeviceId"}}`;
value.close();
expect(result).toEqual(expected);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,52 +40,61 @@ describe("DefaultDeviceSessionService", () => {

it("should have an empty sessions list", () => {
expect(sessionService.getDeviceSessions()).toEqual([]);
deviceSession.close();
});

it("should add a deviceSession", () => {
sessionService.addDeviceSession(deviceSession);
deviceSession.close();
expect(sessionService.getDeviceSessions()).toEqual([deviceSession]);
});

it("should not add a deviceSession if it already exists", () => {
sessionService.addDeviceSession(deviceSession);
sessionService.addDeviceSession(deviceSession);
deviceSession.close();
expect(sessionService.getDeviceSessions()).toEqual([deviceSession]);
});

it("should remove a deviceSession", () => {
sessionService.addDeviceSession(deviceSession);
deviceSession.close();
sessionService.removeDeviceSession(deviceSession.id);
expect(sessionService.getDeviceSessions()).toEqual([]);
});

it("should not remove a deviceSession if it does not exist", () => {
deviceSession.close();
sessionService.removeDeviceSession(deviceSession.id);
expect(sessionService.getDeviceSessions()).toEqual([]);
});

it("should get a deviceSession", () => {
sessionService.addDeviceSession(deviceSession);
deviceSession.close();
expect(sessionService.getDeviceSessionById(deviceSession.id)).toEqual(
Either.of(deviceSession),
);
});

it("should not get a deviceSession if it does not exist", () => {
deviceSession.close();
expect(sessionService.getDeviceSessionById(deviceSession.id)).toEqual(
Left(new DeviceSessionNotFound()),
);
});

it("should get all sessions", () => {
sessionService.addDeviceSession(deviceSession);
deviceSession.close();
expect(sessionService.getDeviceSessions()).toEqual([deviceSession]);
});

it("should retrieve sessionObs", () => {
expect(sessionService.sessionsObs).toBeInstanceOf(
Observable<DeviceSession>,
);
deviceSession.close();
});

it("should emit new session", (done) => {
Expand All @@ -97,6 +106,7 @@ describe("DefaultDeviceSessionService", () => {
},
});
sessionService.addDeviceSession(deviceSession);
deviceSession.close();
});

it("should emit previous added session", () => {
Expand All @@ -114,6 +124,8 @@ describe("DefaultDeviceSessionService", () => {
emittedSessions.push(emittedDeviceSession);
},
});
deviceSession.close();
lastDeviceSession.close();
expect(emittedSessions).toEqual([deviceSession, lastDeviceSession]);
subscription.unsubscribe();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ describe("GetDeviceSessionStateUseCase", () => {
sessionId: fakeSessionId,
});

deviceSession.close();

// then
expect(response).toStrictEqual(deviceSession.state);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ describe("GetConnectedDevice", () => {
sessionId: fakeSessionId,
});

deviceSession.close();

// then
expect(response).toBeInstanceOf(ConnectedDevice);
});
Expand All @@ -68,6 +70,8 @@ describe("GetConnectedDevice", () => {
sessionId: fakeSessionId,
});

deviceSession.close();

// then
expect(response).toStrictEqual(
new ConnectedDevice({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ describe("ListDeviceSessionsUseCase", () => {
// when
const response = useCase.execute();

deviceSession1.close();
deviceSession2.close();

// then
expect(response).toStrictEqual([
new ConnectedDevice({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,16 @@ describe("ListenToConnectedDevice", () => {
sessionId: fakeSessionId,
}),
);
subscription.unsubscribe();
done();
terminate();
},
});

function terminate() {
subscription.unsubscribe();
deviceSession.close();
done();
}

// when
sessionService.addDeviceSession(deviceSession);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe("SendApduUseCase", () => {
apdu: new Uint8Array([0x00, 0x01, 0x02, 0x03]),
});

deviceSession.close();
// then
expect(deviceSession.connectedDevice.sendApdu).toHaveBeenCalledTimes(1);
expect(response).toBeDefined();
Expand Down Expand Up @@ -91,6 +92,8 @@ describe("SendApduUseCase", () => {
apdu: new Uint8Array([0x00, 0x01, 0x02, 0x03]),
});

deviceSession.close();

// then
await expect(response).rejects.toBeInstanceOf(ReceiverApduError);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Right } from "purify-ts";

import { defaultApduResponseStubBuilder } from "@api/device-session/ApduResponse.stub";
import { deviceModelStubBuilder } from "@internal/device-model/model/DeviceModel.stub";
import { InternalConnectedDevice } from "@internal/transport/model/InternalConnectedDevice";
Expand Down Expand Up @@ -30,8 +32,8 @@ describe("InternalConnectedDevice", () => {
});

it("should return the correct send apdu response", () => {
expect(connectedDevice.sendApdu(new Uint8Array())).toMatchObject(
Promise.resolve(defaultApduResponseStubBuilder()),
expect(connectedDevice.sendApdu(new Uint8Array())).resolves.toMatchObject(
Right(defaultApduResponseStubBuilder()),
);
});
});
Loading

0 comments on commit bbff1e6

Please sign in to comment.