From 80547ec2abf7acdd608fe95c0546043d7f3bd1de Mon Sep 17 00:00:00 2001
From: Muyang
Date: Sat, 14 Nov 2026 06:26:28 +0100
Subject: [PATCH] privacy-gate: per-agent controls and a graphical settings
page (#1111)
privacy-gate's settings page and panel gain per-agent switches and an
"other agents" list. disabledAgents entries the UI can't show as one id
(globs such as `claude-*`) are kept verbatim on save and lock the rows
they match. A save reads the options again and refuses when the agent
fields changed since load, and merges into the current object so
unrelated options are kept.
Run at 30956ba616b3 (the reviewed head), on WSL with a temp HOME:
- packages/privacy-gate: bun test, 76 pass / 0 fail.
- bun scripts/check.mjs privacy-gate: ok.
- Broken by hand: with mergeDisabled dropping patterns, "a glob policy
survives an options save and unrelated options are kept" goes red.
Thanks @siml1169.
---
packages/privacy-gate/README.md | 48 +-
packages/privacy-gate/cases.json | 7 +-
packages/privacy-gate/docs/design.md | 31 +-
packages/privacy-gate/docs/operations.md | 19 +
packages/privacy-gate/package.json | 31 +-
packages/privacy-gate/page.test.mjs | 116 +++++
packages/privacy-gate/page/agents-panel.html | 31 ++
packages/privacy-gate/page/agents.js | 62 +++
packages/privacy-gate/page/app.js | 476 ++++++++++++++++++
packages/privacy-gate/page/icon.svg | 19 +
packages/privacy-gate/page/index.html | 62 +++
packages/privacy-gate/page/panel.js | 261 ++++++++++
packages/privacy-gate/page/policy.js | 97 ++++
packages/privacy-gate/page/style.css | 392 +++++++++++++++
.../privacy-gate/privacy-gate.middleware.js | 27 +
15 files changed, 1671 insertions(+), 8 deletions(-)
create mode 100644 packages/privacy-gate/page.test.mjs
create mode 100644 packages/privacy-gate/page/agents-panel.html
create mode 100644 packages/privacy-gate/page/agents.js
create mode 100644 packages/privacy-gate/page/app.js
create mode 100644 packages/privacy-gate/page/icon.svg
create mode 100644 packages/privacy-gate/page/index.html
create mode 100644 packages/privacy-gate/page/panel.js
create mode 100644 packages/privacy-gate/page/policy.js
create mode 100644 packages/privacy-gate/page/style.css
diff --git a/packages/privacy-gate/README.md b/packages/privacy-gate/README.md
index abc6f88..a0e303f 100644
--- a/packages/privacy-gate/README.md
+++ b/packages/privacy-gate/README.md
@@ -13,7 +13,7 @@
-
+
@@ -52,7 +52,7 @@ Codex, Cursor, Claude Code and scripts.
| **Default policy** | `binaryPolicy: fail` — binary part for a non-local model → **403** |
| **Alternative** | `binaryPolicy: guide` — withholds bytes, injects type/media/size metadata + a neutral local-tool prompt |
| **Text masking** | **Not here.** Use Magpie **Settings → Privacy** |
-| **Tests** | 67 cases through the repository's `scripts/middleware.mjs` |
+| **Tests** | 72 middleware cases + 4 page/policy checks through the repository harness |
## Install
@@ -129,6 +129,24 @@ parts are treated as binary parts; keys with names like `signature`, `id`,
`parameters` or `tools` inside tool arguments and message text are ordinary data
and are still scanned.
+## Per-agent controls
+
+The middleware is **agent-aware**: Magpie supplies `ctx.agent` to the gateway
+hook, so ShyPie can protect some agents and deliberately bypass others.
+
+Open **Settings → Plugins → Installed → ShyPie → Open page** for the full page,
+use the **ShyPie protection** card at the bottom of Magpie's **Agents** page for
+quick toggles, or right-click an agent and choose **ShyPie protection** to open
+the page focused on that agent.
+
+Every agent is protected by default, including new agents. A bypass is explicit:
+only ids in `disabledAgents` (or an empty `ctx.agent` when
+`protectUnknownAgent: false`) skip the gate. `disabledAgents` may also contain
+globs or raw policy entries; the page preserves them verbatim and lists them
+read-only, and it seeds custom disabled ids so their bypass is visible. The page
+warns before a request can leave without protection; saving its switches updates
+the same `ctx.options` the middleware reads, with no restart.
+
## Configuration
All settings are one JSON object, stored as `ctx.options`:
@@ -139,6 +157,8 @@ magpie plugin options privacy-gate '{
"allowModels": ["deepseek/*", "copilot/*"],
"localModels": ["ollama/*", "lmstudio/*"],
"localProviders": ["ollama", "lmstudio"],
+ "disabledAgents": [],
+ "protectUnknownAgent": true,
"guideTools": ["shypie.describe", "shypie.extract", "shypie.ocr"]
}'
```
@@ -146,6 +166,9 @@ magpie plugin options privacy-gate '{
| Option | Default | Meaning |
|---|---:|---|
| `binaryPolicy` | `fail` | `fail` returns 403; `guide` withholds every binary part and injects metadata + a local-tool prompt. |
+| `disabledAgents` | `[]` | Magpie agent ids (or globs) that bypass the gate completely. Empty protects every agent. |
+| `protectUnknownAgent` | `true` | Whether a request Magpie cannot name is still gated. |
+| `extraAgents` | `[]` | Page-only list of custom agent ids; the middleware ignores it. |
| `allowModels` | `[]` | Model globs. `[]` allows every model. If present but not an array of non-empty short strings, **every request is refused** — a typo never silently removes the allowlist. |
| `localModels` | `[]` | Model globs allowed to receive binary parts in `onRequest`. Pair with `localProviders`. |
| `localProviders` | `[]` | Provider-id globs checked in `onProvider`. Both lists must agree. |
@@ -163,6 +186,7 @@ magpie plugin options privacy-gate '{
| Image/file/audio/document parts for non-local models | 403 (`fail`) or withheld (`guide`) |
| Encoded blobs in text (base64/base64url, hex, data URI, PDF/ZIP magic) | 403 |
| Model outside `allowModels` | 403 |
+| Agent in `disabledAgents` | passes unchanged (explicit per-agent bypass) |
| Malformed `allowModels` | every request 403 |
| Body too complex for the inspection budget | 403 |
| Protocol outside the four supported shapes | 403 unless `allowUnknownProtocol: true` |
@@ -337,6 +361,20 @@ flowchart LR
工具参数和消息文本中名为 `signature`、`id`、`parameters` 或 `tools` 的字段
属于普通数据,仍会被扫描。
+## 按 Agent 控制
+
+中间件是 **Agent 感知** 的:Magpie 会把 `ctx.agent` 提供给网关钩子,因此 ShyPie
+可以为部分 Agent 开启防护,并明确绕过其他 Agent。
+
+打开 **设置 → 插件 → 已安装 → ShyPie → 打开页面** 使用完整页面;在 Magpie
+**Agents** 页面底部的 **ShyPie 防护** 卡片中快速切换;或右键某个 Agent 选择
+**ShyPie 防护**,页面会定位到该 Agent。
+
+默认保护所有 Agent,包括新出现的 Agent。绕过必须显式配置:只有
+`disabledAgents` 中的 ID(或在 `protectUnknownAgent: false` 时没有
+`ctx.agent` 的请求)才会跳过防护。页面会在请求可能失去保护时提示;保存开关
+会更新中间件读取的同一份 `ctx.options`,无需重启。
+
## 配置
所有配置都在一个 JSON 对象中,即 `ctx.options`:
@@ -347,6 +385,8 @@ magpie plugin options privacy-gate '{
"allowModels": ["deepseek/*", "copilot/*"],
"localModels": ["ollama/*", "lmstudio/*"],
"localProviders": ["ollama", "lmstudio"],
+ "disabledAgents": [],
+ "protectUnknownAgent": true,
"guideTools": ["shypie.describe", "shypie.extract", "shypie.ocr"]
}'
```
@@ -354,6 +394,9 @@ magpie plugin options privacy-gate '{
| 选项 | 默认值 | 含义 |
|---|---:|---|
| `binaryPolicy` | `fail` | `fail` 返回 403;`guide` 扣留所有二进制内容,并注入元数据和本地工具提示。 |
+| `disabledAgents` | `[]` | 完全绕过防护的 Magpie Agent ID(或 glob)。为空时保护所有 Agent。 |
+| `protectUnknownAgent` | `true` | Magpie 无法识别 Agent 时是否仍执行防护。 |
+| `extraAgents` | `[]` | 仅页面使用的自定义 Agent ID 列表;中间件忽略。 |
| `allowModels` | `[]` | 模型 glob。`[]` 允许所有模型。如果该选项存在但不是由非空短字符串组成的数组,**所有请求都会被拒绝**,拼写错误不会静默取消允许列表。 |
| `localModels` | `[]` | 允许在 `onRequest` 接收二进制内容的模型 glob;需要与 `localProviders` 配合。 |
| `localProviders` | `[]` | 在 `onProvider` 中检查的 provider-id glob;两个列表必须同时匹配。 |
@@ -371,6 +414,7 @@ magpie plugin options privacy-gate '{
| 面向非本地模型的图片/文件/音频/文档 part | 403(`fail`)或扣留(`guide`) |
| 文本中的编码数据块(base64/base64url、hex、data URI、PDF/ZIP 头) | 403 |
| 模型不在 `allowModels` 中 | 403 |
+| Agent 在 `disabledAgents` 中 | 原样通过(显式按 Agent 绕过) |
| `allowModels` 格式错误 | 所有请求 403 |
| 请求体超出检查预算 | 403 |
| 四种支持协议之外的请求 | 403,除非 `allowUnknownProtocol: true` |
diff --git a/packages/privacy-gate/cases.json b/packages/privacy-gate/cases.json
index 9637281..f9050af 100644
--- a/packages/privacy-gate/cases.json
+++ b/packages/privacy-gate/cases.json
@@ -1323,5 +1323,10 @@
{"name": "guide: office media maps to the office class", "ctx": {"protocol": "anthropic", "model": "m/x"}, "options": {"binaryPolicy": "guide"}, "request": {"model": "m/x", "messages": [{"role": "user", "content": [{"type": "file", "source": {"type": "base64", "media_type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "data": "iVBORw0KGgo="}}]}]}, "expect_request": {"model": "m/x", "messages": [{"role": "user", "content": [{"type": "text", "text": "[A binary attachment was withheld locally and was NOT sent to you (type file; media office; bytes 9). Do not claim to have seen it. First ask how the user/agent can process or read it locally.]"}]}]}},
{"name": "guide: parameterized PDF media still maps to pdf", "ctx": {"protocol": "anthropic", "model": "m/x"}, "options": {"binaryPolicy": "guide"}, "request": {"model": "m/x", "messages": [{"role": "user", "content": [{"type": "file", "source": {"type": "base64", "media_type": "application/pdf; version=1.7", "data": "iVBORw0KGgo="}}]}]}, "expect_request": {"model": "m/x", "messages": [{"role": "user", "content": [{"type": "text", "text": "[A binary attachment was withheld locally and was NOT sent to you (type file; media pdf; bytes 9). Do not claim to have seen it. First ask how the user/agent can process or read it locally.]"}]}]}},
{"name": "guide: oversized media_type is not normalized", "ctx": {"protocol": "anthropic", "model": "m/x"}, "options": {"binaryPolicy": "guide"}, "request": {"model": "m/x", "messages": [{"role": "user", "content": [{"type": "file", "source": {"type": "base64", "media_type": "application/pdf; version=1.7AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "data": "iVBORw0KGgo="}}]}]}, "expect_request": {"model": "m/x", "messages": [{"role": "user", "content": [{"type": "text", "text": "[A binary attachment was withheld locally and was NOT sent to you (type file; media unknown; bytes 9). Do not claim to have seen it. First ask how the user/agent can process or read it locally.]"}]}]}},
- {"name": "guide: input_audio kind falls back to audio", "ctx": {"protocol": "chat", "model": "m/x"}, "options": {"binaryPolicy": "guide"}, "request": {"model": "m/x", "messages": [{"role": "user", "content": [{"type": "input_audio", "input_audio": {"data": "AAAA", "format": "wav"}}]}]}, "expect_request": {"model": "m/x", "messages": [{"role": "user", "content": [{"type": "text", "text": "[A binary attachment was withheld locally and was NOT sent to you (type input_audio; media audio). Do not claim to have seen it. First ask how the user/agent can process or read it locally.]"}]}]}}
+ {"name": "guide: input_audio kind falls back to audio", "ctx": {"protocol": "chat", "model": "m/x"}, "options": {"binaryPolicy": "guide"}, "request": {"model": "m/x", "messages": [{"role": "user", "content": [{"type": "input_audio", "input_audio": {"data": "AAAA", "format": "wav"}}]}]}, "expect_request": {"model": "m/x", "messages": [{"role": "user", "content": [{"type": "text", "text": "[A binary attachment was withheld locally and was NOT sent to you (type input_audio; media audio). Do not claim to have seen it. First ask how the user/agent can process or read it locally.]"}]}]}},
+ {"name":"agent controls: disabled agent bypasses the gate","ctx":{"protocol":"chat","model":"m/x","agent":"claude"},"options":{"disabledAgents":["claude"]},"request":{"model":"m/x","messages":[{"role":"user","content":[{"type":"image_url","image_url":{"url":"data:image/png;base64,AAAA"}}]}]},"provider":{"id":"remote","name":"Remote"}},
+ {"name":"agent controls: another agent is still gated","ctx":{"protocol":"chat","model":"m/x","agent":"codex"},"options":{"disabledAgents":["claude"]},"request":{"model":"m/x","messages":[{"role":"user","content":[{"type":"image_url","image_url":{"url":"data:image/png;base64,AAAA"}}]}]},"reject":{"status":403,"message":"privacy-gate"}},
+ {"name":"agent controls: unknown agent is protected by default","ctx":{"protocol":"chat","model":"m/x"},"options":false,"request":{"model":"m/x","messages":[{"role":"user","content":[{"type":"image_url","image_url":{"url":"data:image/png;base64,AAAA"}}]}]},"reject":{"status":403,"message":"privacy-gate"}},
+ {"name":"agent controls: unknown agent can be bypassed explicitly","ctx":{"protocol":"chat","model":"m/x"},"options":{"protectUnknownAgent":false},"request":{"model":"m/x","messages":[{"role":"user","content":[{"type":"image_url","image_url":{"url":"data:image/png;base64,AAAA"}}]}]},"provider":{"id":"remote","name":"Remote"}},
+ {"name":"agent controls: malformed disabledAgents stays gated","ctx":{"protocol":"chat","model":"m/x","agent":"claude"},"options":{"disabledAgents":"claude"},"request":{"model":"m/x","messages":[{"role":"user","content":[{"type":"image_url","image_url":{"url":"data:image/png;base64,AAAA"}}]}]},"reject":{"status":403,"message":"privacy-gate"}}
]
diff --git a/packages/privacy-gate/docs/design.md b/packages/privacy-gate/docs/design.md
index dc9f8e9..5aa8a5e 100644
--- a/packages/privacy-gate/docs/design.md
+++ b/packages/privacy-gate/docs/design.md
@@ -166,12 +166,37 @@ only needs enough information to choose a local extractor. MIME parameters are
stripped before classification, and oversized candidates are ignored before
lowercasing so untrusted metadata cannot add unbounded normalization work.
+## Agent controls
+
+Magpie supplies `ctx.agent` to the gateway middleware. ShyPie uses it only for
+policy selection:
+
+- `disabledAgents` (exact ids or globs) bypasses both `onRequest` and
+ `onProvider` for that agent; the request is returned unchanged.
+- `protectUnknownAgent` defaults to `true`. An empty `ctx.agent` stays gated
+ unless the operator explicitly opts out.
+- New agents and newly added plugin agents are protected by default. The policy
+ is a deny-list, not an allow-list, so an unrecognised agent cannot silently
+ escape the gate.
+- The page, the Agents-page panel and the agent-row action all edit the same
+ `disabledAgents` and `protectUnknownAgent` options. The page also keeps a
+ page-only `extraAgents` catalogue for custom ids; the middleware ignores it.
+- Disabled ids and raw policy entries are separate. Exact ids get switches;
+ globs and values the UI cannot represent are kept verbatim, shown read-only,
+ and merged back on save. The page seeds custom exact ids from
+ `disabledAgents` so a custom bypass is not invisible.
+- Saving starts from freshly read options and changes only the three page-owned
+ fields. A concurrent change to those fields is reported instead of overwritten;
+ controls are disabled while a write is pending.
+- `ctx.agent` is host-classified metadata, never a User-Agent header. Session
+ ids and working directories are not used for UI policy.
+
## Verification
-- 67 cases in `cases.json`, run through `scripts/middleware.mjs` using the same
+- 72 cases in `cases.json`, run through `scripts/middleware.mjs` using the same
hook calls Magpie performs.
- The package has no network, timers, Node APIs or imports; the middleware file
is one plain JavaScript module.
- The tests cover fail/guide mode, malformed options, all four protocols,
- vendor-sealed replay fields, site scoping, blob scanning, and guide-mode
- bounds.
+ vendor-sealed replay fields, site scoping, blob scanning, agent controls, and
+ guide-mode bounds.
diff --git a/packages/privacy-gate/docs/operations.md b/packages/privacy-gate/docs/operations.md
index 98e5472..8387154 100644
--- a/packages/privacy-gate/docs/operations.md
+++ b/packages/privacy-gate/docs/operations.md
@@ -64,6 +64,25 @@ in the guide prompt.
5. Check Magpie's plugin log for counts and the `usage` line. The middleware
never logs request content — only provider/model/status/blocked counters.
+## Per-agent controls
+
+Open **Settings → Plugins → Installed → ShyPie → Open page** for the full
+per-agent page. The same switches are available in the **ShyPie protection**
+card at the bottom of Magpie's **Agents** page, and right-clicking an agent adds
+**ShyPie protection** to its row menu.
+
+Every agent is protected by default. A switch that is off writes that agent's id
+to `disabledAgents`; it bypasses both the request gate and the provider check.
+Globs and raw values are preserved verbatim and shown read-only under **Policy
+patterns**. A custom id already present only in `disabledAgents` is seeded into
+the page so its bypass stays visible. The **Unknown / unclassified agent** switch
+controls `protectUnknownAgent` and defaults to on. Saving applies to the next
+request; no Magpie restart is needed. If options changed elsewhere, the page asks
+you to reload rather than overwriting the newer values.
+
+A bypassed agent is intentionally unprotected, including binary parts and
+encoded blobs. Use it only for an agent whose traffic you have chosen to trust.
+
## Troubleshooting
### I get a 403 for an image
diff --git a/packages/privacy-gate/package.json b/packages/privacy-gate/package.json
index 018cbda..a45c1fd 100644
--- a/packages/privacy-gate/package.json
+++ b/packages/privacy-gate/package.json
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/middleware-privacy-gate",
- "version": "0.3.1",
+ "version": "0.4.0",
"description": "Structural egress gate for Magpie: withholds or rejects image/file/audio parts, encoded blobs and disallowed models, and guides agents toward local extraction when Magpie cannot mask text. Companion to ShyPie for DeepSeek Harness.",
"type": "module",
"files": [
@@ -8,7 +8,8 @@
"README.md",
"BRAND.md",
"assets",
- "docs"
+ "docs",
+ "page"
],
"keywords": [
"magpie",
@@ -29,11 +30,37 @@
"magpie": {
"middleware": "./privacy-gate.middleware.js",
"icon": "https://raw.githubusercontent.com/magpie-community/plugins/main/packages/privacy-gate/assets/shypie-icon.svg",
+ "page": {
+ "title": "ShyPie Filter",
+ "titles": { "zh": "ShyPie 防护" },
+ "file": "page/index.html",
+ "icon": "page/icon.svg"
+ },
+ "panels": [
+ {
+ "slot": "agents",
+ "file": "page/agents-panel.html",
+ "title": "ShyPie protection",
+ "titles": { "zh": "ShyPie 防护" }
+ }
+ ],
+ "actions": [
+ {
+ "id": "shypie-protection",
+ "menu": "agent",
+ "title": "ShyPie protection",
+ "titles": { "zh": "ShyPie 防护" }
+ }
+ ],
+ "permissions": [],
"options": {
"allowModels": [],
"localModels": [],
"localProviders": [],
"binaryPolicy": "fail",
+ "disabledAgents": [],
+ "protectUnknownAgent": true,
+ "extraAgents": [],
"guideTools": [],
"allowUnknownProtocol": false,
"blobBase64Chars": 400,
diff --git a/packages/privacy-gate/page.test.mjs b/packages/privacy-gate/page.test.mjs
new file mode 100644
index 0000000..467fb19
--- /dev/null
+++ b/packages/privacy-gate/page.test.mjs
@@ -0,0 +1,116 @@
+import { expect, test } from 'bun:test'
+import { readFileSync } from 'node:fs'
+import { runInNewContext } from 'node:vm'
+
+const dir = new URL('./page/', import.meta.url)
+const read = (file) => readFileSync(new URL(file, dir), 'utf8')
+
+function bridgeCalls(source) {
+ return [...source.matchAll(/\bmagpie\s*(?:\?\.|\.)\s*([\w$]+(?:\s*(?:\?\.|\.)\s*[\w$]+)*)/g)]
+ .map(([, call]) => call.replace(/\s/g, '').replaceAll('?.', '.'))
+}
+
+function safePageScript(source, allowed) {
+ if (/\b(fetch|XMLHttpRequest|WebSocket|EventSource)\b/.test(source)) return false
+ if (/\bmagpie\s*\[|\b(?:window|globalThis)\s*\[\s*["']magpie["']\s*\]/.test(source)) return false
+ return bridgeCalls(source).every((call) => allowed.includes(call))
+}
+
+test('page and panel use only the released bridge and make no network calls', () => {
+ const app = read('app.js')
+ const panel = read('panel.js')
+ const agents = read('agents.js')
+ const policy = read('policy.js')
+ const page = read('index.html')
+ const panelPage = read('agents-panel.html')
+ const allowed = ['ready', 'options.get', 'options.set', 'onAction']
+
+ expect(safePageScript(app, allowed)).toBe(true)
+ expect(safePageScript(panel, allowed)).toBe(true)
+ expect(safePageScript(agents, [])).toBe(true)
+ expect(safePageScript(policy, [])).toBe(true)
+ for (const source of [app, panel]) {
+ expect(safePageScript(`${source}\nfetch('https://evil.example/x')`, allowed)).toBe(false)
+ expect(safePageScript(`${source}\nmagpie.read?.('secrets')`, allowed)).toBe(false)
+ expect(safePageScript(`${source}\nmagpie['read']('secrets')`, allowed)).toBe(false)
+ }
+
+ for (const html of [page, panelPage]) {
+ expect(html).not.toMatch(/
+
+
+
+