@simplysm/core-node 13.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cache/typecheck-node.tsbuildinfo +1 -0
- package/.cache/typecheck-tests-node.tsbuildinfo +1 -0
- package/README.md +375 -0
- package/dist/core-common/src/common.types.d.ts +74 -0
- package/dist/core-common/src/common.types.d.ts.map +1 -0
- package/dist/core-common/src/env.d.ts +6 -0
- package/dist/core-common/src/env.d.ts.map +1 -0
- package/dist/core-common/src/errors/argument-error.d.ts +25 -0
- package/dist/core-common/src/errors/argument-error.d.ts.map +1 -0
- package/dist/core-common/src/errors/not-implemented-error.d.ts +29 -0
- package/dist/core-common/src/errors/not-implemented-error.d.ts.map +1 -0
- package/dist/core-common/src/errors/sd-error.d.ts +27 -0
- package/dist/core-common/src/errors/sd-error.d.ts.map +1 -0
- package/dist/core-common/src/errors/timeout-error.d.ts +31 -0
- package/dist/core-common/src/errors/timeout-error.d.ts.map +1 -0
- package/dist/core-common/src/extensions/arr-ext.d.ts +15 -0
- package/dist/core-common/src/extensions/arr-ext.d.ts.map +1 -0
- package/dist/core-common/src/extensions/arr-ext.helpers.d.ts +19 -0
- package/dist/core-common/src/extensions/arr-ext.helpers.d.ts.map +1 -0
- package/dist/core-common/src/extensions/arr-ext.types.d.ts +215 -0
- package/dist/core-common/src/extensions/arr-ext.types.d.ts.map +1 -0
- package/dist/core-common/src/extensions/map-ext.d.ts +57 -0
- package/dist/core-common/src/extensions/map-ext.d.ts.map +1 -0
- package/dist/core-common/src/extensions/set-ext.d.ts +36 -0
- package/dist/core-common/src/extensions/set-ext.d.ts.map +1 -0
- package/dist/core-common/src/features/debounce-queue.d.ts +53 -0
- package/dist/core-common/src/features/debounce-queue.d.ts.map +1 -0
- package/dist/core-common/src/features/event-emitter.d.ts +66 -0
- package/dist/core-common/src/features/event-emitter.d.ts.map +1 -0
- package/dist/core-common/src/features/serial-queue.d.ts +47 -0
- package/dist/core-common/src/features/serial-queue.d.ts.map +1 -0
- package/dist/core-common/src/index.d.ts +32 -0
- package/dist/core-common/src/index.d.ts.map +1 -0
- package/dist/core-common/src/types/date-only.d.ts +152 -0
- package/dist/core-common/src/types/date-only.d.ts.map +1 -0
- package/dist/core-common/src/types/date-time.d.ts +96 -0
- package/dist/core-common/src/types/date-time.d.ts.map +1 -0
- package/dist/core-common/src/types/lazy-gc-map.d.ts +80 -0
- package/dist/core-common/src/types/lazy-gc-map.d.ts.map +1 -0
- package/dist/core-common/src/types/time.d.ts +68 -0
- package/dist/core-common/src/types/time.d.ts.map +1 -0
- package/dist/core-common/src/types/uuid.d.ts +35 -0
- package/dist/core-common/src/types/uuid.d.ts.map +1 -0
- package/dist/core-common/src/utils/bytes.d.ts +51 -0
- package/dist/core-common/src/utils/bytes.d.ts.map +1 -0
- package/dist/core-common/src/utils/date-format.d.ts +90 -0
- package/dist/core-common/src/utils/date-format.d.ts.map +1 -0
- package/dist/core-common/src/utils/json.d.ts +34 -0
- package/dist/core-common/src/utils/json.d.ts.map +1 -0
- package/dist/core-common/src/utils/num.d.ts +60 -0
- package/dist/core-common/src/utils/num.d.ts.map +1 -0
- package/dist/core-common/src/utils/obj.d.ts +258 -0
- package/dist/core-common/src/utils/obj.d.ts.map +1 -0
- package/dist/core-common/src/utils/path.d.ts +23 -0
- package/dist/core-common/src/utils/path.d.ts.map +1 -0
- package/dist/core-common/src/utils/primitive.d.ts +18 -0
- package/dist/core-common/src/utils/primitive.d.ts.map +1 -0
- package/dist/core-common/src/utils/str.d.ts +103 -0
- package/dist/core-common/src/utils/str.d.ts.map +1 -0
- package/dist/core-common/src/utils/template-strings.d.ts +84 -0
- package/dist/core-common/src/utils/template-strings.d.ts.map +1 -0
- package/dist/core-common/src/utils/transferable.d.ts +47 -0
- package/dist/core-common/src/utils/transferable.d.ts.map +1 -0
- package/dist/core-common/src/utils/wait.d.ts +19 -0
- package/dist/core-common/src/utils/wait.d.ts.map +1 -0
- package/dist/core-common/src/utils/xml.d.ts +36 -0
- package/dist/core-common/src/utils/xml.d.ts.map +1 -0
- package/dist/core-common/src/zip/sd-zip.d.ts +80 -0
- package/dist/core-common/src/zip/sd-zip.d.ts.map +1 -0
- package/dist/core-node/src/features/fs-watcher.d.ts +70 -0
- package/dist/core-node/src/features/fs-watcher.d.ts.map +1 -0
- package/dist/core-node/src/index.d.ts +7 -0
- package/dist/core-node/src/index.d.ts.map +1 -0
- package/dist/core-node/src/utils/fs.d.ts +197 -0
- package/dist/core-node/src/utils/fs.d.ts.map +1 -0
- package/dist/core-node/src/utils/path.d.ts +75 -0
- package/dist/core-node/src/utils/path.d.ts.map +1 -0
- package/dist/core-node/src/worker/create-worker.d.ts +23 -0
- package/dist/core-node/src/worker/create-worker.d.ts.map +1 -0
- package/dist/core-node/src/worker/types.d.ts +67 -0
- package/dist/core-node/src/worker/types.d.ts.map +1 -0
- package/dist/core-node/src/worker/worker.d.ts +27 -0
- package/dist/core-node/src/worker/worker.d.ts.map +1 -0
- package/dist/features/fs-watcher.js +100 -0
- package/dist/features/fs-watcher.js.map +7 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +7 -0
- package/dist/utils/fs.js +305 -0
- package/dist/utils/fs.js.map +7 -0
- package/dist/utils/path.js +48 -0
- package/dist/utils/path.js.map +7 -0
- package/dist/worker/create-worker.js +85 -0
- package/dist/worker/create-worker.js.map +7 -0
- package/dist/worker/types.js +1 -0
- package/dist/worker/types.js.map +7 -0
- package/dist/worker/worker.js +142 -0
- package/dist/worker/worker.js.map +7 -0
- package/lib/worker-dev-proxy.js +12 -0
- package/package.json +23 -0
- package/src/features/fs-watcher.ts +176 -0
- package/src/index.ts +11 -0
- package/src/utils/fs.ts +550 -0
- package/src/utils/path.ts +128 -0
- package/src/worker/create-worker.ts +141 -0
- package/src/worker/types.ts +86 -0
- package/src/worker/worker.ts +207 -0
- package/tests/utils/fs-watcher.spec.ts +295 -0
- package/tests/utils/fs.spec.ts +754 -0
- package/tests/utils/path.spec.ts +192 -0
- package/tests/worker/fixtures/test-worker.ts +35 -0
- package/tests/worker/sd-worker.spec.ts +183 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import {
|
|
4
|
+
pathPosix,
|
|
5
|
+
pathNorm,
|
|
6
|
+
pathIsChildPath,
|
|
7
|
+
pathChangeFileDirectory,
|
|
8
|
+
pathGetBasenameWithoutExt,
|
|
9
|
+
pathFilterByTargets,
|
|
10
|
+
type NormPath,
|
|
11
|
+
} from "../../src/utils/path";
|
|
12
|
+
|
|
13
|
+
describe("path ํจ์๋ค", () => {
|
|
14
|
+
//#region posix
|
|
15
|
+
|
|
16
|
+
describe("pathPosix", () => {
|
|
17
|
+
it("๋จ์ผ ๊ฒฝ๋ก ์ธ์๋ฅผ POSIX ์คํ์ผ๋ก ๋ณํ", () => {
|
|
18
|
+
const result = pathPosix("C:\\Users\\test\\file.txt");
|
|
19
|
+
expect(result).toBe("C:/Users/test/file.txt");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("์ฌ๋ฌ ๊ฒฝ๋ก ์ธ์๋ฅผ ์กฐํฉํ์ฌ POSIX ์คํ์ผ๋ก ๋ณํ", () => {
|
|
23
|
+
const result = pathPosix("C:\\Users", "test", "file.txt");
|
|
24
|
+
expect(result).toBe("C:/Users/test/file.txt");
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("์ด๋ฏธ POSIX ์คํ์ผ์ธ ๊ฒฝ๋ก๋ ๊ทธ๋๋ก ์ ์ง", () => {
|
|
28
|
+
const result = pathPosix("/usr/local/bin");
|
|
29
|
+
expect(result).toBe("/usr/local/bin");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("ํผํฉ๋ ๊ฒฝ๋ก ๊ตฌ๋ถ์ ์ฒ๋ฆฌ", () => {
|
|
33
|
+
const result = pathPosix("C:/Users\\test/file.txt");
|
|
34
|
+
expect(result).toBe("C:/Users/test/file.txt");
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
|
|
40
|
+
//#region norm
|
|
41
|
+
|
|
42
|
+
describe("pathNorm", () => {
|
|
43
|
+
it("๊ฒฝ๋ก๋ฅผ ์ ๊ทํํ๊ณ NormPath ํ์
์ผ๋ก ๋ฐํ", () => {
|
|
44
|
+
const result: NormPath = pathNorm("./test/../file.txt");
|
|
45
|
+
expect(result).toBe(path.resolve("./test/../file.txt"));
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("์ฌ๋ฌ ๊ฒฝ๋ก ์ธ์๋ฅผ ์กฐํฉํ์ฌ ์ ๊ทํ", () => {
|
|
49
|
+
const basePath = path.resolve("/base");
|
|
50
|
+
const result = pathNorm(basePath, "sub", "file.txt");
|
|
51
|
+
expect(result).toBe(path.resolve(basePath, "sub", "file.txt"));
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("์๋ ๊ฒฝ๋ก๋ฅผ ์ ๋ ๊ฒฝ๋ก๋ก ๋ณํ", () => {
|
|
55
|
+
const result = pathNorm("relative/path");
|
|
56
|
+
expect(path.isAbsolute(result)).toBe(true);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
|
|
62
|
+
//#region isChildPath
|
|
63
|
+
|
|
64
|
+
describe("pathIsChildPath", () => {
|
|
65
|
+
it("์์ ๊ฒฝ๋ก์ธ ๊ฒฝ์ฐ true ๋ฐํ", () => {
|
|
66
|
+
const parent = pathNorm("/parent/dir");
|
|
67
|
+
const child = pathNorm("/parent/dir/child/file.txt");
|
|
68
|
+
expect(pathIsChildPath(child, parent)).toBe(true);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("๊ฐ์ ๊ฒฝ๋ก์ธ ๊ฒฝ์ฐ false ๋ฐํ", () => {
|
|
72
|
+
const parent = pathNorm("/parent/dir");
|
|
73
|
+
const child = pathNorm("/parent/dir");
|
|
74
|
+
expect(pathIsChildPath(child, parent)).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("์์ ๊ฒฝ๋ก๊ฐ ์๋ ๊ฒฝ์ฐ false ๋ฐํ", () => {
|
|
78
|
+
const parent = pathNorm("/parent/dir");
|
|
79
|
+
const child = pathNorm("/other/dir/file.txt");
|
|
80
|
+
expect(pathIsChildPath(child, parent)).toBe(false);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("๋ถ๋ชจ ๊ฒฝ๋ก์ ์ผ๋ถ ๋ฌธ์์ด๋ง ์ผ์นํ๋ ๊ฒฝ์ฐ false ๋ฐํ", () => {
|
|
84
|
+
const parent = pathNorm("/parent/dir");
|
|
85
|
+
const child = pathNorm("/parent/directory/file.txt");
|
|
86
|
+
expect(pathIsChildPath(child, parent)).toBe(false);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
//#endregion
|
|
91
|
+
|
|
92
|
+
//#region changeFileDirectory
|
|
93
|
+
|
|
94
|
+
describe("pathChangeFileDirectory", () => {
|
|
95
|
+
it("ํ์ผ์ ๋๋ ํ ๋ฆฌ๋ฅผ ๋ณ๊ฒฝ", () => {
|
|
96
|
+
const file = pathNorm("/source/sub/file.txt");
|
|
97
|
+
const from = pathNorm("/source");
|
|
98
|
+
const to = pathNorm("/target");
|
|
99
|
+
|
|
100
|
+
const result = pathChangeFileDirectory(file, from, to);
|
|
101
|
+
expect(result).toBe(pathNorm("/target/sub/file.txt"));
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("์ค์ฒฉ๋ ๊ฒฝ๋ก์์ ๋๋ ํ ๋ฆฌ ๋ณ๊ฒฝ", () => {
|
|
105
|
+
const file = pathNorm("/a/b/c/d/file.txt");
|
|
106
|
+
const from = pathNorm("/a/b");
|
|
107
|
+
const to = pathNorm("/x/y");
|
|
108
|
+
|
|
109
|
+
const result = pathChangeFileDirectory(file, from, to);
|
|
110
|
+
expect(result).toBe(pathNorm("/x/y/c/d/file.txt"));
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("ํ์ผ์ด fromDirectory ์์ ์์ผ๋ฉด ์๋ฌ ๋ฐ์", () => {
|
|
114
|
+
const file = pathNorm("/other/path/file.txt");
|
|
115
|
+
const from = pathNorm("/source");
|
|
116
|
+
const to = pathNorm("/target");
|
|
117
|
+
|
|
118
|
+
expect(() => pathChangeFileDirectory(file, from, to)).toThrow();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("filePath์ fromDirectory๊ฐ ๋์ผํ ๊ฒฝ์ฐ toDirectory ๋ฐํ", () => {
|
|
122
|
+
const file = pathNorm("/source");
|
|
123
|
+
const from = pathNorm("/source");
|
|
124
|
+
const to = pathNorm("/target");
|
|
125
|
+
|
|
126
|
+
const result = pathChangeFileDirectory(file, from, to);
|
|
127
|
+
expect(result).toBe(to);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
//#endregion
|
|
132
|
+
|
|
133
|
+
//#region getBasenameWithoutExt
|
|
134
|
+
|
|
135
|
+
describe("pathGetBasenameWithoutExt", () => {
|
|
136
|
+
it("๋จ์ผ ํ์ฅ์ ์ ๊ฑฐ (basename๋ง ๋ฐํ)", () => {
|
|
137
|
+
const result = pathGetBasenameWithoutExt("/path/to/file.txt");
|
|
138
|
+
expect(result).toBe("file");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("๋ค์ค ํ์ฅ์์์ ๋ง์ง๋ง ํ์ฅ์๋ง ์ ๊ฑฐ", () => {
|
|
142
|
+
const result = pathGetBasenameWithoutExt("/path/to/file.spec.ts");
|
|
143
|
+
expect(result).toBe("file.spec");
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("ํ์ฅ์๊ฐ ์๋ ํ์ผ์ basename ๋ฐํ", () => {
|
|
147
|
+
const result = pathGetBasenameWithoutExt("/path/to/file");
|
|
148
|
+
expect(result).toBe("file");
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("์จ๊น ํ์ผ(์ ์ผ๋ก ์์)์ ๊ทธ๋๋ก ๋ฐํ", () => {
|
|
152
|
+
const result = pathGetBasenameWithoutExt("/path/to/.gitignore");
|
|
153
|
+
expect(result).toBe(".gitignore");
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
//#endregion
|
|
158
|
+
|
|
159
|
+
//#region filterByTargets
|
|
160
|
+
|
|
161
|
+
describe("pathFilterByTargets", () => {
|
|
162
|
+
const cwd = "/proj";
|
|
163
|
+
const files = ["/proj/src/a.ts", "/proj/src/b.ts", "/proj/tests/c.ts", "/proj/lib/d.ts"];
|
|
164
|
+
|
|
165
|
+
it("๋น ํ๊ฒ ๋ฐฐ์ด์ด๋ฉด ๋ชจ๋ ํ์ผ ๋ฐํ", () => {
|
|
166
|
+
const result = pathFilterByTargets(files, [], cwd);
|
|
167
|
+
expect(result).toEqual(files);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("๋จ์ผ ํ๊ฒ์ผ๋ก ํํฐ๋ง", () => {
|
|
171
|
+
const result = pathFilterByTargets(files, ["src"], cwd);
|
|
172
|
+
expect(result).toEqual(["/proj/src/a.ts", "/proj/src/b.ts"]);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("๋ค์ค ํ๊ฒ์ผ๋ก ํํฐ๋ง", () => {
|
|
176
|
+
const result = pathFilterByTargets(files, ["src", "tests"], cwd);
|
|
177
|
+
expect(result).toEqual(["/proj/src/a.ts", "/proj/src/b.ts", "/proj/tests/c.ts"]);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("๋งค์นญ๋๋ ํ์ผ์ด ์์ผ๋ฉด ๋น ๋ฐฐ์ด ๋ฐํ", () => {
|
|
181
|
+
const result = pathFilterByTargets(files, ["nonexistent"], cwd);
|
|
182
|
+
expect(result).toEqual([]);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("์ ํํ ํ์ผ ๊ฒฝ๋ก๋ก ํํฐ๋ง", () => {
|
|
186
|
+
const result = pathFilterByTargets(files, ["src/a.ts"], cwd);
|
|
187
|
+
expect(result).toEqual(["/proj/src/a.ts"]);
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
//#endregion
|
|
192
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { createWorker } from "../../../src";
|
|
2
|
+
|
|
3
|
+
interface TestWorkerEvents extends Record<string, unknown> {
|
|
4
|
+
progress: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const methods = {
|
|
8
|
+
add: (a: number, b: number) => {
|
|
9
|
+
sender.send("progress", 50);
|
|
10
|
+
return a + b;
|
|
11
|
+
},
|
|
12
|
+
echo: (message: string) => `Echo: ${message}`,
|
|
13
|
+
throwError: () => {
|
|
14
|
+
throw new Error("Intentional error");
|
|
15
|
+
},
|
|
16
|
+
delay: async (ms: number) => {
|
|
17
|
+
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
18
|
+
return ms;
|
|
19
|
+
},
|
|
20
|
+
noReturn: () => {},
|
|
21
|
+
logMessage: (message: string) => {
|
|
22
|
+
console.log(message);
|
|
23
|
+
return "logged";
|
|
24
|
+
},
|
|
25
|
+
crash: () => {
|
|
26
|
+
// ๋๊ธฐ์ ์ผ๋ก ์ฆ์ ์ข
๋ฃ
|
|
27
|
+
setImmediate(() => process.exit(1));
|
|
28
|
+
return "crashing...";
|
|
29
|
+
},
|
|
30
|
+
getEnv: (key: string) => process.env[key],
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const sender = createWorker<typeof methods, TestWorkerEvents>(methods);
|
|
34
|
+
|
|
35
|
+
export default sender;
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import type { WorkerProxy } from "../../src/worker/types";
|
|
4
|
+
import { Worker } from "../../src/worker/worker";
|
|
5
|
+
import type * as TestWorkerModule from "./fixtures/test-worker";
|
|
6
|
+
|
|
7
|
+
describe("SdWorker", () => {
|
|
8
|
+
const workerPath = path.resolve(import.meta.dirname, "fixtures/test-worker.ts");
|
|
9
|
+
let worker: WorkerProxy<typeof TestWorkerModule> | undefined;
|
|
10
|
+
|
|
11
|
+
afterEach(async () => {
|
|
12
|
+
if (worker) {
|
|
13
|
+
await worker.terminate();
|
|
14
|
+
worker = undefined;
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
//#region ๋ฉ์๋ ํธ์ถ
|
|
19
|
+
|
|
20
|
+
describe("๋ฉ์๋ ํธ์ถ", () => {
|
|
21
|
+
it("์์ปค ๋ฉ์๋ ํธ์ถ ๋ฐ ๊ฒฐ๊ณผ ๋ฐํ", async () => {
|
|
22
|
+
worker = Worker.create<typeof TestWorkerModule>(workerPath);
|
|
23
|
+
|
|
24
|
+
const result = await worker.add(10, 20);
|
|
25
|
+
|
|
26
|
+
expect(result).toBe(30);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("๋ฌธ์์ด ๋ฐํ ๋ฉ์๋ ํธ์ถ", async () => {
|
|
30
|
+
worker = Worker.create<typeof TestWorkerModule>(workerPath);
|
|
31
|
+
|
|
32
|
+
const result = await worker.echo("Hello");
|
|
33
|
+
|
|
34
|
+
expect(result).toBe("Echo: Hello");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("์์ปค์์ ์๋ฌ ๋ฐ์ ์ reject", async () => {
|
|
38
|
+
worker = Worker.create<typeof TestWorkerModule>(workerPath);
|
|
39
|
+
|
|
40
|
+
await expect(worker.throwError()).rejects.toThrow();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("์กด์ฌํ์ง ์๋ ๋ฉ์๋ ํธ์ถ ์ ์๋ฌ", async () => {
|
|
44
|
+
worker = Worker.create<typeof TestWorkerModule>(workerPath);
|
|
45
|
+
|
|
46
|
+
// ํ์
์์คํ
์ ์ฐํํ์ฌ ์กด์ฌํ์ง ์๋ ๋ฉ์๋ ํธ์ถ
|
|
47
|
+
const unknownWorker = worker as unknown as { unknownMethod: () => Promise<void> };
|
|
48
|
+
|
|
49
|
+
await expect(unknownWorker.unknownMethod()).rejects.toThrow("์ ์ ์๋ ๋ฉ์๋: unknownMethod");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("๋ค์ค ์์ฒญ ๋์ ์ฒ๋ฆฌ", async () => {
|
|
53
|
+
worker = Worker.create<typeof TestWorkerModule>(workerPath);
|
|
54
|
+
|
|
55
|
+
const [result1, result2, result3] = await Promise.all([worker.add(1, 2), worker.add(3, 4), worker.add(5, 6)]);
|
|
56
|
+
|
|
57
|
+
expect(result1).toBe(3);
|
|
58
|
+
expect(result2).toBe(7);
|
|
59
|
+
expect(result3).toBe(11);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("void ๋ฐํ ๋ฉ์๋ ํธ์ถ", async () => {
|
|
63
|
+
worker = Worker.create<typeof TestWorkerModule>(workerPath);
|
|
64
|
+
|
|
65
|
+
const result = await worker.noReturn();
|
|
66
|
+
|
|
67
|
+
expect(result).toBeUndefined();
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
//#endregion
|
|
72
|
+
|
|
73
|
+
//#region ์ด๋ฒคํธ
|
|
74
|
+
|
|
75
|
+
describe("์ด๋ฒคํธ", () => {
|
|
76
|
+
it("์์ปค์์ ์ด๋ฒคํธ ์์ ", async () => {
|
|
77
|
+
worker = Worker.create<typeof TestWorkerModule>(workerPath);
|
|
78
|
+
|
|
79
|
+
const events: number[] = [];
|
|
80
|
+
worker.on("progress", (value) => {
|
|
81
|
+
events.push(value);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
await worker.add(1, 2);
|
|
85
|
+
|
|
86
|
+
expect(events).toContain(50);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("off()๋ก ์ด๋ฒคํธ ๋ฆฌ์ค๋ ์ ๊ฑฐ", async () => {
|
|
90
|
+
worker = Worker.create<typeof TestWorkerModule>(workerPath);
|
|
91
|
+
|
|
92
|
+
const events: number[] = [];
|
|
93
|
+
const listener = (value: number) => {
|
|
94
|
+
events.push(value);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
worker.on("progress", listener);
|
|
98
|
+
await worker.add(1, 2);
|
|
99
|
+
|
|
100
|
+
// ๋ฆฌ์ค๋ ์ ๊ฑฐ
|
|
101
|
+
worker.off("progress", listener);
|
|
102
|
+
await worker.add(3, 4);
|
|
103
|
+
|
|
104
|
+
// ์ฒซ ๋ฒ์งธ ํธ์ถ์ ์ด๋ฒคํธ๋ง ์์ ๋์ด์ผ ํจ
|
|
105
|
+
expect(events).toHaveLength(1);
|
|
106
|
+
expect(events[0]).toBe(50);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
//#endregion
|
|
111
|
+
|
|
112
|
+
//#region terminate
|
|
113
|
+
|
|
114
|
+
describe("terminate", () => {
|
|
115
|
+
it("๋๊ธฐ ์ค์ธ ์์ฒญ์ด Worker terminated ์๋ฌ์ ํจ๊ป reject", async () => {
|
|
116
|
+
worker = Worker.create<typeof TestWorkerModule>(workerPath);
|
|
117
|
+
|
|
118
|
+
const runPromise = worker.delay(5000);
|
|
119
|
+
|
|
120
|
+
// ์์ปค ์ข
๋ฃ ์ ์ ๋ฏธ๋ฆฌ ์๋ฌ๋ฅผ ์บ์นํ ์ค๋น๋ฅผ ํ๋ค
|
|
121
|
+
const errorPromise = runPromise.catch((err: unknown) => err);
|
|
122
|
+
|
|
123
|
+
// ์์ปค ์ข
๋ฃ
|
|
124
|
+
await worker.terminate();
|
|
125
|
+
worker = undefined;
|
|
126
|
+
|
|
127
|
+
const error = await errorPromise;
|
|
128
|
+
expect(error).toBeInstanceOf(Error);
|
|
129
|
+
expect((error as Error).message).toBe("์์ปค๊ฐ ์ข
๋ฃ๋จ (method: delay)");
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("์์ปค ๋น์ ์ ์ข
๋ฃ ์ ๋๊ธฐ ์ค์ธ ์์ฒญ reject", async () => {
|
|
133
|
+
worker = Worker.create<typeof TestWorkerModule>(workerPath);
|
|
134
|
+
|
|
135
|
+
// ๊ธด delay๋ฅผ ๋จผ์ ํธ์ถํ์ฌ pending ์ํ๋ก ๋ง๋ฆ
|
|
136
|
+
const delayPromise = worker.delay(5000).catch((err: unknown) => err);
|
|
137
|
+
|
|
138
|
+
// ์งง์ ๋๊ธฐ ํ crash ํธ์ถ (delay๊ฐ pending ์ํ์ธ ๋์)
|
|
139
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
140
|
+
await worker.crash();
|
|
141
|
+
|
|
142
|
+
// ์์ปค๊ฐ ๋น์ ์ ์ข
๋ฃ๋๋ฉด pending ์ํ์ delay ์์ฒญ์ด reject๋์ด์ผ ํจ
|
|
143
|
+
const error = await delayPromise;
|
|
144
|
+
expect(error).toBeInstanceOf(Error);
|
|
145
|
+
expect((error as Error).message).toContain("์์ปค๊ฐ ๋น์ ์ ์ข
๋ฃ๋จ");
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
//#endregion
|
|
150
|
+
|
|
151
|
+
//#region stdout/stderr
|
|
152
|
+
|
|
153
|
+
describe("stdout/stderr", () => {
|
|
154
|
+
it("์์ปค์ console.log ์ถ๋ ฅ์ด ๋ฉ์ธ ํ๋ก์ธ์ค๋ก ์ ๋ฌ", async () => {
|
|
155
|
+
worker = Worker.create<typeof TestWorkerModule>(workerPath);
|
|
156
|
+
|
|
157
|
+
const result = await worker.logMessage("test message");
|
|
158
|
+
|
|
159
|
+
// ๋ฉ์๋๊ฐ ์ ์ ๋ฐํ๋๋ฉด stdout ํ์ดํ์ด ๋์ํ ๊ฒ
|
|
160
|
+
expect(result).toBe("logged");
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
//#endregion
|
|
165
|
+
|
|
166
|
+
//#region env ์ต์
|
|
167
|
+
|
|
168
|
+
describe("env ์ต์
", () => {
|
|
169
|
+
it("env ์ต์
์ด ์์ปค์ ์ ๋ฌ", async () => {
|
|
170
|
+
worker = Worker.create<typeof TestWorkerModule>(workerPath, {
|
|
171
|
+
env: {
|
|
172
|
+
TEST_ENV_VAR: "test-value-123",
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
const result = await worker.getEnv("TEST_ENV_VAR");
|
|
177
|
+
|
|
178
|
+
expect(result).toBe("test-value-123");
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
//#endregion
|
|
183
|
+
});
|