-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifyImage.test.ts
99 lines (88 loc) · 2.88 KB
/
classifyImage.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { describe, it, expect } from "vitest";
import classifyImage from "../src";
import * as metadata from "./testFiles/metadata.json";
const model = "https://teachablemachine.withgoogle.com/models/jAIOHvmge";
const imageHand = "https://www.stgeorges.nhs.uk/wp-content/uploads/2014/03/hand-2.jpeg";
const imageNoHand =
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Black_colour.jpg/640px-Black_colour.jpg";
const imageHandJPEG = "./test/images/hand.jpeg";
const imageHandPNG = "./test/images/hand.png";
describe("classifyImage function - Node", async () => {
describe("Result returns", async () => {
it("returns hand when shown a picture of a hand", async () => {
const result = await classifyImage(model, imageHand);
if (result instanceof Error) {
return new Error();
} else {
expect(result[0].label).toBe("Hand");
}
});
it("returns 'No hand' when shown a picture not including hand", async () => {
const result = await classifyImage(model, imageNoHand);
if (result instanceof Error) {
return new Error();
} else {
expect(result[0].label).toBe("No hand");
}
});
it("returns a probability level", async () => {
const result = await classifyImage(model, imageNoHand);
if (result instanceof Error) {
return new Error();
} else {
expect(result[0].probability).not.toBe(null);
}
});
it("returns when MODEL_DIR_PATH ends with slash", async () => {
const result = await classifyImage(model + "/", imageHand);
if (result instanceof Error) {
return new Error();
} else {
expect(result[0].label).toBe("Hand");
}
});
it("works with specified metadata", async () => {
const result = await classifyImage(model, imageNoHand, undefined, metadata);
if (result instanceof Error) {
return new Error();
} else {
expect(result[0].probability).not.toBe(null);
}
});
});
/* ERROR BOUNDRIES */
describe("Error boundries", async () => {
it("returns an error when missing a parameter", async () => {
//@ts-expect-error
const result = await classifyImage(imageNoHand);
expect(result).toBeInstanceOf(Error);
});
});
/* IMAGE TYPES */
describe("Image types", async () => {
it("returns a result on url image-input", async () => {
const result = await classifyImage(model, imageHand);
if (result instanceof Error) {
return new Error();
} else {
expect(result[0].label).toBe("Hand");
}
});
it("returns a result on JPEG image-input", async () => {
const result = await classifyImage(model, imageHandJPEG);
if (result instanceof Error) {
return new Error();
} else {
expect(result[0].label).toBe("Hand");
}
});
it("returns a result on PNG image-input", async () => {
const result = await classifyImage(model, imageHandPNG);
if (result instanceof Error) {
return new Error();
} else {
expect(result[0].label).toBe("Hand");
}
});
});
});