Skip to content

Commit f1777e5

Browse files
committed
test: add unit tests using jest
1 parent 67e8ab4 commit f1777e5

File tree

11 files changed

+788
-13
lines changed

11 files changed

+788
-13
lines changed

jest.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Config } from "@jest/types";
2+
3+
const config: Config.InitialOptions = {
4+
verbose: true,
5+
transform: {
6+
"^.+\\.tsx?$": "ts-jest",
7+
},
8+
};
9+
export default config;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
],
1111
"scripts": {
1212
"build": "tsup",
13-
"watch": "tsup --watch"
13+
"watch": "tsup --watch",
14+
"test": "jest"
1415
},
1516
"keywords": [
1617
"asterisk",

src/event.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class Event extends Message {
3333
*/
3434
completed(): boolean {
3535
return (
36-
new RegExp(`\\bComplete\\b`, "i").test(this.event) ||
36+
new RegExp(`Complete`, "i").test(this.event) ||
3737
this.eventlistCompleted()
3838
);
3939
}
@@ -48,8 +48,8 @@ export class Event extends Message {
4848
if (!this.eventlist) return false;
4949

5050
return (
51-
new RegExp(`\\bComplete\\b`, "i").test(this.eventlist) ||
52-
new RegExp(`\\DBGetResponse\\b`, "i").test(this.eventlist)
51+
new RegExp(`Complete`, "i").test(this.eventlist) ||
52+
new RegExp(`DBGetResponse`, "i").test(this.eventlist)
5353
);
5454
}
5555
}

test/action.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { Action } from "../src/action";
2+
3+
describe("Action", () => {
4+
test("should create an Action with a specific name", () => {
5+
const action = new Action("TestAction");
6+
expect(action).toBeInstanceOf(Action);
7+
expect(action["Action"]).toBe("TestAction");
8+
});
9+
10+
test("should assign a unique ActionID to each Action", () => {
11+
const action1 = new Action("TestAction1");
12+
const action2 = new Action("TestAction2");
13+
14+
expect(action1.ActionID).toBeDefined();
15+
expect(action2.ActionID).toBeDefined();
16+
expect(action1.ActionID).not.toBe(action2.ActionID);
17+
});
18+
19+
test("should marshall to a properly formatted AMI action string", () => {
20+
const action = new Action("TestAction");
21+
action["Key1"] = "Value1";
22+
action["Key2"] = "Value2";
23+
24+
const result = action.marshall();
25+
26+
expect(result).toContain("Action: TestAction");
27+
expect(result).toContain(`ActionID: ${action.ActionID}`);
28+
expect(result).toContain("Key1: Value1");
29+
expect(result).toContain("Key2: Value2");
30+
expect(result.endsWith("\r\n\r\n")).toBe(true);
31+
});
32+
33+
test("should properly include variables", () => {
34+
const action = new Action("TestAction");
35+
action["variables"] = {
36+
channel: "SIP/1234",
37+
context: "default"
38+
};
39+
40+
const result = action.marshall();
41+
42+
expect(result).toContain("Action: TestAction");
43+
expect(result).toContain(`ActionID: ${action.ActionID}`);
44+
expect(result).toContain("Variable: channel=SIP/1234");
45+
expect(result).toContain("Variable: context=default");
46+
});
47+
48+
test("asJson should return a plain object with action properties", () => {
49+
const action = new Action("TestAction");
50+
action["Key1"] = "Value1";
51+
52+
const json = action.asJson();
53+
54+
expect(json).toHaveProperty("Action", "TestAction");
55+
expect(json).toHaveProperty("ActionID");
56+
expect(json).toHaveProperty("Key1", "Value1");
57+
expect(json).not.toHaveProperty("variables");
58+
expect(json).not.toHaveProperty("EOL");
59+
});
60+
});

test/actions.test.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import { Login } from "../src/actions/Login";
2+
import { Logoff } from "../src/actions/Logoff";
3+
import { Ping } from "../src/actions/Ping";
4+
import { Hangup } from "../src/actions/Hangup";
5+
import { Status } from "../src/actions/Status";
6+
import { CoreShowChannels } from "../src/actions/CoreShowChannels";
7+
import { CustomAction } from "../src/actions/CustomAction";
8+
9+
describe("AMI Actions", () => {
10+
describe("Login", () => {
11+
test("should create a Login action with username and password", () => {
12+
const username = "admin";
13+
const password = "secret";
14+
const login = new Login(username, password);
15+
16+
expect(login).toBeInstanceOf(Login);
17+
const marshalled = login.marshall();
18+
19+
expect(marshalled).toContain("Action: Login");
20+
expect(marshalled).toContain("Username: admin");
21+
expect(marshalled).toContain("Secret: secret");
22+
});
23+
});
24+
25+
describe("Logoff", () => {
26+
test("should create a Logoff action", () => {
27+
const logoff = new Logoff();
28+
29+
expect(logoff).toBeInstanceOf(Logoff);
30+
const marshalled = logoff.marshall();
31+
32+
expect(marshalled).toContain("Action: Logoff");
33+
});
34+
});
35+
36+
describe("Ping", () => {
37+
test("should create a Ping action", () => {
38+
const ping = new Ping();
39+
40+
expect(ping).toBeInstanceOf(Ping);
41+
const marshalled = ping.marshall();
42+
43+
expect(marshalled).toContain("Action: Ping");
44+
});
45+
});
46+
47+
describe("Hangup", () => {
48+
test("should create a Hangup action with channel name", () => {
49+
const channel = "SIP/1234-00000123";
50+
const hangup = new Hangup(channel);
51+
52+
expect(hangup).toBeInstanceOf(Hangup);
53+
const marshalled = hangup.marshall();
54+
55+
expect(marshalled).toContain("Action: Hangup");
56+
expect(marshalled).toContain(`Channel: ${channel}`);
57+
});
58+
59+
test("should create a Hangup action with channel name and cause", () => {
60+
const channel = "SIP/1234-00000123";
61+
const cause = "16";
62+
const hangup = new Hangup(channel, cause);
63+
64+
expect(hangup).toBeInstanceOf(Hangup);
65+
const marshalled = hangup.marshall();
66+
67+
expect(marshalled).toContain("Action: Hangup");
68+
expect(marshalled).toContain(`Channel: ${channel}`);
69+
expect(marshalled).toContain(`Cause: ${cause}`);
70+
});
71+
});
72+
73+
describe("Status", () => {
74+
test("should create a Status action without channel", () => {
75+
const status = new Status();
76+
77+
expect(status).toBeInstanceOf(Status);
78+
const marshalled = status.marshall();
79+
80+
expect(marshalled).toContain("Action: Status");
81+
expect(marshalled).not.toContain("Channel:");
82+
});
83+
84+
test("should create a Status action with channel name", () => {
85+
const channel = "SIP/1234-00000123";
86+
const status = new Status(channel);
87+
88+
expect(status).toBeInstanceOf(Status);
89+
const marshalled = status.marshall();
90+
91+
expect(marshalled).toContain("Action: Status");
92+
expect(marshalled).toContain(`Channel: ${channel}`);
93+
});
94+
});
95+
96+
describe("CoreShowChannels", () => {
97+
test("should create a CoreShowChannels action", () => {
98+
const coreShowChannels = new CoreShowChannels();
99+
100+
expect(coreShowChannels).toBeInstanceOf(CoreShowChannels);
101+
const marshalled = coreShowChannels.marshall();
102+
103+
expect(marshalled).toContain("Action: CoreShowChannels");
104+
});
105+
});
106+
107+
describe("CustomAction", () => {
108+
test("should create a custom action with specified name", () => {
109+
const actionName = "CustomActionName";
110+
const customAction = new CustomAction({ name: actionName });
111+
112+
expect(customAction).toBeInstanceOf(CustomAction);
113+
const marshalled = customAction.marshall();
114+
115+
expect(marshalled).toContain(`Action: ${actionName}`);
116+
});
117+
118+
test("should add parameters to custom action", () => {
119+
const actionName = "CustomActionName";
120+
const customAction = new CustomAction({
121+
name: actionName,
122+
params: {
123+
Param1: "Value1",
124+
Param2: "Value2"
125+
}
126+
});
127+
128+
const marshalled = customAction.marshall();
129+
130+
expect(marshalled).toContain(`Action: ${actionName}`);
131+
expect(marshalled).toContain("Param1: Value1");
132+
expect(marshalled).toContain("Param2: Value2");
133+
});
134+
});
135+
});

0 commit comments

Comments
 (0)