Files
wordpress-theme-aksrt/tests/admin/baidu-push-settings-reference-contract.test.cjs
T
kate522 cf39e72126 feat(extensions): align Baidu push with reference theme
与参考主题(zibll)1:1 对齐百度推送,并清掉存量双路与死代码:

- 新增普通/快速收录通道开关,快速收录走 &type=daily;自动推送 JS 与参考实现一致

- 分类法 saved_term 推送(可推送类型推导公开分类法,排除 post_format,aksrt_baidu_push_taxonomies 可过滤)

- 推送结果写入 meta 去重,成功后不重复提交;编辑页 Metabox 展示结果并提供「重新提交」(nonce 校验后强制推送)

- 配置面收敛到扩展页单一 canonical 键:删除 SEO 页 seo_baidu_* 重复面与两处合并逻辑,不写迁移兜底

- 清理死代码:push_batch(零调用)及 batch_size、从不写入的 baidu_push_verified、零消费的熊掌号 xzh_* 七键;baidu_push_log_enabled 接入 log()

新增 PHP harness 驱动真实逻辑(通道/去重/重试/重新提交/术语/面板渲染)+ 参考主题契约测试;tests/admin 与 tests/widget-system 全量通过。
2026-10-13 21:01:03 +08:00

204 lines
9.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
/**
* 「百度推送」后台设置页契约(对齐参考主题 zibll):
* 1) PHP schema(SchemaExtensionsPush.php)与 JS schema(push.js)字段键、顺序、默认值一致;
* 2) 参考主题字段全集存在:自动推送 JS、普通收录、快速收录、准入 Token、发布/更新触发;
* 3) 双路/迁移字段(SEO 页 seo_baidu_*、熊掌号 xzh_*、批量推送 batch_*)已彻底移除,无双路兼容;
* 4) 运行时(SearchPush + ExtensionRuntime)只读取 canonical 键,快速收录走 type=daily;
* 5) 分类法 saved_term 推送、meta 去重、「重新提交」nonce 与编辑页面板均已接入,
* 并由 PHP harness 驱动真实逻辑验证。
*/
const assert = require('node:assert/strict');
const { execFileSync } = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');
const test = require('node:test');
const root = path.resolve(__dirname, '..', '..');
const php = fs.readFileSync(path.join(root, 'src/Admin/Schema/SchemaExtensionsPush.php'), 'utf8');
const js = fs.readFileSync(path.join(root, 'assets/admin/src/schema/sections/extensions/push.js'), 'utf8');
const runtime = fs.readFileSync(path.join(root, 'src/Extensions/SearchPush.php'), 'utf8');
const extensionRuntime = fs.readFileSync(path.join(root, 'src/Settings/ExtensionRuntime.php'), 'utf8');
const globalPhp = fs.readFileSync(path.join(root, 'src/Admin/Schema/GlobalSchema.php'), 'utf8');
const seoJs = fs.readFileSync(path.join(root, 'assets/admin/src/schema/sections/global/seo.js'), 'utf8');
const displayGroups = fs.readFileSync(path.join(root, 'src/Settings/DisplayPageGroups.php'), 'utf8');
const metaBox = fs.readFileSync(path.join(root, 'src/Extensions/SearchPushMetaBox.php'), 'utf8');
const functions = fs.readFileSync(path.join(root, 'functions.php'), 'utf8');
// 参考主题(zibll)百度推送字段全集,AKSRT 单一 canonical 配置面(扩展&增强 → 百度推送)。
const REFERENCE_KEYS = [
'baidu_push_enabled',
'baidu_push_token',
'baidu_push_auto',
'baidu_push_on_publish',
'baidu_push_on_update',
'baidu_push_normal',
'baidu_push_fast',
'baidu_push_log_enabled',
];
// 双路 / 迁移 / 死代码字段:SEO 页 seo_baidu_*、熊掌号 xzh_*、批量推送与校验占位。
const DUAL_KEYS = [
'seo_baidu_push',
'seo_baidu_normal',
'seo_baidu_fast',
'seo_baidu_token',
'xzh_on',
'xzh_appid',
'xzh_render_tail',
'xzh_jsonld_single',
'xzh_jsonld_page',
'xzh_jsonld_img',
'baidu_push_batch_size',
'baidu_push_verified',
'push_batch',
'batch_size',
];
function phpFieldKeys(src) {
const found = [];
const re = /'key'\s*=>\s*'([a-z0-9_]+)',\s*'label'/g;
let m;
while ((m = re.exec(src)) !== null) found.push(m[1]);
return found;
}
function jsFieldKeys(src) {
const found = [];
const re = /key:\s*'([a-z0-9_]+)',\s*label:/g;
let m;
while ((m = re.exec(src)) !== null) found.push(m[1]);
return found;
}
test('PHP push schema 字段键与顺序对齐参考主题', () => {
assert.deepEqual(phpFieldKeys(php), REFERENCE_KEYS);
});
test('JS push schema 字段键与顺序对齐参考主题', () => {
assert.deepEqual(jsFieldKeys(js), REFERENCE_KEYS);
});
test('PHP schema 默认值对齐', () => {
assert.match(php, /'baidu_push_enabled',\s*'label'\s*=>\s*'启用百度推送',\s*'type'\s*=>\s*'switch',\s*'default'\s*=>\s*false/);
assert.match(php, /'baidu_push_token',\s*'label'\s*=>\s*'推送 Token',\s*'type'\s*=>\s*'input',\s*'default'\s*=>\s*''/);
assert.match(php, /'baidu_push_auto',\s*'label'\s*=>\s*'自动推送',\s*'type'\s*=>\s*'switch',\s*'default'\s*=>\s*true/);
assert.match(php, /'baidu_push_on_publish',\s*'label'\s*=>\s*'发布时主动推送',\s*'type'\s*=>\s*'switch',\s*'default'\s*=>\s*true/);
assert.match(php, /'baidu_push_on_update',\s*'label'\s*=>\s*'更新时推送',\s*'type'\s*=>\s*'switch',\s*'default'\s*=>\s*false/);
assert.match(php, /'baidu_push_normal',\s*'label'\s*=>\s*'普通收录',\s*'type'\s*=>\s*'switch',\s*'default'\s*=>\s*true/);
assert.match(php, /'baidu_push_fast',\s*'label'\s*=>\s*'快速收录',\s*'type'\s*=>\s*'switch',\s*'default'\s*=>\s*false/);
assert.match(php, /'baidu_push_log_enabled',\s*'label'\s*=>\s*'记录推送日志',\s*'type'\s*=>\s*'switch',\s*'default'\s*=>\s*true/);
});
test('JS schema 默认值对齐', () => {
assert.match(js, /baidu_push_enabled:\s*false/);
assert.match(js, /baidu_push_token:\s*''/);
assert.match(js, /baidu_push_auto:\s*true/);
assert.match(js, /baidu_push_on_publish:\s*true/);
assert.match(js, /baidu_push_on_update:\s*false/);
assert.match(js, /baidu_push_normal:\s*true/);
assert.match(js, /baidu_push_fast:\s*false/);
assert.match(js, /baidu_push_log_enabled:\s*true/);
});
test('双路/迁移/死代码字段在 schema、运行时与展示层均已移除(无双路)', () => {
for (const legacy of DUAL_KEYS) {
assert.ok(!php.includes(legacy), `SchemaExtensionsPush 残留: ${legacy}`);
assert.ok(!js.includes(legacy), `push.js 残留: ${legacy}`);
assert.ok(!runtime.includes(legacy), `SearchPush 残留: ${legacy}`);
assert.ok(!extensionRuntime.includes(legacy), `ExtensionRuntime 残留: ${legacy}`);
assert.ok(!globalPhp.includes(legacy), `GlobalSchema 残留: ${legacy}`);
assert.ok(!seoJs.includes(legacy), `seo.js 残留: ${legacy}`);
assert.ok(!displayGroups.includes(legacy), `DisplayPageGroups 残留: ${legacy}`);
assert.ok(!metaBox.includes(legacy), `SearchPushMetaBox 残留: ${legacy}`);
assert.ok(!functions.includes(legacy), `functions.php 残留: ${legacy}`);
}
assert.ok(!globalPhp.includes("'baidu'"), 'GlobalSchema SEO 分组残留百度重复配置面');
assert.ok(!seoJs.includes("key: 'baidu'"), 'seo.js 残留百度重复配置面');
});
test('普通/快速收录通道接入百度链接提交接口', () => {
assert.match(runtime, /data\.zz\.baidu\.com\/urls\?site=/);
assert.match(runtime, /'baidu_push_normal'/);
assert.match(runtime, /'baidu_push_fast'/);
assert.match(runtime, /&type=daily/);
assert.match(extensionRuntime, /'baidu_push_normal'\s*=>/);
assert.match(extensionRuntime, /'baidu_push_fast'\s*=>/);
});
test('前台自动推送 JS 与参考主题一致', () => {
assert.match(runtime, /zz\.bdstatic\.com\/linksubmit\/push\.js/);
assert.match(runtime, /push\.zhanzhang\.baidu\.com\/push\.js/);
assert.match(runtime, /add_action\('wp_footer'/);
});
test('发布/更新触发沿用 transition_post_status 且仅限可检索内容类型', () => {
assert.match(runtime, /transition_post_status/);
assert.match(runtime, /aksrt_searchable_post_types/);
assert.match(runtime, /wp_is_post_revision/);
assert.match(runtime, /wp_is_post_autosave/);
});
test('推送日志开关接入运行时', () => {
assert.match(runtime, /baidu_push_log_enabled/);
assert.match(extensionRuntime, /'baidu_push_log_enabled'/);
});
test('服务状态契约保留扩展总览消费的键', () => {
for (const key of ['enabled', 'provider', 'configured', 'available', 'verified', 'transport', 'missing_fields']) {
assert.ok(runtime.includes(`'${key}'`), `get_status 缺少 ${key}`);
}
});
test('分类法推送接入 saved_term 且排除文章形式', () => {
assert.match(runtime, /add_action\('saved_term'/);
assert.match(runtime, /public static function pushable_taxonomies/);
assert.match(runtime, /array_diff\(\(array\) \$taxonomies, \['post_format', 'nav_menu'\]\)/);
assert.match(runtime, /aksrt_baidu_push_taxonomies/);
assert.match(runtime, /get_term_link\(\$term_id, \$taxonomy\)/);
});
test('推送结果写入 meta 并按记录去重', () => {
assert.match(runtime, /META_KEY\s*=\s*'_aksrt_baidu_push_back'/);
assert.match(runtime, /private function record_is_complete/);
assert.match(runtime, /private function build_record/);
assert.match(runtime, /'normal_push'/);
assert.match(runtime, /'daily_push'/);
assert.match(runtime, /'update_time'/);
assert.match(runtime, /update_term_meta\(/);
assert.match(runtime, /delete_term_meta\(/);
});
test('「重新提交」勾选需通过 metabox nonce 校验', () => {
assert.match(runtime, /NONCE_ACTION\s*=\s*'aksrt_push_meta_save'/);
assert.match(runtime, /NONCE_FIELD\s*=\s*'aksrt_push_meta_nonce'/);
assert.match(runtime, /RESUBMIT_FIELD\s*=\s*'aksrt_push_resubmit'/);
assert.match(runtime, /wp_verify_nonce\(\$nonce, self::NONCE_ACTION\)/);
});
test('编辑页面板与参考主题「百度资源提交」Metabox 对齐', () => {
assert.match(metaBox, /add_meta_box\(\s*'aksrt_baidu_push',\s*__\('百度资源提交', 'aksrt'\)/);
assert.match(metaBox, /'side',\s*'high'/);
assert.match(metaBox, /\{\$taxonomy\}_edit_form_fields/);
assert.match(metaBox, /\{\$taxonomy\}_add_form_fields/);
assert.match(metaBox, /普通收录:成功/);
assert.match(metaBox, /快速收录:失败/);
assert.match(metaBox, /更新时间:/);
assert.match(metaBox, /重新提交/);
assert.match(metaBox, /发布、更新后刷新页面后可查看提交结果/);
assert.match(metaBox, /wp_nonce_field\(SearchPush::NONCE_ACTION, SearchPush::NONCE_FIELD\)/);
assert.match(metaBox, /esc_html\(\$line\[1\]\)/, '接口结果需转义后输出');
});
test('functions.php 接线推送面板', () => {
assert.match(functions, /SearchPushMetaBox::get_instance\(\)->init\(\)/);
});
test('百度推送行为 harness 通过', () => {
const harness = path.join(root, 'tests', 'admin', 'baidu-push-harness.php');
const output = execFileSync('php', [harness], { encoding: 'utf8' });
assert.match(output, /Baidu push harness passed/);
});