Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7136689efe | |||
| e366605299 |
8
SKILL.md
8
SKILL.md
@@ -1,7 +1,7 @@
|
||||
---
|
||||
name: 技能开发模板(通用业务版)
|
||||
description: "OpenClaw 通用业务技能开发模板,供复制后定制新业务 skill。定制步骤见 development/DEVELOPMENT.md。"
|
||||
version: 1.0.14
|
||||
version: 1.0.32
|
||||
author: 深圳匠厂科技有限公司
|
||||
metadata:
|
||||
openclaw:
|
||||
@@ -38,6 +38,7 @@ allowed-tools:
|
||||
- **只有当**用户明确询问命令、参数、输出结构、开发、调试、集成或排错时,才读取 `references/` 或 `development/`。
|
||||
- 若 `README.md` 与 `SKILL.md` / `references` 表述冲突:对用户展示与市场说明以 `README.md` 为准;对执行契约、CLI、schema、运行约束以 `SKILL.md` / `references` 为准。
|
||||
- 回答用户时**不要**暴露 Playwright、DOM、Python、RPA 等实现细节,除非用户明确询问技术实现。
|
||||
- 用户未问技术实现时,**不要**讨论 slug 命名规则;开发者问命名或 slug 时,读取 `development/NAMING.md`。
|
||||
|
||||
## 文档分工
|
||||
|
||||
@@ -82,10 +83,13 @@ python {baseDir}/scripts/main.py version
|
||||
2. 按 `development/DEVELOPMENT.md` 完成开发与文档定制。
|
||||
3. 用户市场说明写入根 `README.md`;Agent 调用契约见 `references/CLI.md`、`references/SCHEMA.md`。
|
||||
4. 同步修改 `scripts/util/constants.py` 中的 `SKILL_SLUG` / `SKILL_VERSION`。
|
||||
5. 本地 SQLite 的中文表名/字段名与字段顺序规范见 `references/SCHEMA.md`(元数据表 `_jiangchang_*` + `CREATE TABLE` 物理顺序)。
|
||||
5. 本地 SQLite 的中文表名/字段名、标准时间字段与字段顺序规范见 `references/SCHEMA.md`(元数据表 `_jiangchang_*` + `CREATE TABLE` 物理顺序)。
|
||||
|
||||
## 技能命名与 slug
|
||||
|
||||
- 标准格式:`{verb}-{noun-phrase}-{platform}`(详见 `development/NAMING.md`)。
|
||||
- slug / repo / 目录 / `SKILL_SLUG` / DB 文件名:**英文** kebab-case;`name`、`description`、根 `README.md`:**中文**。
|
||||
- 复制为新业务技能前**必须**阅读 `development/NAMING.md`。
|
||||
- `name`(frontmatter):**用户可见的中文业务名称**;`description` 是说明,不是名称。
|
||||
- `metadata.openclaw.slug`:**英文 kebab-case** 机器标识;目录名、默认 `{slug}.db` 使用 slug,不使用中文。
|
||||
- 复制后全局替换 `your-skill-slug`;正式技能不得保留英文占位 `name` 或 slug。
|
||||
|
||||
13
assets/naming/platforms.txt
Normal file
13
assets/naming/platforms.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
amazon
|
||||
shopee
|
||||
xinghang
|
||||
alibaba
|
||||
kingdee
|
||||
icbc
|
||||
pingpong
|
||||
worldfirst
|
||||
paypal
|
||||
lianlian
|
||||
single-window
|
||||
export-rebate
|
||||
etax
|
||||
4
assets/naming/scopes.txt
Normal file
4
assets/naming/scopes.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
all
|
||||
batch
|
||||
multi
|
||||
default
|
||||
25
assets/naming/verbs.txt
Normal file
25
assets/naming/verbs.txt
Normal file
@@ -0,0 +1,25 @@
|
||||
scrape
|
||||
receive
|
||||
download
|
||||
export
|
||||
add
|
||||
fill
|
||||
enter
|
||||
bind
|
||||
withdraw
|
||||
reconcile
|
||||
verify
|
||||
submit
|
||||
query
|
||||
process
|
||||
reply
|
||||
generate
|
||||
draft
|
||||
review
|
||||
confirm
|
||||
track
|
||||
upload
|
||||
match
|
||||
triage
|
||||
ship
|
||||
reprice
|
||||
@@ -79,8 +79,9 @@
|
||||
|
||||
你不应该直接在这个仓库里开发业务,而应该:
|
||||
|
||||
0. 按 [`NAMING.md`](NAMING.md) 确定 slug(`{verb}-{noun-phrase}-{platform}`)
|
||||
1. 复制这个目录
|
||||
2. 改成新 skill 的目录名
|
||||
2. 改成新 skill 的目录名(与 slug 一致)
|
||||
3. 把占位内容替换掉
|
||||
4. 再开始写业务逻辑
|
||||
|
||||
@@ -447,6 +448,8 @@ metadata:
|
||||
只做增删查改(模板默认表:`task_logs`)
|
||||
- `db/display_metadata.py`
|
||||
创建 `_jiangchang_tables` / `_jiangchang_columns` 并幂等写入中文展示名(见 `references/SCHEMA.md`)
|
||||
- `db/timestamp_columns.py`
|
||||
标准时间字段 trigger 与维护约定(`created_at` / `updated_at`,Unix 秒级)
|
||||
|
||||
不要在 `db` 层里:
|
||||
|
||||
@@ -680,6 +683,8 @@ uses: client-jiangchang/jiangchang-platform-kit/.github/workflows/reusable-relea
|
||||
每个新 skill 发布前,建议技术人员逐条确认:
|
||||
|
||||
- [ ] 目录结构符合当前模板
|
||||
- [ ] slug 符合 [`NAMING.md`](NAMING.md)(verb-noun-platform)
|
||||
- [ ] 目录名、`SKILL.md` slug、`constants.SKILL_SLUG` 三者一致
|
||||
- [ ] `SKILL.md` 中 slug、名称、描述都已替换
|
||||
- [ ] `scripts/util/constants.py` 已修改
|
||||
- [ ] `../references/CLI.md` 示例命令已改成真实命令
|
||||
@@ -749,11 +754,46 @@ uses: client-jiangchang/jiangchang-platform-kit/.github/workflows/reusable-relea
|
||||
- 用户说明在根 `README.md`,Agent 资料在 `references/`,开发规范在 `development/`
|
||||
- 统一走 `scripts/main.py` 作为 CLI 入口
|
||||
|
||||
### 错误 5:平台放 slug 最前
|
||||
|
||||
表现:
|
||||
|
||||
- slug 形如 `amazon-download-settlement-report`
|
||||
- 语义校验或 `test_slug_naming` 失败
|
||||
|
||||
要改:
|
||||
|
||||
- 动词放首段,平台放末段,例如 `download-settlement-report-amazon`
|
||||
- 见 [`NAMING.md`](NAMING.md)
|
||||
|
||||
### 错误 6:noun 段过长导致超 48 字符
|
||||
|
||||
表现:
|
||||
|
||||
- slug 超过 48 字符
|
||||
- 中间名词段堆砌过多
|
||||
|
||||
要改:
|
||||
|
||||
- 精简 noun-phrase,保留核心业务含义
|
||||
- 见 [`NAMING.md`](NAMING.md) §3 硬约束
|
||||
|
||||
### 错误 7:只改目录名未改 slug
|
||||
|
||||
表现:
|
||||
|
||||
- 与「错误 1」相同,但目录已改名而 `SKILL.md` / `constants.py` 仍为占位
|
||||
|
||||
要检查:
|
||||
|
||||
- `SKILL.md` 的 `metadata.openclaw.slug`
|
||||
- `scripts/util/constants.py` 的 `SKILL_SLUG`
|
||||
|
||||
## 18. 推荐开发顺序总结
|
||||
|
||||
如果让一个新人照着做,我建议他按这个顺序:
|
||||
|
||||
1. 复制模板并改目录名
|
||||
1. 按 [`NAMING.md`](NAMING.md) 确定 slug,复制模板并改目录名
|
||||
2. 改 `SKILL.md` 与根 `README.md`
|
||||
3. 改 `scripts/util/constants.py`
|
||||
4. 改 `references/` 与 `development/`
|
||||
|
||||
236
development/NAMING.md
Normal file
236
development/NAMING.md
Normal file
@@ -0,0 +1,236 @@
|
||||
# 技能 slug 命名规范(verb-noun-platform)
|
||||
|
||||
> 本文是匠厂**新业务 skill** 的 slug / Git 仓库名 / 目录名命名权威说明。复制 `skill-template` 出去开发新技能前**必读**;Agent 在开发者询问命名或 slug 时也应先读本文。
|
||||
|
||||
---
|
||||
|
||||
## 1. 适用范围
|
||||
|
||||
| 范围 | 说明 |
|
||||
|------|------|
|
||||
| ✅ **适用** | 从 `skill-template` **复制出去的新技能** |
|
||||
| ❌ **不追溯** | 已上线历史技能(如 `account-manager`、`reconcile-finance`、`scrape-contacts-lianjia` 等)保持不动 |
|
||||
| ❌ **不修改** | 其他仓库(尤其 `account-manager`)不在本文约束范围内 |
|
||||
| ✅ **模板占位** | 模板本体的 `your-skill-slug` 占位必须继续通过全部测试 |
|
||||
|
||||
**新技能勿仿**历史 2 段无平台形态(如 `reconcile-finance`)或不符合 verb-noun-platform 的旧 repo 名;校验器对历史 repo **不追溯**,但规范中明确标注为反模式。
|
||||
|
||||
---
|
||||
|
||||
## 2. 命名格式
|
||||
|
||||
标准形态(约 95% 新技能):
|
||||
|
||||
```text
|
||||
{verb}-{noun-phrase}-{platform}
|
||||
```
|
||||
|
||||
| 部分 | 规则 |
|
||||
|------|------|
|
||||
| **verb** | 1 个词,来自 [动词白名单](#5-动词白名单) |
|
||||
| **noun-phrase** | 1~3 个词(按 `-` 分段),描述业务对象 |
|
||||
| **platform** | 1 个词,来自 [平台白名单](#6-平台白名单);可含连字符(如 `single-window`) |
|
||||
|
||||
**展示层分工不变:**
|
||||
|
||||
| 字段 | 语言 | 示例 |
|
||||
|------|------|------|
|
||||
| slug / repo / 目录 / `SKILL_SLUG` / DB 文件名 | **英文** kebab-case | `download-settlement-report-amazon` |
|
||||
| `SKILL.md` 的 `name`、`description`、根 `README.md` | **中文** | `亚马逊结算报表下载` |
|
||||
|
||||
---
|
||||
|
||||
## 3. 硬约束(新技能强制校验)
|
||||
|
||||
| 约束 | 值 |
|
||||
|------|-----|
|
||||
| 格式 | `{verb}-{noun-phrase}-{platform}`(标准型,默认) |
|
||||
| 总段数(按 `-` 分割) | **3~5 段**(标准型);scope 型见 [§4](#4-三类合法形态) |
|
||||
| 总长度 | **≤ 48 字符** |
|
||||
| 字符集 | 小写字母、数字、连字符;kebab-case |
|
||||
| 禁止 | 中文 slug、下划线、**平台放最前**(如 `amazon-download-xxx`) |
|
||||
|
||||
实现校验见 `scripts/util/slug_naming.py`;`scripts/db/display_metadata_validator.py` 在 `SKILL.md` frontmatter 自检时调用。
|
||||
|
||||
---
|
||||
|
||||
## 4. 三类合法形态
|
||||
|
||||
### 4.1 标准型(95%)
|
||||
|
||||
`verb-noun-platform` 或 `verb-noun-noun-platform`(3~5 段)
|
||||
|
||||
- 第 1 段 ∈ 动词白名单
|
||||
- 最后 1 段(或 hyphenated 平台的最后 2 段)∈ 平台白名单
|
||||
- 中间为 noun-phrase(1~3 段)
|
||||
|
||||
示例:
|
||||
|
||||
- `download-settlement-report-amazon`
|
||||
- `add-product-shopee`
|
||||
- `enter-sales-receipt-kingdee`
|
||||
- `fill-export-declaration-single-window`
|
||||
|
||||
### 4.2 scope 型(少见)
|
||||
|
||||
`verb-noun-scope`
|
||||
|
||||
- 最后 1 段 ∈ scope 白名单:`all` | `batch` | `multi` | `default`
|
||||
- 第 1 段 ∈ 动词白名单
|
||||
- 总段数 3~5
|
||||
|
||||
示例:`generate-article-all`
|
||||
|
||||
> 说明:`geo-generate-article-all` 若作为新技能且不在豁免表、首段 `geo` 不在动词表,应写为 `generate-article-all`。
|
||||
|
||||
### 4.3 历史豁免(不校验语义)
|
||||
|
||||
以下 slug 跳过 verb/platform 语义校验(见 `LEGACY_EXEMPT_SLUGS`):
|
||||
|
||||
- `account-manager`
|
||||
- `skill-template`
|
||||
- `your-skill-slug` / `your_skill_slug`(模板占位)
|
||||
|
||||
---
|
||||
|
||||
## 5. 动词白名单
|
||||
|
||||
词表文件:[`assets/naming/verbs.txt`](../assets/naming/verbs.txt)(每行一词,便于后续扩展)
|
||||
|
||||
| 动词 | 动词 | 动词 | 动词 |
|
||||
|------|------|------|------|
|
||||
| scrape | receive | download | export |
|
||||
| add | fill | enter | bind |
|
||||
| withdraw | reconcile | verify | submit |
|
||||
| query | process | reply | generate |
|
||||
| draft | review | confirm | track |
|
||||
| upload | match | triage | ship |
|
||||
| reprice | | | |
|
||||
|
||||
---
|
||||
|
||||
## 6. 平台白名单
|
||||
|
||||
词表文件:[`assets/naming/platforms.txt`](../assets/naming/platforms.txt)
|
||||
|
||||
| 平台 | 平台 | 平台 |
|
||||
|------|------|------|
|
||||
| amazon | shopee | xinghang |
|
||||
| alibaba | kingdee | icbc |
|
||||
| pingpong | worldfirst | paypal |
|
||||
| lianlian | single-window | export-rebate |
|
||||
| etax | | |
|
||||
|
||||
scope 词表:[`assets/naming/scopes.txt`](../assets/naming/scopes.txt) — `all`、`batch`、`multi`、`default`
|
||||
|
||||
---
|
||||
|
||||
## 7. slug 与展示字段分工
|
||||
|
||||
```yaml
|
||||
---
|
||||
name: 亚马逊结算报表下载 # 中文,用户可见
|
||||
description: "从卖家中心下载结算报表并入库" # 中文说明
|
||||
metadata:
|
||||
openclaw:
|
||||
slug: download-settlement-report-amazon # 英文机器标识
|
||||
---
|
||||
```
|
||||
|
||||
同步修改:
|
||||
|
||||
- 目录名
|
||||
- `metadata.openclaw.slug`
|
||||
- `scripts/util/constants.py` 中的 `SKILL_SLUG`
|
||||
- 默认数据库路径 `{slug}/{slug}.db`
|
||||
|
||||
三者必须一致。
|
||||
|
||||
---
|
||||
|
||||
## 8. 与 account-manager 的关系
|
||||
|
||||
slug **末段 platform** 与 `account-manager` 中的 `platform_key` **语义对应**,但**不要求字符串完全相等**。
|
||||
|
||||
| slug 末段 (platform) | account-manager platform_key(示例) |
|
||||
|----------------------|--------------------------------------|
|
||||
| amazon | `amazon_sim` / `amazon` |
|
||||
| shopee | `shopee` |
|
||||
| kingdee | `kingdee` |
|
||||
|
||||
新技能若需账号下发,在 `REQUIREMENTS.md` §0 填写拟用的 `platform_key`,并与兄弟技能调用参数对齐。
|
||||
|
||||
---
|
||||
|
||||
## 9. 复制新技能流程
|
||||
|
||||
1. **先定 slug** — 按本文规范拟定 `{verb}-{noun-phrase}-{platform}`,确认在白名单内且 ≤ 48 字符
|
||||
2. **复制目录** — 将 `skill-template` 复制为新目录,**目录名 = slug**
|
||||
3. **全局替换** — `your-skill-slug` → 新 slug;更新 `SKILL.md`、`constants.py`、文档中的占位
|
||||
4. **填写 REQUIREMENTS** — 完成 [`REQUIREMENTS.md`](REQUIREMENTS.md) §0 技能标识
|
||||
5. **开发 & 自检** — `python tests/run_tests.py -v`,含 slug 语义测试
|
||||
|
||||
---
|
||||
|
||||
## 10. 正例 / 反例
|
||||
|
||||
### ✅ 正例
|
||||
|
||||
| slug | 说明 |
|
||||
|------|------|
|
||||
| `download-settlement-report-amazon` | 标准型,4 段 |
|
||||
| `add-product-shopee` | 标准型,3 段 |
|
||||
| `enter-sales-receipt-kingdee` | 标准型 |
|
||||
| `fill-export-declaration-single-window` | 标准型,平台含连字符 |
|
||||
| `generate-article-all` | scope 型 |
|
||||
|
||||
### ❌ 反例
|
||||
|
||||
| slug | 原因 |
|
||||
|------|------|
|
||||
| `amazon-download-settlement-report` | 平台在最前 |
|
||||
| `enter-cross-border-sales-receipt-voucher-kingdee` | 过长、noun 臃肿 |
|
||||
| `reconcile-finance` | 新技能禁止 2 段无平台 |
|
||||
| `download_settlement_amazon` | 非 kebab-case(下划线) |
|
||||
|
||||
---
|
||||
|
||||
## 11. 反模式
|
||||
|
||||
- **平台前置**:`amazon-download-...`、`shopee-add-...`
|
||||
- **英文占位 name**:复制后仍保留 `My Skill` 或纯英文 slug 风格名称
|
||||
- **只改目录名**:`SKILL.md` slug 与 `constants.SKILL_SLUG` 未同步
|
||||
- **noun 堆砌**:中间段过多导致超 48 字符或难以阅读
|
||||
- **仿历史 2 段 repo**:`reconcile-finance`、`content-manager` 等
|
||||
|
||||
---
|
||||
|
||||
## 12. 演进说明(当前不启用)
|
||||
|
||||
当技能库规模 **> 200** 时,可再评估引入 `{domain}-verb-noun-platform` 前缀形态。**当前不启用**,新技能一律使用本文 verb-noun-platform 标准型或 scope 型。
|
||||
|
||||
---
|
||||
|
||||
## 13. 历史形态说明
|
||||
|
||||
以下为代表性**历史 repo**,新技能**勿仿**:
|
||||
|
||||
| 历史 slug | 说明 |
|
||||
|-----------|------|
|
||||
| `account-manager` | 跨平台账号管理,非 verb-noun-platform |
|
||||
| `reconcile-finance` | 2 段无 platform |
|
||||
| `scrape-contacts-lianjia` | 平台名不在当前白名单(链家) |
|
||||
| `skill-template` | 模板仓库本身 |
|
||||
|
||||
校验器通过 `LEGACY_EXEMPT_SLUGS` 对模板占位与少数豁免 slug 跳过语义检查;**新复制出去的业务 skill 不在豁免表内**,必须满足 §3 硬约束。
|
||||
|
||||
---
|
||||
|
||||
## 14. 相关文档
|
||||
|
||||
| 文档 | 内容 |
|
||||
|------|------|
|
||||
| [`DEVELOPMENT.md`](DEVELOPMENT.md) | 复制模板步骤、发布前检查清单 |
|
||||
| [`REQUIREMENTS.md`](REQUIREMENTS.md) | §0 技能标识必填项 |
|
||||
| [`../references/SCHEMA.md`](../references/SCHEMA.md) | kebab-case 与数据库路径 |
|
||||
| [`../SKILL.md`](../SKILL.md) | frontmatter 命名摘要 |
|
||||
@@ -3,12 +3,13 @@
|
||||
本目录面向**人类开发者**与 **AI 编程代理**。开始定制 skill 前,建议按以下顺序阅读:
|
||||
|
||||
1. [`REQUIREMENTS.md`](REQUIREMENTS.md) — 需求文档模板与验收标准
|
||||
2. [`DEVELOPMENT.md`](DEVELOPMENT.md) — 完整开发步骤与目录规范
|
||||
3. [`TESTING.md`](TESTING.md) — 测试分层、隔离数据根与档位开关
|
||||
4. [`ADAPTER.md`](ADAPTER.md) — 涉及外部系统对接时
|
||||
5. [`RPA.md`](RPA.md) — 涉及浏览器 / 桌面 / 手机自动化时
|
||||
6. [`CONFIG.md`](CONFIG.md) — `.env` 规范与 bootstrap 机制
|
||||
7. [`RUNTIME.md`](RUNTIME.md) — 共享 runtime、数据路径、发布打包与编码约定
|
||||
2. [`NAMING.md`](NAMING.md) — slug / 仓库名命名规范(复制模板前必读)
|
||||
3. [`DEVELOPMENT.md`](DEVELOPMENT.md) — 完整开发步骤与目录规范
|
||||
4. [`TESTING.md`](TESTING.md) — 测试分层、隔离数据根与档位开关
|
||||
5. [`ADAPTER.md`](ADAPTER.md) — 涉及外部系统对接时
|
||||
6. [`RPA.md`](RPA.md) — 涉及浏览器 / 桌面 / 手机自动化时
|
||||
7. [`CONFIG.md`](CONFIG.md) — `.env` 规范与 bootstrap 机制
|
||||
8. [`RUNTIME.md`](RUNTIME.md) — 共享 runtime、数据路径、发布打包与编码约定
|
||||
|
||||
Agent 调用契约见 [`../references/CLI.md`](../references/CLI.md)、[`../references/SCHEMA.md`](../references/SCHEMA.md)。
|
||||
用户市场说明见根目录 [`README.md`](../README.md),不要写进本目录。
|
||||
|
||||
@@ -15,6 +15,14 @@
|
||||
|
||||
# REQUIREMENTS
|
||||
|
||||
## 0. 技能标识(复制 REQUIREMENTS 时必填)
|
||||
|
||||
- 拟定 slug(英文,符合 `development/NAMING.md`):
|
||||
- 中文 name:
|
||||
- 中文 description(一句话):
|
||||
- account-manager platform_key(如有):
|
||||
- 命名形态:标准型 / scope 型
|
||||
|
||||
## 1. 文档目标
|
||||
|
||||
说明这份需求文档的作用,以及它解决什么问题。
|
||||
@@ -149,7 +157,7 @@
|
||||
- `health`、`version` 命令执行正常
|
||||
- 主命令(如 `run`)在 mock / simulator 档位可重复验证
|
||||
- `python tests/run_tests.py -v` 必跑测试全部通过
|
||||
- `task_logs` 写入和查询符合 `references/SCHEMA.md`
|
||||
- `task_logs` 写入和查询符合 `references/SCHEMA.md`(含 `created_at` / `updated_at` Unix 秒级规范)
|
||||
- `init_db()` 已写入 `_jiangchang_tables` / `_jiangchang_columns`;用户可见表/字段具备中文 `display_name`
|
||||
- 字段展示顺序与 `PRAGMA table_info(task_logs)` 的 cid 一致;不依赖 `display_order`
|
||||
- `tests/test_display_metadata.py` 通过
|
||||
|
||||
@@ -71,7 +71,7 @@ RPA 录屏成片(`RpaVideoSession`)、ffmpeg 路径、背景音乐探测均
|
||||
|
||||
- `scripts/main.py`:唯一 CLI 入口
|
||||
- `scripts/cli/`:参数解析与命令分发
|
||||
- `scripts/db/`:SQLite 或本地持久化层
|
||||
- `scripts/db/`:SQLite 或本地持久化层(标准时间字段见 `references/SCHEMA.md`)
|
||||
- `scripts/service/`:业务用例与外部交互
|
||||
- `scripts/util/`:通用工具、常量、日志、路径薄封装
|
||||
|
||||
|
||||
@@ -29,8 +29,10 @@
|
||||
- `SKILL.md` YAML slug vs [`constants.SKILL_SLUG`](../scripts/util/constants.py);
|
||||
- SQLite 骨架:`task_logs` 创建幂等与仓储读写;
|
||||
- 数据管理元数据:`_jiangchang_*` 幂等初始化、中文展示名、`PRAGMA table_info` 字段顺序(见 `test_display_metadata.py`);
|
||||
- 标准时间字段:`created_at` / `updated_at` 默认值、trigger 维护与 `datetime_unix_seconds` 元数据(见 `test_timestamp_columns.py`);
|
||||
- **adapter profile**:[`tests/test_adapter_profile_policy.py`](../tests/test_adapter_profile_policy.py) + [`adapter_test_utils`](../tests/adapter_test_utils.py) ——验证在未授权情况下绝不误判开启真实网络/RPA。
|
||||
- **发布打包守护**:[`tests/test_release_packaging_constraints.py`](../tests/test_release_packaging_constraints.py) ——`scripts/**/*.py` 单文件 < 1000 行、文本文件 UTF-8 without BOM。
|
||||
- **slug 语义**:[`tests/test_slug_naming.py`](../tests/test_slug_naming.py) ——verb-noun-platform 命名规范校验。
|
||||
|
||||
**原则:`tests/test_*.py` 不允许隐形访问外网、不允许拉起真实浏览器、不允许读写开发者机器的真实数据根**。如果需要仿真服务器,也应仅在 integration(并由档位变量放行)。
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ metadata:
|
||||
|
||||
- `name`:用户可见的中文业务名称(可含品牌,如 `GEO 文章生成`、`1688 联系人采集`)。
|
||||
- `description`:能力说明,不是技能名称。
|
||||
- `metadata.openclaw.slug`:机器标识,kebab-case 英文;目录名与默认数据库文件名使用 slug。
|
||||
- `metadata.openclaw.slug`:机器标识,kebab-case 英文;目录名与默认数据库文件名使用 slug。slug 语义规范见 [`../development/NAMING.md`](../development/NAMING.md);本文件仅描述数据库与 kebab-case 格式。
|
||||
- 复制模板后不得将 `your-skill-slug`、`My Skill` 等占位内容作为正式技能发布名称。
|
||||
|
||||
## 数据库路径与命名
|
||||
@@ -69,7 +69,7 @@ metadata:
|
||||
| `visible` | 是 | 是否展示 |
|
||||
| `searchable` | 是 | 是否可搜索 |
|
||||
| `editable` | 是 | 是否可编辑(与宿主权限结合) |
|
||||
| `display_type` | 否 | 如 `text` / `textarea` |
|
||||
| `display_type` | 否 | 如 `text` / `textarea` / `datetime_unix_seconds`(标准时间字段见下文) |
|
||||
| `options_json` | 否 | 枚举选项 JSON |
|
||||
|
||||
这两张表是**数据字典**,不是业务数据;宿主不会把它们当作普通业务表展示。
|
||||
@@ -90,7 +90,36 @@ metadata:
|
||||
4. 状态、结果、错误等辅助字段
|
||||
5. created_at(如有)
|
||||
6. updated_at(如有)
|
||||
```
|
||||
|
||||
## 标准时间字段(新技能统一规范)
|
||||
|
||||
新技能业务表若需要时间审计字段,**统一**采用 Unix **秒级**时间戳,不要使用毫秒时间戳、ISO 字符串或 SQLite 文本 datetime。
|
||||
|
||||
| 项目 | 规范 |
|
||||
|------|------|
|
||||
| 物理字段 | `created_at INTEGER`、`updated_at INTEGER` |
|
||||
| 存储格式 | Unix 秒级整数 |
|
||||
| 展示元数据 `display_type` | `datetime_unix_seconds` |
|
||||
| 中文名 | `created_at` → **创建时间**;`updated_at` → **更新时间** |
|
||||
| 用户编辑 | `editable = 0`(系统字段,宿主只展示格式化结果,不写回字符串) |
|
||||
| 字段顺序 | 位于业务字段末尾:`… → created_at → updated_at` |
|
||||
| 排序 | **不使用** `display_order`;顺序仅来自 `PRAGMA table_info` |
|
||||
|
||||
### 自动生成与维护
|
||||
|
||||
- **created_at**:`INTEGER NOT NULL DEFAULT (unixepoch())`;插入时可省略,由数据库生成。
|
||||
- **updated_at**:`INTEGER NOT NULL DEFAULT (unixepoch())`;插入时同样可省略。
|
||||
- **updated_at 更新**:模板默认表 `task_logs` 通过 SQLite trigger `{table}_set_updated_at` 在业务字段更新后自动刷新;若技能所有 UPDATE 都经过统一 repository,也可在 repository 层设置 `updated_at = unixepoch()`,**不要**叠加两套机制。
|
||||
|
||||
实现参考:
|
||||
|
||||
- DDL 与 trigger:`scripts/db/connection.py`、`scripts/db/timestamp_columns.py`
|
||||
- 中文元数据种子:`scripts/db/display_metadata.py`
|
||||
- 自检:`scripts/db/display_metadata_validator.py`
|
||||
|
||||
宿主(匠厂)只负责把 `display_type = datetime_unix_seconds` 格式化为 `YYYY-MM-DD HH:mm:ss` 展示,**不修改**数据库原始值。
|
||||
|
||||
> `datetime_unix_milliseconds`、`datetime_iso` 是宿主兼容能力,**不是**新技能模板推荐标准。
|
||||
|
||||
### 模板默认:`task_logs`
|
||||
|
||||
@@ -106,11 +135,13 @@ CREATE TABLE IF NOT EXISTS task_logs (
|
||||
status TEXT NOT NULL,
|
||||
error_msg TEXT,
|
||||
result_summary TEXT,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
||||
updated_at INTEGER NOT NULL DEFAULT (unixepoch())
|
||||
);
|
||||
```
|
||||
|
||||
插入时可省略 `created_at`、`updated_at`;`updated_at` 在 UPDATE 时由 trigger `task_logs_set_updated_at` 自动维护(见 `scripts/db/timestamp_columns.py`)。
|
||||
|
||||
对应中文展示(由 `init_db()` 写入元数据表):
|
||||
|
||||
| 物理字段 | 中文名 |
|
||||
@@ -123,8 +154,8 @@ CREATE TABLE IF NOT EXISTS task_logs (
|
||||
| status | 状态 |
|
||||
| error_msg | 错误信息 |
|
||||
| result_summary | 结果摘要 |
|
||||
| created_at | 创建时间 |
|
||||
| updated_at | 更新时间 |
|
||||
| created_at | 创建时间(`display_type = datetime_unix_seconds`,不可编辑) |
|
||||
| updated_at | 更新时间(`display_type = datetime_unix_seconds`,不可编辑) |
|
||||
|
||||
表中文名:**任务日志**。
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
import sqlite3
|
||||
|
||||
from db.display_metadata import init_display_metadata
|
||||
from db.timestamp_columns import init_task_logs_timestamp_maintenance
|
||||
from util.runtime_paths import get_db_path
|
||||
|
||||
|
||||
@@ -27,11 +28,12 @@ def init_db() -> None:
|
||||
status TEXT NOT NULL,
|
||||
error_msg TEXT,
|
||||
result_summary TEXT,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
||||
updated_at INTEGER NOT NULL DEFAULT (unixepoch())
|
||||
)
|
||||
"""
|
||||
)
|
||||
init_task_logs_timestamp_maintenance(cur)
|
||||
init_display_metadata(cur)
|
||||
conn.commit()
|
||||
finally:
|
||||
|
||||
@@ -14,6 +14,8 @@ LEGACY_METADATA_TABLES = frozenset({"_schema_meta"})
|
||||
METADATA_TABLES = INTERNAL_METADATA_TABLES | LEGACY_METADATA_TABLES
|
||||
|
||||
TASK_LOGS_TABLE = "task_logs"
|
||||
DATETIME_UNIX_SECONDS = "datetime_unix_seconds"
|
||||
STANDARD_TIMESTAMP_COLUMNS = frozenset({"created_at", "updated_at"})
|
||||
|
||||
_JIANGCHANG_TABLES_DDL = """
|
||||
CREATE TABLE IF NOT EXISTS _jiangchang_tables (
|
||||
@@ -59,8 +61,24 @@ TASK_LOGS_COLUMN_METADATA: tuple[dict[str, object], ...] = (
|
||||
{"column_name": "status", "display_name": "状态", "description": "任务当前状态", "visible": 1, "searchable": 1, "editable": 1},
|
||||
{"column_name": "error_msg", "display_name": "错误信息", "description": "任务失败时的错误说明", "visible": 1, "searchable": 1, "editable": 1},
|
||||
{"column_name": "result_summary", "display_name": "结果摘要", "description": "任务执行结果摘要", "visible": 1, "searchable": 1, "editable": 1},
|
||||
{"column_name": "created_at", "display_name": "创建时间", "description": "记录创建时间", "visible": 1, "searchable": 1, "editable": 0},
|
||||
{"column_name": "updated_at", "display_name": "更新时间", "description": "记录最后更新时间", "visible": 1, "searchable": 1, "editable": 0},
|
||||
{
|
||||
"column_name": "created_at",
|
||||
"display_name": "创建时间",
|
||||
"description": "记录创建时间(Unix 秒级时间戳,数据库自动生成)",
|
||||
"visible": 1,
|
||||
"searchable": 0,
|
||||
"editable": 0,
|
||||
"display_type": DATETIME_UNIX_SECONDS,
|
||||
},
|
||||
{
|
||||
"column_name": "updated_at",
|
||||
"display_name": "更新时间",
|
||||
"description": "记录最后更新时间(Unix 秒级时间戳,自动维护)",
|
||||
"visible": 1,
|
||||
"searchable": 0,
|
||||
"editable": 0,
|
||||
"display_type": DATETIME_UNIX_SECONDS,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@@ -94,24 +112,26 @@ def upsert_column_metadata(
|
||||
visible: int = 1,
|
||||
searchable: int = 1,
|
||||
editable: int = 1,
|
||||
display_type: str | None = None,
|
||||
) -> None:
|
||||
# display_order 保留为 NULL:宿主按 PRAGMA table_info cid 展示字段,不读 display_order。
|
||||
cursor.execute(
|
||||
"""
|
||||
INSERT INTO _jiangchang_columns (
|
||||
table_name, column_name, display_name, description,
|
||||
display_order, visible, searchable, editable
|
||||
display_order, visible, searchable, editable, display_type
|
||||
)
|
||||
VALUES (?, ?, ?, ?, NULL, ?, ?, ?)
|
||||
VALUES (?, ?, ?, ?, NULL, ?, ?, ?, ?)
|
||||
ON CONFLICT(table_name, column_name) DO UPDATE SET
|
||||
display_name = excluded.display_name,
|
||||
description = excluded.description,
|
||||
display_order = NULL,
|
||||
visible = excluded.visible,
|
||||
searchable = excluded.searchable,
|
||||
editable = excluded.editable
|
||||
editable = excluded.editable,
|
||||
display_type = excluded.display_type
|
||||
""",
|
||||
(table_name, column_name, display_name, description, visible, searchable, editable),
|
||||
(table_name, column_name, display_name, description, visible, searchable, editable, display_type),
|
||||
)
|
||||
|
||||
|
||||
@@ -135,6 +155,7 @@ def seed_task_logs_display_metadata(cursor) -> None:
|
||||
visible=int(column["visible"]),
|
||||
searchable=int(column["searchable"]),
|
||||
editable=int(column["editable"]),
|
||||
display_type=str(column["display_type"]) if column.get("display_type") else None,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -8,13 +8,17 @@ import sqlite3
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from db.display_metadata import (
|
||||
DATETIME_UNIX_SECONDS,
|
||||
METADATA_TABLES,
|
||||
STANDARD_TIMESTAMP_COLUMNS,
|
||||
TASK_LOGS_COLUMN_METADATA,
|
||||
TASK_LOGS_TABLE,
|
||||
iter_user_tables,
|
||||
list_physical_column_names,
|
||||
)
|
||||
from db.timestamp_columns import has_updated_at_trigger
|
||||
from util.constants import SKILL_SLUG
|
||||
from util.slug_naming import LEGACY_EXEMPT_SLUGS, validate_slug_semantics
|
||||
|
||||
_TEMPLATE_PLACEHOLDER_SLUGS = frozenset({"your-skill-slug", "your_skill_slug"})
|
||||
_ENGLISH_PLACEHOLDER_NAME_PATTERNS = (
|
||||
@@ -124,9 +128,127 @@ def validate_skill_frontmatter(skill_root: str, *, allow_template_placeholders:
|
||||
f"SKILL.md slug ({slug!r}) 与 util.constants.SKILL_SLUG ({constants_slug!r}) 不一致"
|
||||
)
|
||||
|
||||
if slug and slug not in LEGACY_EXEMPT_SLUGS and _KEBAB_SLUG.fullmatch(slug):
|
||||
naming_errors, naming_warnings = validate_slug_semantics(
|
||||
slug,
|
||||
strict=not allow_template_placeholders,
|
||||
)
|
||||
report.errors.extend(naming_errors)
|
||||
report.warnings.extend(naming_warnings)
|
||||
|
||||
return report
|
||||
|
||||
|
||||
def _quote_identifier(name: str) -> str:
|
||||
return '"' + name.replace('"', '""') + '"'
|
||||
|
||||
|
||||
def _table_column_info(cursor, table_name: str) -> dict[str, dict[str, object]]:
|
||||
cursor.execute(f"PRAGMA table_info({_quote_identifier(table_name)})")
|
||||
info: dict[str, dict[str, object]] = {}
|
||||
for row in cursor.fetchall():
|
||||
info[str(row[1])] = {
|
||||
"cid": int(row[0]),
|
||||
"name": str(row[1]),
|
||||
"type": str(row[2] or ""),
|
||||
"notnull": int(row[3] or 0),
|
||||
"dflt_value": row[4],
|
||||
"pk": int(row[5] or 0),
|
||||
}
|
||||
return info
|
||||
|
||||
|
||||
def _default_uses_unixepoch(default_value: object) -> bool:
|
||||
if default_value is None:
|
||||
return False
|
||||
text = str(default_value).strip().lower()
|
||||
return "unixepoch" in text
|
||||
|
||||
|
||||
def _expected_timestamp_display_name(column_name: str) -> str:
|
||||
if column_name == "created_at":
|
||||
return "创建时间"
|
||||
if column_name == "updated_at":
|
||||
return "更新时间"
|
||||
return ""
|
||||
|
||||
|
||||
def validate_standard_timestamp_columns(
|
||||
cursor,
|
||||
table_name: str,
|
||||
*,
|
||||
physical_columns: list[str] | None = None,
|
||||
) -> tuple[list[str], list[str]]:
|
||||
errors: list[str] = []
|
||||
warnings: list[str] = []
|
||||
ordered = physical_columns or list_physical_column_names(cursor, table_name)
|
||||
column_info = _table_column_info(cursor, table_name)
|
||||
|
||||
for column_name in STANDARD_TIMESTAMP_COLUMNS:
|
||||
if column_name not in ordered:
|
||||
continue
|
||||
|
||||
physical = column_info.get(column_name)
|
||||
if not physical:
|
||||
errors.append(f"{table_name}.{column_name} 缺少物理字段定义")
|
||||
continue
|
||||
|
||||
if "INT" not in str(physical["type"]).upper():
|
||||
errors.append(
|
||||
f"{table_name}.{column_name} 字段类型应为 INTEGER(Unix 秒级时间戳),当前为 {physical['type']!r}"
|
||||
)
|
||||
|
||||
if column_name == "created_at" and not _default_uses_unixepoch(physical["dflt_value"]):
|
||||
errors.append(
|
||||
f"{table_name}.created_at 应设置数据库默认值 DEFAULT (unixepoch()),当前默认值为 {physical['dflt_value']!r}"
|
||||
)
|
||||
if column_name == "updated_at" and not _default_uses_unixepoch(physical["dflt_value"]):
|
||||
errors.append(
|
||||
f"{table_name}.updated_at 应设置数据库初始默认值 DEFAULT (unixepoch()),当前默认值为 {physical['dflt_value']!r}"
|
||||
)
|
||||
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT display_name, display_type, editable, searchable, display_order
|
||||
FROM _jiangchang_columns
|
||||
WHERE table_name = ? AND column_name = ?
|
||||
""",
|
||||
(table_name, column_name),
|
||||
)
|
||||
meta_row = cursor.fetchone()
|
||||
if not meta_row:
|
||||
errors.append(f"{table_name}.{column_name} 缺少 _jiangchang_columns 元数据")
|
||||
continue
|
||||
|
||||
display_name, display_type, editable, searchable, display_order = meta_row
|
||||
expected_name = _expected_timestamp_display_name(column_name)
|
||||
if str(display_name or "").strip() != expected_name:
|
||||
errors.append(
|
||||
f"{table_name}.{column_name} 的 display_name 应为 {expected_name!r},当前为 {display_name!r}"
|
||||
)
|
||||
if str(display_type or "").strip() != DATETIME_UNIX_SECONDS:
|
||||
errors.append(
|
||||
f"{table_name}.{column_name} 的 display_type 应为 {DATETIME_UNIX_SECONDS!r},当前为 {display_type!r}"
|
||||
)
|
||||
if editable is None or int(editable) != 0:
|
||||
errors.append(f"{table_name}.{column_name} 的 editable 应为 0(系统时间字段不可编辑)")
|
||||
if display_order is not None:
|
||||
errors.append(f"{table_name}.{column_name} 的 display_order 应为 NULL")
|
||||
if searchable is not None and int(searchable) != 0:
|
||||
warnings.append(f"{table_name}.{column_name} 的时间字段通常不必开启 searchable")
|
||||
|
||||
if "created_at" in ordered and "updated_at" in ordered:
|
||||
if ordered.index("updated_at") < ordered.index("created_at"):
|
||||
warnings.append(f"{table_name}: updated_at 不应出现在 created_at 之前")
|
||||
|
||||
if "updated_at" in ordered and not has_updated_at_trigger(cursor, table_name):
|
||||
errors.append(
|
||||
f"{table_name}.updated_at 缺少自动维护 trigger(期望 {table_name}_set_updated_at)"
|
||||
)
|
||||
|
||||
return errors, warnings
|
||||
|
||||
|
||||
def column_order_warnings(table_name: str, ordered_names: list[str]) -> list[str]:
|
||||
warnings: list[str] = []
|
||||
if "id" in ordered_names and ordered_names.index("id") != 0:
|
||||
@@ -208,6 +330,14 @@ def validate_display_metadata(conn: sqlite3.Connection, *, check_column_order: b
|
||||
f"字段 {table_name}.{column_name} 的 display_name 应使用中文:{display_name!r}"
|
||||
)
|
||||
|
||||
ts_errors, ts_warnings = validate_standard_timestamp_columns(
|
||||
cur,
|
||||
table_name,
|
||||
physical_columns=physical_columns,
|
||||
)
|
||||
report.errors.extend(ts_errors)
|
||||
report.warnings.extend(ts_warnings)
|
||||
|
||||
cur.execute("SELECT table_name, column_name FROM _jiangchang_columns")
|
||||
for table_name, column_name in cur.fetchall():
|
||||
if table_name in METADATA_TABLES:
|
||||
|
||||
@@ -9,7 +9,6 @@ from __future__ import annotations
|
||||
from typing import Any, List, Optional, Tuple
|
||||
|
||||
from db.connection import get_conn, init_db
|
||||
from util.timeutil import now_unix
|
||||
|
||||
|
||||
def save_task_log(
|
||||
@@ -22,17 +21,16 @@ def save_task_log(
|
||||
result_summary: Optional[str] = None,
|
||||
) -> int:
|
||||
init_db()
|
||||
now = now_unix()
|
||||
conn = get_conn()
|
||||
try:
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO task_logs
|
||||
(task_type, target_id, input_id, input_title, status, error_msg, result_summary, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
(task_type, target_id, input_id, input_title, status, error_msg, result_summary)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(task_type, target_id, input_id, input_title or "", status, error_msg, result_summary, now, now),
|
||||
(task_type, target_id, input_id, input_title or "", status, error_msg, result_summary),
|
||||
)
|
||||
new_id = int(cur.lastrowid)
|
||||
conn.commit()
|
||||
|
||||
36
scripts/db/timestamp_columns.py
Normal file
36
scripts/db/timestamp_columns.py
Normal file
@@ -0,0 +1,36 @@
|
||||
"""标准时间字段:Unix 秒级时间戳、默认值与 updated_at 自动维护。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
STANDARD_TIMESTAMP_COLUMNS = frozenset({"created_at", "updated_at"})
|
||||
DATETIME_UNIX_SECONDS = "datetime_unix_seconds"
|
||||
TASK_LOGS_UPDATED_AT_TRIGGER = "task_logs_set_updated_at"
|
||||
|
||||
_TASK_LOGS_UPDATED_AT_TRIGGER_DDL = f"""
|
||||
CREATE TRIGGER IF NOT EXISTS {TASK_LOGS_UPDATED_AT_TRIGGER}
|
||||
AFTER UPDATE ON task_logs
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
UPDATE task_logs
|
||||
SET updated_at = unixepoch()
|
||||
WHERE id = NEW.id AND updated_at = OLD.updated_at;
|
||||
END
|
||||
"""
|
||||
|
||||
|
||||
def init_task_logs_timestamp_maintenance(cursor) -> None:
|
||||
"""幂等创建 task_logs.updated_at 自动维护 trigger。"""
|
||||
cursor.execute(_TASK_LOGS_UPDATED_AT_TRIGGER_DDL)
|
||||
|
||||
|
||||
def has_updated_at_trigger(cursor, table_name: str) -> bool:
|
||||
trigger_name = f"{table_name}_set_updated_at"
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT 1 FROM sqlite_master
|
||||
WHERE type = 'trigger' AND name = ? AND tbl_name = ?
|
||||
LIMIT 1
|
||||
""",
|
||||
(trigger_name, table_name),
|
||||
)
|
||||
return cursor.fetchone() is not None
|
||||
@@ -1,6 +1,6 @@
|
||||
"""技能标识、版本与平台公共库约束(复制后请修改 slug/version/logger)。"""
|
||||
|
||||
SKILL_SLUG = "your-skill-slug"
|
||||
SKILL_VERSION = "1.0.14"
|
||||
SKILL_VERSION = "1.0.32"
|
||||
LOG_LOGGER_NAME = "openclaw.skill.your_skill_slug"
|
||||
PLATFORM_KIT_MIN_VERSION = "1.0.17"
|
||||
|
||||
227
scripts/util/slug_naming.py
Normal file
227
scripts/util/slug_naming.py
Normal file
@@ -0,0 +1,227 @@
|
||||
"""技能 slug 语义校验(verb-noun-platform 规范,见 development/NAMING.md)。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
|
||||
_KEBAB_SLUG = re.compile(r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
|
||||
_MAX_SLUG_LENGTH = 48
|
||||
_MIN_SEGMENTS = 3
|
||||
_MAX_SEGMENTS = 5
|
||||
_MAX_NOUN_SEGMENTS = 3
|
||||
|
||||
_SKILL_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
_NAMING_DIR = os.path.join(_SKILL_ROOT, "assets", "naming")
|
||||
|
||||
_BUILTIN_VERBS = frozenset(
|
||||
{
|
||||
"scrape",
|
||||
"receive",
|
||||
"download",
|
||||
"export",
|
||||
"add",
|
||||
"fill",
|
||||
"enter",
|
||||
"bind",
|
||||
"withdraw",
|
||||
"reconcile",
|
||||
"verify",
|
||||
"submit",
|
||||
"query",
|
||||
"process",
|
||||
"reply",
|
||||
"generate",
|
||||
"draft",
|
||||
"review",
|
||||
"confirm",
|
||||
"track",
|
||||
"upload",
|
||||
"match",
|
||||
"triage",
|
||||
"ship",
|
||||
"reprice",
|
||||
}
|
||||
)
|
||||
_BUILTIN_PLATFORMS = frozenset(
|
||||
{
|
||||
"amazon",
|
||||
"shopee",
|
||||
"xinghang",
|
||||
"alibaba",
|
||||
"kingdee",
|
||||
"icbc",
|
||||
"pingpong",
|
||||
"worldfirst",
|
||||
"paypal",
|
||||
"lianlian",
|
||||
"single-window",
|
||||
"export-rebate",
|
||||
"etax",
|
||||
}
|
||||
)
|
||||
_BUILTIN_SCOPES = frozenset({"all", "batch", "multi", "default"})
|
||||
|
||||
LEGACY_EXEMPT_SLUGS: frozenset[str] = frozenset(
|
||||
{
|
||||
"account-manager",
|
||||
"skill-template",
|
||||
"your-skill-slug",
|
||||
"your_skill_slug",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def load_wordlist(filename: str) -> frozenset[str]:
|
||||
"""从 assets/naming/{filename} 加载词表;文件不存在时 fallback 内置集合。"""
|
||||
fallbacks = {
|
||||
"verbs.txt": _BUILTIN_VERBS,
|
||||
"platforms.txt": _BUILTIN_PLATFORMS,
|
||||
"scopes.txt": _BUILTIN_SCOPES,
|
||||
}
|
||||
fallback = fallbacks.get(filename, frozenset())
|
||||
path = os.path.join(_NAMING_DIR, filename)
|
||||
if not os.path.isfile(path):
|
||||
return fallback
|
||||
words: set[str] = set()
|
||||
with open(path, encoding="utf-8") as f:
|
||||
for line in f:
|
||||
word = line.strip()
|
||||
if word and not word.startswith("#"):
|
||||
words.add(word)
|
||||
return frozenset(words) if words else fallback
|
||||
|
||||
|
||||
def parse_slug_segments(slug: str) -> list[str]:
|
||||
"""按 '-' 分割;非法字符由上层 kebab 校验处理。"""
|
||||
slug = slug.strip()
|
||||
if not slug:
|
||||
return []
|
||||
return slug.split("-")
|
||||
|
||||
|
||||
def _match_platform_suffix(
|
||||
segments: list[str],
|
||||
platforms: frozenset[str],
|
||||
) -> tuple[str, int] | None:
|
||||
"""返回 (platform_name, platform_part_count) 或 None。"""
|
||||
for platform in sorted(platforms, key=lambda item: (-item.count("-"), item)):
|
||||
parts = platform.split("-")
|
||||
part_count = len(parts)
|
||||
if len(segments) < part_count + 2:
|
||||
continue
|
||||
if segments[-part_count:] == parts:
|
||||
return platform, part_count
|
||||
return None
|
||||
|
||||
|
||||
def classify_slug_form(segments: list[str], *, slug: str = "") -> str:
|
||||
"""
|
||||
返回: 'legacy_exempt' | 'standard' | 'scope' | 'invalid'
|
||||
|
||||
- legacy_exempt: slug in LEGACY_EXEMPT_SLUGS
|
||||
- standard: 末段匹配 platforms, 首段 in verbs, 3<=len<=5
|
||||
- scope: 末段 in scopes, 首段 in verbs, 3<=len<=5
|
||||
- invalid: 其他
|
||||
"""
|
||||
if slug in LEGACY_EXEMPT_SLUGS:
|
||||
return "legacy_exempt"
|
||||
|
||||
if not segments or not (_MIN_SEGMENTS <= len(segments) <= _MAX_SEGMENTS):
|
||||
return "invalid"
|
||||
|
||||
verbs = load_wordlist("verbs.txt")
|
||||
platforms = load_wordlist("platforms.txt")
|
||||
scopes = load_wordlist("scopes.txt")
|
||||
|
||||
if segments[0] not in verbs:
|
||||
return "invalid"
|
||||
|
||||
if segments[-1] in scopes:
|
||||
return "scope"
|
||||
|
||||
if _match_platform_suffix(segments, platforms) is not None:
|
||||
return "standard"
|
||||
|
||||
return "invalid"
|
||||
|
||||
|
||||
def validate_slug_semantics(
|
||||
slug: str,
|
||||
*,
|
||||
strict: bool = True,
|
||||
) -> tuple[list[str], list[str]]:
|
||||
"""返回 (errors, warnings)。"""
|
||||
slug = slug.strip()
|
||||
if slug in LEGACY_EXEMPT_SLUGS:
|
||||
return [], []
|
||||
|
||||
errors: list[str] = []
|
||||
warnings: list[str] = []
|
||||
|
||||
if not _KEBAB_SLUG.fullmatch(slug):
|
||||
msg = f"slug 必须为 kebab-case(小写字母、数字、连字符):{slug!r}"
|
||||
(errors if strict else warnings).append(msg)
|
||||
return errors, warnings
|
||||
|
||||
if len(slug) > _MAX_SLUG_LENGTH:
|
||||
msg = f"slug 长度不得超过 {_MAX_SLUG_LENGTH} 字符(当前 {len(slug)}):{slug!r}"
|
||||
(errors if strict else warnings).append(msg)
|
||||
|
||||
segments = parse_slug_segments(slug)
|
||||
segment_count = len(segments)
|
||||
|
||||
if segment_count < _MIN_SEGMENTS:
|
||||
msg = (
|
||||
f"slug 段数应为 {_MIN_SEGMENTS}~{_MAX_SEGMENTS}(verb-noun-platform),"
|
||||
f"当前为 {segment_count} 段:{slug!r};新技能禁止 2 段无平台形态(如 reconcile-finance)"
|
||||
)
|
||||
(errors if strict else warnings).append(msg)
|
||||
return errors, warnings
|
||||
|
||||
if segment_count > _MAX_SEGMENTS:
|
||||
msg = f"slug 段数不得超过 {_MAX_SEGMENTS}(当前 {segment_count}):{slug!r}"
|
||||
(errors if strict else warnings).append(msg)
|
||||
|
||||
verbs = load_wordlist("verbs.txt")
|
||||
platforms = load_wordlist("platforms.txt")
|
||||
scopes = load_wordlist("scopes.txt")
|
||||
|
||||
if segments[0] in platforms and segments[0] not in verbs:
|
||||
msg = f"平台名不应放在 slug 最前:{slug!r}(应为 verb-noun-platform)"
|
||||
(errors if strict else warnings).append(msg)
|
||||
|
||||
if segments[0] not in verbs:
|
||||
msg = f"slug 首段应为动词白名单中的词:{segments[0]!r}(slug={slug!r})"
|
||||
(errors if strict else warnings).append(msg)
|
||||
|
||||
form = classify_slug_form(segments, slug=slug)
|
||||
if form == "scope":
|
||||
if segments[-1] not in scopes:
|
||||
msg = f"slug 末段应为 scope 白名单中的词:{segments[-1]!r}(slug={slug!r})"
|
||||
(errors if strict else warnings).append(msg)
|
||||
noun_count = segment_count - 2
|
||||
elif form == "standard":
|
||||
platform_match = _match_platform_suffix(segments, platforms)
|
||||
if platform_match is None:
|
||||
msg = f"slug 末段应匹配平台白名单:{slug!r}"
|
||||
(errors if strict else warnings).append(msg)
|
||||
noun_count = 0
|
||||
else:
|
||||
_platform_name, platform_part_count = platform_match
|
||||
noun_count = segment_count - 1 - platform_part_count
|
||||
else:
|
||||
if segment_count >= _MIN_SEGMENTS:
|
||||
if segments[-1] not in scopes:
|
||||
platform_match = _match_platform_suffix(segments, platforms)
|
||||
if platform_match is None:
|
||||
msg = f"slug 末段应匹配平台或 scope 白名单:{slug!r}"
|
||||
(errors if strict else warnings).append(msg)
|
||||
noun_count = 0
|
||||
|
||||
if noun_count > _MAX_NOUN_SEGMENTS:
|
||||
warnings.append(
|
||||
f"slug 中间名词段建议不超过 {_MAX_NOUN_SEGMENTS} 个词(当前 {noun_count}):{slug!r}"
|
||||
)
|
||||
|
||||
return errors, warnings
|
||||
@@ -74,6 +74,7 @@ python tests/run_tests.py test_cli_smoke
|
||||
| ``SKILL.md`` slug 与 ``constants.SKILL_SLUG`` | ``test_skill_metadata.py`` |
|
||||
| DB / ``task_logs`` 骨架冒烟 | ``test_db_smoke.py`` |
|
||||
| 数据管理元数据 / 字段顺序 | ``test_display_metadata.py`` |
|
||||
| 标准时间字段(Unix 秒 / trigger / 元数据) | ``test_timestamp_columns.py`` |
|
||||
| **adapter profile / 外呼授权策略** | ``test_adapter_profile_policy.py`` + ``adapter_test_utils.py``(非 ``test_`` 前缀,不自动当用例收集) |
|
||||
| **无 vendored ``jiangchang_skill_core``** | ``test_platform_import.py`` |
|
||||
| **runtime diagnostics 架构守护** | ``test_runtime_diagnostics_integration.py`` |
|
||||
|
||||
@@ -324,7 +324,7 @@ name: My Skill
|
||||
description: demo
|
||||
metadata:
|
||||
openclaw:
|
||||
slug: demo-skill
|
||||
slug: query-demo-info
|
||||
---
|
||||
"""
|
||||
with open(os.path.join(tmp, "SKILL.md"), "w", encoding="utf-8") as f:
|
||||
|
||||
@@ -141,6 +141,15 @@ class TestDocsStandards(unittest.TestCase):
|
||||
msg="SKILL.md must state README.md is user-facing documentation",
|
||||
)
|
||||
|
||||
def test_naming_md_exists_and_covers_standards(self) -> None:
|
||||
text = self._read("development/NAMING.md")
|
||||
self.assertIn("verb-noun-platform", text)
|
||||
self.assertIn("48", text)
|
||||
self.assertTrue("LEGACY" in text or "历史" in text)
|
||||
self.assertTrue("verbs.txt" in text or "动词白名单" in text)
|
||||
self.assertTrue("platforms.txt" in text or "平台白名单" in text)
|
||||
self.assertTrue("仅适用于新创建" in text or "新技能" in text)
|
||||
|
||||
def test_release_workflow_uses_main_channel(self) -> None:
|
||||
text = self._read(".github/workflows/release_skill.yaml")
|
||||
uses_line = _extract_workflow_uses_line(text)
|
||||
|
||||
87
tests/test_slug_naming.py
Normal file
87
tests/test_slug_naming.py
Normal file
@@ -0,0 +1,87 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""slug 语义校验(verb-noun-platform)。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from util.slug_naming import (
|
||||
LEGACY_EXEMPT_SLUGS,
|
||||
classify_slug_form,
|
||||
parse_slug_segments,
|
||||
validate_slug_semantics,
|
||||
)
|
||||
|
||||
|
||||
class TestSlugNaming(unittest.TestCase):
|
||||
def test_parse_slug_segments(self) -> None:
|
||||
self.assertEqual(parse_slug_segments("download-settlement-report-amazon"), [
|
||||
"download",
|
||||
"settlement",
|
||||
"report",
|
||||
"amazon",
|
||||
])
|
||||
|
||||
def test_classify_standard(self) -> None:
|
||||
segments = parse_slug_segments("download-settlement-report-amazon")
|
||||
self.assertEqual(classify_slug_form(segments, slug="download-settlement-report-amazon"), "standard")
|
||||
|
||||
def test_classify_scope(self) -> None:
|
||||
segments = parse_slug_segments("generate-article-all")
|
||||
self.assertEqual(classify_slug_form(segments, slug="generate-article-all"), "scope")
|
||||
|
||||
def test_classify_legacy_exempt(self) -> None:
|
||||
segments = parse_slug_segments("your-skill-slug")
|
||||
self.assertEqual(classify_slug_form(segments, slug="your-skill-slug"), "legacy_exempt")
|
||||
|
||||
def test_valid_hyphenated_platform_slug(self) -> None:
|
||||
slug = "fill-export-declaration-single-window"
|
||||
segments = parse_slug_segments(slug)
|
||||
self.assertEqual(classify_slug_form(segments, slug=slug), "standard")
|
||||
errors, warnings = validate_slug_semantics(slug)
|
||||
self.assertEqual(errors, [], msg=f"errors={errors!r} warnings={warnings!r}")
|
||||
|
||||
def test_valid_standard_slugs(self) -> None:
|
||||
for slug in (
|
||||
"download-settlement-report-amazon",
|
||||
"add-product-shopee",
|
||||
"generate-article-all",
|
||||
"your-skill-slug",
|
||||
"account-manager",
|
||||
):
|
||||
with self.subTest(slug=slug):
|
||||
errors, warnings = validate_slug_semantics(slug)
|
||||
self.assertEqual(errors, [], msg=f"errors={errors!r} warnings={warnings!r}")
|
||||
|
||||
def test_invalid_slugs(self) -> None:
|
||||
cases = (
|
||||
"amazon-download-settlement-report",
|
||||
"enter-cross-border-sales-receipt-voucher-kingdee",
|
||||
"reconcile-finance",
|
||||
"download-settlement-report-unknownplat",
|
||||
"foobar-settlement-report-amazon",
|
||||
)
|
||||
for slug in cases:
|
||||
with self.subTest(slug=slug):
|
||||
errors, _warnings = validate_slug_semantics(slug)
|
||||
self.assertTrue(errors, msg=f"expected errors for {slug!r}")
|
||||
|
||||
def test_length_boundary_48_passes(self) -> None:
|
||||
slug = "query-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-amazon"
|
||||
self.assertEqual(len(slug), 48)
|
||||
errors, _warnings = validate_slug_semantics(slug)
|
||||
self.assertEqual(errors, [])
|
||||
|
||||
def test_length_boundary_49_fails(self) -> None:
|
||||
slug = "query-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-amazon"
|
||||
self.assertEqual(len(slug), 49)
|
||||
errors, _warnings = validate_slug_semantics(slug)
|
||||
self.assertTrue(errors)
|
||||
self.assertTrue(any("48" in err for err in errors))
|
||||
|
||||
def test_legacy_exempt_set(self) -> None:
|
||||
self.assertIn("your-skill-slug", LEGACY_EXEMPT_SLUGS)
|
||||
self.assertIn("account-manager", LEGACY_EXEMPT_SLUGS)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
336
tests/test_timestamp_columns.py
Normal file
336
tests/test_timestamp_columns.py
Normal file
@@ -0,0 +1,336 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""标准时间字段:Unix 秒级时间戳、默认值、trigger 与元数据。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
import time
|
||||
import unittest
|
||||
|
||||
from _support import IsolatedDataRoot
|
||||
|
||||
|
||||
class TestTimestampColumns(unittest.TestCase):
|
||||
def test_init_db_creates_integer_timestamp_columns_with_defaults(self) -> None:
|
||||
with IsolatedDataRoot():
|
||||
from db.connection import get_conn, init_db
|
||||
from db.display_metadata import list_physical_column_names
|
||||
from db.display_metadata_validator import _table_column_info
|
||||
|
||||
init_db()
|
||||
conn = get_conn()
|
||||
try:
|
||||
cur = conn.cursor()
|
||||
physical = list_physical_column_names(cur, "task_logs")
|
||||
self.assertEqual(physical[-2:], ["created_at", "updated_at"])
|
||||
|
||||
info = _table_column_info(cur, "task_logs")
|
||||
for column_name in ("created_at", "updated_at"):
|
||||
column = info[column_name]
|
||||
self.assertIn("INT", str(column["type"]).upper())
|
||||
self.assertTrue(str(column["dflt_value"]).lower().find("unixepoch") >= 0)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_insert_without_timestamps_auto_generates_unix_seconds(self) -> None:
|
||||
with IsolatedDataRoot():
|
||||
from db.connection import get_conn, init_db
|
||||
|
||||
init_db()
|
||||
conn = get_conn()
|
||||
try:
|
||||
cur = conn.cursor()
|
||||
before = int(time.time())
|
||||
cur.execute(
|
||||
"INSERT INTO task_logs (task_type, status) VALUES ('auto-ts', 'success')"
|
||||
)
|
||||
conn.commit()
|
||||
cur.execute(
|
||||
"SELECT created_at, updated_at FROM task_logs ORDER BY id DESC LIMIT 1"
|
||||
)
|
||||
created_at, updated_at = cur.fetchone()
|
||||
after = int(time.time())
|
||||
self.assertIsInstance(created_at, int)
|
||||
self.assertIsInstance(updated_at, int)
|
||||
self.assertGreaterEqual(created_at, before)
|
||||
self.assertLessEqual(created_at, after)
|
||||
self.assertEqual(created_at, updated_at)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_update_business_field_keeps_created_at_and_refreshes_updated_at(self) -> None:
|
||||
with IsolatedDataRoot():
|
||||
from db.connection import get_conn, init_db
|
||||
|
||||
init_db()
|
||||
conn = get_conn()
|
||||
try:
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO task_logs (task_type, status, created_at, updated_at)
|
||||
VALUES ('update-ts', 'success', 1000, 1000)
|
||||
"""
|
||||
)
|
||||
log_id = int(cur.lastrowid)
|
||||
conn.commit()
|
||||
|
||||
cur.execute("UPDATE task_logs SET status = 'failed' WHERE id = ?", (log_id,))
|
||||
conn.commit()
|
||||
|
||||
cur.execute(
|
||||
"SELECT created_at, updated_at FROM task_logs WHERE id = ?",
|
||||
(log_id,),
|
||||
)
|
||||
created_at, updated_at = cur.fetchone()
|
||||
self.assertEqual(created_at, 1000)
|
||||
self.assertGreater(updated_at, 1000)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_reinit_does_not_rewrite_existing_timestamps(self) -> None:
|
||||
with IsolatedDataRoot():
|
||||
from db.connection import get_conn, init_db
|
||||
|
||||
init_db()
|
||||
conn = get_conn()
|
||||
try:
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO task_logs (task_type, status, created_at, updated_at)
|
||||
VALUES ('stable-ts', 'success', 1234567890, 1234567891)
|
||||
"""
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
init_db()
|
||||
conn = get_conn()
|
||||
try:
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"SELECT created_at, updated_at FROM task_logs WHERE task_type = 'stable-ts'"
|
||||
)
|
||||
self.assertEqual(cur.fetchone(), (1234567890, 1234567891))
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_timestamp_metadata_contract(self) -> None:
|
||||
with IsolatedDataRoot():
|
||||
from db.connection import get_conn, init_db
|
||||
from db.display_metadata import DATETIME_UNIX_SECONDS
|
||||
|
||||
init_db()
|
||||
conn = get_conn()
|
||||
try:
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT column_name, display_name, display_type, editable, display_order
|
||||
FROM _jiangchang_columns
|
||||
WHERE table_name = 'task_logs'
|
||||
AND column_name IN ('created_at', 'updated_at')
|
||||
"""
|
||||
)
|
||||
by_name = {
|
||||
name: (display_name, display_type, editable, display_order)
|
||||
for name, display_name, display_type, editable, display_order in cur.fetchall()
|
||||
}
|
||||
self.assertEqual(by_name["created_at"][0], "创建时间")
|
||||
self.assertEqual(by_name["updated_at"][0], "更新时间")
|
||||
self.assertEqual(by_name["created_at"][1], DATETIME_UNIX_SECONDS)
|
||||
self.assertEqual(by_name["updated_at"][1], DATETIME_UNIX_SECONDS)
|
||||
self.assertEqual(int(by_name["created_at"][2]), 0)
|
||||
self.assertEqual(int(by_name["updated_at"][2]), 0)
|
||||
self.assertIsNone(by_name["created_at"][3])
|
||||
self.assertIsNone(by_name["updated_at"][3])
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_metadata_upsert_keeps_timestamp_display_type(self) -> None:
|
||||
with IsolatedDataRoot():
|
||||
from db.connection import get_conn, init_db
|
||||
from db.display_metadata import DATETIME_UNIX_SECONDS
|
||||
|
||||
init_db()
|
||||
init_db()
|
||||
conn = get_conn()
|
||||
try:
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT display_type FROM _jiangchang_columns
|
||||
WHERE table_name = 'task_logs' AND column_name = 'created_at'
|
||||
"""
|
||||
)
|
||||
self.assertEqual(cur.fetchone()[0], DATETIME_UNIX_SECONDS)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_repository_insert_uses_database_timestamps(self) -> None:
|
||||
with IsolatedDataRoot():
|
||||
from db import task_logs_repository as tlr
|
||||
from db.connection import get_conn, init_db
|
||||
|
||||
init_db()
|
||||
before = int(time.time())
|
||||
new_id = tlr.save_task_log(task_type="repo-ts", status="success")
|
||||
after = int(time.time())
|
||||
|
||||
conn = get_conn()
|
||||
try:
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"SELECT created_at, updated_at FROM task_logs WHERE id = ?",
|
||||
(new_id,),
|
||||
)
|
||||
created_at, updated_at = cur.fetchone()
|
||||
self.assertGreaterEqual(int(created_at), before)
|
||||
self.assertLessEqual(int(created_at), after)
|
||||
self.assertEqual(created_at, updated_at)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_validator_rejects_missing_display_type(self) -> None:
|
||||
with IsolatedDataRoot():
|
||||
from db.connection import get_conn, init_db
|
||||
from db.display_metadata_validator import validate_template_database
|
||||
|
||||
init_db()
|
||||
conn = get_conn()
|
||||
try:
|
||||
conn.execute(
|
||||
"UPDATE _jiangchang_columns SET display_type = NULL WHERE table_name = 'task_logs' AND column_name = 'created_at'"
|
||||
)
|
||||
conn.commit()
|
||||
report = validate_template_database(conn)
|
||||
self.assertFalse(report.ok)
|
||||
self.assertTrue(any("display_type" in err for err in report.errors))
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_validator_rejects_wrong_timestamp_type(self) -> None:
|
||||
conn = sqlite3.connect(":memory:")
|
||||
try:
|
||||
from db.display_metadata import init_display_metadata
|
||||
from db.display_metadata_validator import validate_display_metadata
|
||||
|
||||
conn.executescript(
|
||||
"""
|
||||
CREATE TABLE demo_items (
|
||||
id INTEGER PRIMARY KEY,
|
||||
title TEXT,
|
||||
created_at TEXT
|
||||
);
|
||||
"""
|
||||
)
|
||||
init_display_metadata(conn.cursor())
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO _jiangchang_tables (table_name, display_name, visible, readonly)
|
||||
VALUES ('demo_items', '演示', 1, 0)
|
||||
"""
|
||||
)
|
||||
for column_name, display_name in (
|
||||
("id", "编号"),
|
||||
("title", "标题"),
|
||||
("created_at", "创建时间"),
|
||||
):
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO _jiangchang_columns
|
||||
(table_name, column_name, display_name, visible, searchable, editable, display_type)
|
||||
VALUES ('demo_items', ?, ?, 1, 0, 0, 'datetime_unix_seconds')
|
||||
""",
|
||||
(column_name, display_name),
|
||||
)
|
||||
conn.commit()
|
||||
report = validate_display_metadata(conn)
|
||||
self.assertFalse(report.ok)
|
||||
self.assertTrue(any("created_at 字段类型应为 INTEGER" in err for err in report.errors))
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_validator_rejects_editable_timestamp(self) -> None:
|
||||
with IsolatedDataRoot():
|
||||
from db.connection import get_conn, init_db
|
||||
from db.display_metadata_validator import validate_template_database
|
||||
|
||||
init_db()
|
||||
conn = get_conn()
|
||||
try:
|
||||
conn.execute(
|
||||
"UPDATE _jiangchang_columns SET editable = 1 WHERE table_name = 'task_logs' AND column_name = 'updated_at'"
|
||||
)
|
||||
conn.commit()
|
||||
report = validate_template_database(conn)
|
||||
self.assertFalse(report.ok)
|
||||
self.assertTrue(any("editable 应为 0" in err for err in report.errors))
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_validator_does_not_flag_amount_as_timestamp(self) -> None:
|
||||
conn = sqlite3.connect(":memory:")
|
||||
try:
|
||||
from db.display_metadata import init_display_metadata
|
||||
from db.display_metadata_validator import validate_display_metadata
|
||||
|
||||
conn.executescript(
|
||||
"""
|
||||
CREATE TABLE demo_items (
|
||||
id INTEGER PRIMARY KEY,
|
||||
amount INTEGER,
|
||||
count INTEGER
|
||||
);
|
||||
"""
|
||||
)
|
||||
init_display_metadata(conn.cursor())
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO _jiangchang_tables (table_name, display_name, visible, readonly)
|
||||
VALUES ('demo_items', '演示', 1, 0)
|
||||
"""
|
||||
)
|
||||
for column_name, display_name in (
|
||||
("id", "编号"),
|
||||
("amount", "数量"),
|
||||
("count", "计数"),
|
||||
):
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO _jiangchang_columns
|
||||
(table_name, column_name, display_name, visible, searchable, editable)
|
||||
VALUES ('demo_items', ?, ?, 1, 1, 1)
|
||||
""",
|
||||
(column_name, display_name),
|
||||
)
|
||||
conn.commit()
|
||||
report = validate_display_metadata(conn)
|
||||
self.assertFalse(any("amount" in err and "display_type" in err for err in report.errors))
|
||||
self.assertFalse(any("count" in err and "display_type" in err for err in report.errors))
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_trigger_creation_is_idempotent(self) -> None:
|
||||
with IsolatedDataRoot():
|
||||
from db.connection import get_conn, init_db
|
||||
from db.timestamp_columns import TASK_LOGS_UPDATED_AT_TRIGGER, has_updated_at_trigger
|
||||
|
||||
init_db()
|
||||
init_db()
|
||||
conn = get_conn()
|
||||
try:
|
||||
self.assertTrue(has_updated_at_trigger(conn.cursor(), "task_logs"))
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'trigger' AND name = ?",
|
||||
(TASK_LOGS_UPDATED_AT_TRIGGER,),
|
||||
)
|
||||
self.assertEqual(int(cur.fetchone()[0]), 1)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user