/
convert.test.ts
64 lines (54 loc) · 1.52 KB
/
convert.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
import "@tensorflow/tfjs-backend-wasm";
import { convert } from "../src/convert";
import { convertAsync } from "../src/convert-async";
import { SAMPLE } from "../__mocks__/sample";
import { ready, setBackend } from "@tensorflow/tfjs-core";
beforeAll(async () => {
await setBackend("wasm"); // set tensorflow wasm backend
await ready();
});
describe("convert base64 to tensor", () => {
test("use pure js", async () => {
const tensor = convert(SAMPLE);
expect(tensor).toEqual({
kept: false,
isDisposedInternal: false,
shape: [113, 150, 3],
dtype: "int32",
size: 50850,
strides: [450, 3],
dataId: { id: 1 },
id: 0,
rankType: "3",
});
tensor?.dispose();
});
test("use sharp", async () => {
const tensor = await convertAsync(SAMPLE);
expect(tensor).toEqual({
kept: false,
isDisposedInternal: false,
shape: [113, 150, 3],
dtype: "int32",
size: 50850,
strides: [450, 3],
dataId: { id: 3 }, // next tensor in line
id: 1,
rankType: "3",
});
tensor?.dispose();
});
test("handle missing jpeg data sync", async () => {
const tensor = convert("");
expect(tensor).toEqual(null);
});
test("handle missing jpeg data async", async () => {
const tensor = await convertAsync("");
expect(tensor).toEqual(null);
});
test("handle missing jpeg data error async", async () => {
return expect(convertAsync("dqdw")).rejects.toEqual(
new Error("Input Buffer is empty")
);
});
});