@orielweb/api 0.5.0 → 0.6.0

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/src/mocks.js CHANGED
@@ -5,16 +5,13 @@
5
5
  //
6
6
  // 与 Tauri 的差别:
7
7
  // * Tauri 的 `mockIPC` 回调签名是 `(cmd, payload) => ...`;这里一致。
8
- // * 本库**没有 window 概念**(尚未有 window 模块),所以 `mockWindows` 只做占位——
9
- // 它记录 label 列表,等 window 模块落地后由它读。现在调用不会报错,但也不影响任何行为。
8
+ // * `mockWindows` 在本库有真实作用:它决定 mock 传输层上报的 `label`
9
+ // (第一个参数),进而决定 `getCurrentWindow()` 返回哪个窗口句柄。
10
10
 
11
- import { setTransportOverride, hasTransportOverride } from './internal.js';
11
+ import { setTransportOverride, hasTransportOverride, resetCloseInterceptRefs } from './internal.js';
12
12
 
13
13
  /**
14
- * @typedef {{
15
- * invoke: (name: string, args?: Record<string, unknown>) => Promise<unknown>,
16
- * supportsPerCallTimeout?: boolean
17
- * }} OrielTransport
14
+ * @typedef {import('./types.js').OrielTransport} OrielTransport
18
15
  *
19
16
  * @typedef {(cmd: string, payload: Record<string, unknown> | null) => unknown} MockHandler
20
17
  */
@@ -25,9 +22,19 @@ import { setTransportOverride, hasTransportOverride } from './internal.js';
25
22
  */
26
23
  let handler = null;
27
24
 
28
- /** `mockWindows` 登记的 label 列表(供将来 window 模块读取)。 */
25
+ /**
26
+ * `mockWindows` 登记的 label 列表;第一个元素是 mock 传输层上报的"本窗口"。
27
+ * @type {string[]}
28
+ */
29
29
  let mockWindowLabels = ['main'];
30
30
 
31
+ /**
32
+ * `mockConvertFileSrc` 登记的协议名;`null` 表示没登记——mock 传输层就
33
+ * 不提供 `convertFileSrc`(与旧宿主同语义,core.convertFileSrc 会如实报"不支持")。
34
+ * @type {string | null}
35
+ */
36
+ let mockFileProtocol = null;
37
+
31
38
  /**
32
39
  * mock 传输层的事件订阅表:事件名 → 处理器集合。
33
40
  *
@@ -52,7 +59,9 @@ export function mockIPC(invoke) {
52
59
  }
53
60
 
54
61
  handler = invoke;
55
- setTransportOverride({
62
+
63
+ /** @type {OrielTransport} */
64
+ const transport = {
56
65
  // 声明支持 per-call timeout。
57
66
  //
58
67
  // 不声明的话,页面在测试里写 `invoke(cmd, args, { timeout })` 会被 core 的显式检查
@@ -84,7 +93,26 @@ export function mockIPC(invoke) {
84
93
  }
85
94
  };
86
95
  },
87
- });
96
+ };
97
+
98
+ // label / convertFileSrc 用 getter 挂:mockWindows / mockConvertFileSrc 在
99
+ // mockIPC **之后**调用也要能生效(测试里常见顺序是先 mockIPC 再补窗口设置),
100
+ // 快照值会把后面的调用变成静默不生效——正是本模块要消灭的那类陷阱。
101
+ const dynamic = /** @type {OrielTransport} */ (/** @type {unknown} */ (
102
+ Object.defineProperties(transport, {
103
+ label: {
104
+ get: () => mockWindowLabels[0],
105
+ },
106
+ convertFileSrc: {
107
+ get: () => (mockFileProtocol === null
108
+ ? undefined
109
+ : (/** @param {string} filePath */ (filePath) =>
110
+ `${mockFileProtocol}://file.oriel/${encodeURIComponent(filePath)}`)),
111
+ },
112
+ })
113
+ ));
114
+
115
+ setTransportOverride(dynamic);
88
116
  }
89
117
 
90
118
  /**
@@ -118,12 +146,15 @@ export function mockEmit(eventName, payload) {
118
146
  }
119
147
 
120
148
  /**
121
- * 登记"有哪些窗口"(供将来 window 模块的 `getAllWindows` 读取)。
149
+ * 登记"有哪些窗口"(对标 Tauri 的 `mockWindows`)。
150
+ *
151
+ * 第一个 label 是 mock 传输层上报的**本窗口**——`getCurrentWindow()` 会返回
152
+ * 它的句柄,后续改成别的 label 也会实时生效(getter,见 `mockIPC`)。
153
+ * 其余 label 供 `getAllWindows` 一类查询读取(`getMockWindowLabels`,内部用)。
122
154
  *
123
- * 现在调用不改变任何可观测行为——本包还没有 window 模块。提前提供是为了让
124
- * 迁移过来的测试代码不必等到那时才写。
155
+ * 在 `mockIPC` 之前或之后调用都可以。
125
156
  *
126
- * @param {string[]} labels
157
+ * @param {...string} labels
127
158
  * @returns {void}
128
159
  */
129
160
  export function mockWindows(...labels) {
@@ -141,7 +172,7 @@ export function mockWindows(...labels) {
141
172
  }
142
173
 
143
174
  /**
144
- * 取当前登记的窗口 label(内部用;window 模块落地后由它消费)。
175
+ * 取当前登记的窗口 label(内部用;`window.getAllWindows` 一类查询的 mock 数据源)。
145
176
  * @returns {string[]}
146
177
  */
147
178
  export function getMockWindowLabels() {
@@ -149,10 +180,14 @@ export function getMockWindowLabels() {
149
180
  }
150
181
 
151
182
  /**
152
- * 占位:对标 Tauri 的 `mockConvertFileSrc`。
183
+ * 让 mock 传输层支持 `convertFileSrc`(对标 Tauri 的 `mockConvertFileSrc`)。
153
184
  *
154
- * 本库还没有文件 asset 协议(见 core.js 的 `convertFileSrc`),所以这里只记录
155
- * 自定义协议名,供 `convertFileSrc` 实现时读取。
185
+ * 登记后 `core.convertFileSrc` 在 mock 下返回
186
+ * `<protocol>://file.oriel/<encodeURIComponent(path)>`;未登记时 mock 传输层
187
+ * 不提供该能力(`convertFileSrc` 如实抛"当前宿主不支持",与旧宿主同语义)。
188
+ *
189
+ * 在 `mockIPC` 之前或之后调用都可以。注意 protocol 只影响 URL 前缀——
190
+ * 本库的文件内容固定走 `file.oriel` 这个 host,与真实宿主一致。
156
191
  *
157
192
  * @param {string} protocol
158
193
  * @returns {void}
@@ -161,6 +196,8 @@ export function mockConvertFileSrc(protocol = 'oriel') {
161
196
  if (typeof protocol !== 'string' || protocol === '') {
162
197
  throw new Error('mockConvertFileSrc: protocol 必须是非空字符串');
163
198
  }
199
+
200
+ mockFileProtocol = protocol;
164
201
  }
165
202
 
166
203
  /**
@@ -170,9 +207,13 @@ export function mockConvertFileSrc(protocol = 'oriel') {
170
207
  export function clearMocks() {
171
208
  handler = null;
172
209
  mockWindowLabels = ['main'];
210
+ mockFileProtocol = null;
173
211
  // 事件订阅表也要清:不清的话上一条用例订阅的处理器会在下一条用例的 mockEmit 里
174
212
  // 继续被调用,而那两个用例本该互不干扰——这种污染最难查(症状是"单跑通过、连跑失败")。
175
213
  mockListeners = new Map();
214
+ // 关闭接管的订阅计数也要清:它是按窗口 label 记的,留着会让下一条用例的
215
+ // 退订逻辑以为"还有别人在订阅",于是不去关宿主接管。
216
+ resetCloseInterceptRefs();
176
217
  setTransportOverride(null);
177
218
  }
178
219