|
|
@@ -0,0 +1,278 @@
|
|
|
+/**
|
|
|
+ * 契约级单测(TDD-RED→GREEN)——能力键 article.detail.videoFullscreen(前端部分)
|
|
|
+ *
|
|
|
+ * 来源:
|
|
|
+ * requirement: docs/tdd/orchestration/req-article-single-video-fullscreen-2026-12-01.md
|
|
|
+ * (§1 需求 R1-R5 / §2 设计 D1-D7 / §3 契约 C1-C4 / §4 验收)
|
|
|
+ * design: §2 设计 D1-D6(data 新增 singleVideoSrc/videoFullscreenFired;methods 新增
|
|
|
+ * isVideoOnlyContent/extractVideoSrc/onSingleVideoReady;loadDetail 接入;模板 video+mp-html 条件渲染;样式)
|
|
|
+ * 契约布局:本文档(页面方法/模板结构 + 行为断言)
|
|
|
+ *
|
|
|
+ * 说明(既有 Jest 体系约束):
|
|
|
+ * cfc-frontend 无 vue-jest/jsdom 组件环境 —— 沿用「读取真实 SFC 源码 → 桩化 import → 求值
|
|
|
+ * <script> 拿真实 export default → 驱动 data()/methods/computed 做行为断言,模板做结构断言」,
|
|
|
+ * 参考 __tests__/pages/article-center/articleSearch.contract.test.js。
|
|
|
+ * 本文档不修改 pages/、utils/ 任何业务代码。
|
|
|
+ * 小程序约束(AGENTS.md):禁可选链 ?.、禁 CSS Grid、禁 :key 表达式、禁 new Date(string)。
|
|
|
+ */
|
|
|
+
|
|
|
+import fs from 'fs'
|
|
|
+import path from 'path'
|
|
|
+import { BrandColors } from '@/utils/brand-colors.js'
|
|
|
+
|
|
|
+const PAGE_PATH = path.join(__dirname, '../../../pages/article-center/article-detail.vue')
|
|
|
+
|
|
|
+/** 把 ES import 语句替换为从 globalThis.__articleDetailVideoStubs 取值的声明(不解析模块,避免污染业务实现) */
|
|
|
+function stubImports(script, stubMap) {
|
|
|
+ return script.replace(/import\s+([^;]+?)\s+from\s+['"]([^'"]+)['"];?/g, function (m, clause, id) {
|
|
|
+ var names = []
|
|
|
+ var c = clause.trim()
|
|
|
+ var braceIdx = c.indexOf('{')
|
|
|
+ if (braceIdx >= 0) {
|
|
|
+ var def = c.slice(0, braceIdx).replace(/,\s*$/, '').trim()
|
|
|
+ if (def) names.push(def)
|
|
|
+ var named = c.slice(braceIdx + 1, c.lastIndexOf('}'))
|
|
|
+ named.split(',').forEach(function (part) {
|
|
|
+ var p = part.trim()
|
|
|
+ if (!p) return
|
|
|
+ var parts = p.split(/\s+as\s+/)
|
|
|
+ var local = parts.length > 1 ? parts[1].trim() : parts[0].trim()
|
|
|
+ if (local) names.push(local)
|
|
|
+ })
|
|
|
+ } else if (c.indexOf('*') === 0) {
|
|
|
+ var nsMatch = c.match(/\*\s+as\s+(\w+)/)
|
|
|
+ if (nsMatch) names.push(nsMatch[1])
|
|
|
+ } else {
|
|
|
+ names.push(c)
|
|
|
+ }
|
|
|
+ return names.map(function (n) {
|
|
|
+ return 'var ' + n + ' = ((globalThis.__articleDetailVideoStubs || {})[' + JSON.stringify(id) + '] || {})[' + JSON.stringify(n) + '];'
|
|
|
+ }).join('\n')
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+/** 加载真实 SFC:返回 { component, template }(component = 真实 <script> 的 export default 对象) */
|
|
|
+function loadPage() {
|
|
|
+ var src = fs.readFileSync(PAGE_PATH, 'utf8')
|
|
|
+ var tmplMatch = src.match(/<template>([\s\S]*?)<\/template>/)
|
|
|
+ var scriptMatch = src.match(/<script>([\s\S]*?)<\/script>/)
|
|
|
+ if (!scriptMatch) throw new Error('无法在 article-detail.vue 中定位 <script> 块')
|
|
|
+ var script = stubImports(scriptMatch[1], globalThis.__articleDetailVideoStubs || {})
|
|
|
+ script = script.replace(/export default\s*\{/, 'module.exports = {')
|
|
|
+ var mod = { exports: {} }
|
|
|
+ // eslint-disable-next-line no-new-func
|
|
|
+ var fn = new Function('module', 'exports', script)
|
|
|
+ fn(mod, mod.exports)
|
|
|
+ return { component: mod.exports, template: tmplMatch ? tmplMatch[1] : '' }
|
|
|
+}
|
|
|
+
|
|
|
+/** 依据真实组件的 data()/methods/computed(含 mixin)构造可驱动上下文的实例 */
|
|
|
+function instantiate(component) {
|
|
|
+ var vm = {}
|
|
|
+ // 合并 mixin 的 data/methods/computed(模拟 Vue mixin 合并),保证 loadDetail 等能取到 mixin 方法
|
|
|
+ var mixins = component.mixins || []
|
|
|
+ var mergedData = {}
|
|
|
+ var mergedMethods = {}
|
|
|
+ var mergedComputed = {}
|
|
|
+ mixins.forEach(function (m) {
|
|
|
+ if (!m) return
|
|
|
+ if (typeof m.data === 'function') {
|
|
|
+ var md = m.data.call(vm)
|
|
|
+ Object.keys(md).forEach(function (k) { mergedData[k] = md[k] })
|
|
|
+ }
|
|
|
+ if (m.methods) Object.keys(m.methods).forEach(function (k) { mergedMethods[k] = m.methods[k] })
|
|
|
+ if (m.computed) Object.keys(m.computed).forEach(function (k) { mergedComputed[k] = m.computed[k] })
|
|
|
+ })
|
|
|
+ var data = typeof component.data === 'function' ? component.data.call(vm) : (component.data || {})
|
|
|
+ Object.keys(mergedData).forEach(function (k) { vm[k] = mergedData[k] })
|
|
|
+ Object.keys(data).forEach(function (k) { vm[k] = data[k] })
|
|
|
+ var methods = component.methods || {}
|
|
|
+ Object.keys(mergedMethods).forEach(function (k) { vm[k] = mergedMethods[k] })
|
|
|
+ Object.keys(methods).forEach(function (k) { vm[k] = methods[k] })
|
|
|
+ var computed = component.computed || {}
|
|
|
+ var allComputed = Object.assign({}, mergedComputed, computed)
|
|
|
+ Object.keys(allComputed).forEach(function (k) {
|
|
|
+ Object.defineProperty(vm, k, { get: allComputed[k], enumerable: true, configurable: true })
|
|
|
+ })
|
|
|
+ return vm
|
|
|
+}
|
|
|
+
|
|
|
+/** 每次调用时重新加载真实 SFC(读取当时的 __articleDetailVideoStubs),避免闭包捕获旧值 */
|
|
|
+function getComponent() {
|
|
|
+ return loadPage().component
|
|
|
+}
|
|
|
+
|
|
|
+/** 刷新微任务队列:让 fire-and-forget 的 async 方法(loadDetail 等)结算完成 */
|
|
|
+function flushPromises() {
|
|
|
+ return new Promise(function (resolve) { setTimeout(resolve, 0) })
|
|
|
+}
|
|
|
+
|
|
|
+// ---------------------------------------------------------------------------
|
|
|
+// 测试夹具
|
|
|
+// ---------------------------------------------------------------------------
|
|
|
+
|
|
|
+var apiStubs
|
|
|
+
|
|
|
+beforeEach(function () {
|
|
|
+ global.uni._storage = {}
|
|
|
+ global.uni.showToast.mockReset()
|
|
|
+ global.uni.navigateTo.mockReset()
|
|
|
+ global.uni.request.mockReset()
|
|
|
+ // video context 桩:onSingleVideoReady 通过 uni.createVideoContext(id, this) 拿上下文进全屏
|
|
|
+ global.uni.createVideoContext = jest.fn(function () {
|
|
|
+ return { requestFullScreen: jest.fn() }
|
|
|
+ })
|
|
|
+ apiStubs = {
|
|
|
+ getArticleDetail: jest.fn(function () { return Promise.resolve({ code: 200, message: 'ok', data: null }) }),
|
|
|
+ getArticleAudio: jest.fn(function () { return Promise.resolve({ code: 200, message: 'ok', data: { audioUrls: [] } }) }),
|
|
|
+ reportReadingTime: jest.fn(function () { return Promise.resolve({ code: 200, message: 'ok', data: null }) }),
|
|
|
+ getShareQrCode: jest.fn(function () { return Promise.resolve({ code: 200, message: 'ok', data: null }) }),
|
|
|
+ completeArticleRead: jest.fn(function () { return Promise.resolve({ code: 200, message: 'ok', data: null }) }),
|
|
|
+ generateQuiz: jest.fn(function () { return Promise.resolve({ code: 200, message: 'ok', data: null }) }),
|
|
|
+ submitQuiz: jest.fn(function () { return Promise.resolve({ code: 200, message: 'ok', data: null }) }),
|
|
|
+ completeTask: jest.fn(function () { return Promise.resolve({ code: 200, message: 'ok', data: null }) })
|
|
|
+ }
|
|
|
+ globalThis.__articleDetailVideoStubs = {
|
|
|
+ '../../utils/brand-colors.js': { BrandColors: BrandColors },
|
|
|
+ '@/utils/api.js': apiStubs,
|
|
|
+ '@/config.js': { config: { API_BASE_URL: 'http://127.0.0.1:9082' } },
|
|
|
+ '../../components/share-mixin.js': { shareMixin: { data: function () { return {} }, methods: { setShareInfo: jest.fn() } } },
|
|
|
+ '@/components/ContentSharePoster.vue': {},
|
|
|
+ '@/components/RelatedContent.vue': {},
|
|
|
+ '@/components/mp-html/mp-html.vue': {},
|
|
|
+ '@/utils/fontScale.js': { getFontScale: function () { return 1 } }
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
+afterEach(function () {
|
|
|
+ delete globalThis.__articleDetailVideoStubs
|
|
|
+})
|
|
|
+
|
|
|
+// ---------------------------------------------------------------------------
|
|
|
+// 契约 C1:isVideoOnlyContent 判定(仅一条视频、无实质文本/图片/其他媒体/链接,容忍编辑器尾部空段)
|
|
|
+// ---------------------------------------------------------------------------
|
|
|
+
|
|
|
+describe('契约C1 article-detail.vue:isVideoOnlyContent 判定', function () {
|
|
|
+ test('C1.1 仅单视频 src → true', function () {
|
|
|
+ var vm = instantiate(getComponent())
|
|
|
+ expect(vm.isVideoOnlyContent('<video src="http://x/a.mp4"></video>')).toBe(true)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('C1.2 单视频 + 尾部空段(<p><br></p>)→ true(容忍编辑器尾部空段)', function () {
|
|
|
+ var vm = instantiate(getComponent())
|
|
|
+ expect(vm.isVideoOnlyContent('<p><video src="http://x/a.mp4"></video></p><p><br></p>')).toBe(true)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('C1.3 单 video 内嵌 source → true', function () {
|
|
|
+ var vm = instantiate(getComponent())
|
|
|
+ expect(vm.isVideoOnlyContent('<video src="http://x/a.mp4"><source src="http://x/b.mp4"></video>')).toBe(true)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('C1.4 两条 video → false', function () {
|
|
|
+ var vm = instantiate(getComponent())
|
|
|
+ expect(vm.isVideoOnlyContent('<video src="http://x/a.mp4"></video><video src="http://x/b.mp4"></video>')).toBe(false)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('C1.5 一条 video + 正文文本 → false', function () {
|
|
|
+ var vm = instantiate(getComponent())
|
|
|
+ expect(vm.isVideoOnlyContent('<video src="http://x/a.mp4"></video><p>正文文字</p>')).toBe(false)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('C1.6 一条 video + 图片 → false', function () {
|
|
|
+ var vm = instantiate(getComponent())
|
|
|
+ expect(vm.isVideoOnlyContent('<video src="http://x/a.mp4"></video><img src="http://x/i.png">')).toBe(false)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('C1.7 空值 / 非字符串 → false', function () {
|
|
|
+ var vm = instantiate(getComponent())
|
|
|
+ expect(vm.isVideoOnlyContent('')).toBe(false)
|
|
|
+ expect(vm.isVideoOnlyContent(null)).toBe(false)
|
|
|
+ expect(vm.isVideoOnlyContent(undefined)).toBe(false)
|
|
|
+ expect(vm.isVideoOnlyContent(123)).toBe(false)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('C1.8 纯文本段落(无视频)→ false', function () {
|
|
|
+ var vm = instantiate(getComponent())
|
|
|
+ expect(vm.isVideoOnlyContent('<p>纯文本</p>')).toBe(false)
|
|
|
+ })
|
|
|
+})
|
|
|
+
|
|
|
+// ---------------------------------------------------------------------------
|
|
|
+// 契约 C2:extractVideoSrc(从唯一 video 解析出播放地址)
|
|
|
+// ---------------------------------------------------------------------------
|
|
|
+
|
|
|
+describe('契约C2 article-detail.vue:extractVideoSrc 解析', function () {
|
|
|
+ test('C2.1 video 自带 src → 返回 src', function () {
|
|
|
+ var vm = instantiate(getComponent())
|
|
|
+ expect(vm.extractVideoSrc('<video src="http://x/a.mp4"></video>')).toBe('http://x/a.mp4')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('C2.2 video 内嵌 source src → 返回 source src', function () {
|
|
|
+ var vm = instantiate(getComponent())
|
|
|
+ expect(vm.extractVideoSrc('<video><source src="http://x/b.mp4"></video>')).toBe('http://x/b.mp4')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('C2.3 video 仅 poster 无 src → 返回空串', function () {
|
|
|
+ var vm = instantiate(getComponent())
|
|
|
+ expect(vm.extractVideoSrc('<video poster="p.png"></video>')).toBe('')
|
|
|
+ })
|
|
|
+})
|
|
|
+
|
|
|
+// ---------------------------------------------------------------------------
|
|
|
+// 契约 C3:行为(loadDetail 接入 + onSingleVideoReady 全屏守卫)
|
|
|
+// ---------------------------------------------------------------------------
|
|
|
+
|
|
|
+describe('契约C3 article-detail.vue:单视频默认全屏行为', function () {
|
|
|
+ test('C3.1 单视频文章 loadDetail 后启用 singleVideoSrc,模板含 video 与 mp-html', async function () {
|
|
|
+ apiStubs.getArticleDetail.mockResolvedValue({ code: 200, message: 'ok', data: { id: 1, title: 'T', coverImage: '', content: '<video src="http://x/a.mp4"></video>', publishedAt: '2026-01-01T00:00:00', viewCount: 0, readTime: 3 } })
|
|
|
+ var vm = instantiate(getComponent())
|
|
|
+ await vm.loadDetail(1)
|
|
|
+ expect(vm.singleVideoSrc).toBe('http://x/a.mp4')
|
|
|
+ var template = loadPage().template
|
|
|
+ expect(template).toMatch(/id="article-single-video"/)
|
|
|
+ expect(template).toMatch(/mp-html/)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('C3.2 非单视频文章(正文+video)loadDetail 后 singleVideoSrc 为空(未启用)', async function () {
|
|
|
+ apiStubs.getArticleDetail.mockResolvedValue({ code: 200, message: 'ok', data: { id: 1, title: 'T', coverImage: '', content: '<p>正文</p><video src="http://x/a.mp4"></video>', publishedAt: '2026-01-01T00:00:00', viewCount: 0, readTime: 3 } })
|
|
|
+ var vm = instantiate(getComponent())
|
|
|
+ await vm.loadDetail(1)
|
|
|
+ expect(vm.singleVideoSrc).toBe('')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('C3.3 onSingleVideoReady 首次进全屏一次,再次调用不重复(守卫)', function () {
|
|
|
+ var vm = instantiate(getComponent())
|
|
|
+ vm.singleVideoSrc = 'http://x/a.mp4'
|
|
|
+ vm.onSingleVideoReady()
|
|
|
+ expect(global.uni.createVideoContext).toHaveBeenCalledTimes(1)
|
|
|
+ expect(global.uni.createVideoContext).toHaveBeenCalledWith('article-single-video', vm)
|
|
|
+ var ctx = global.uni.createVideoContext.mock.results[0].value
|
|
|
+ expect(ctx.requestFullScreen).toHaveBeenCalledTimes(1)
|
|
|
+ vm.onSingleVideoReady()
|
|
|
+ expect(global.uni.createVideoContext).toHaveBeenCalledTimes(1)
|
|
|
+ })
|
|
|
+})
|
|
|
+
|
|
|
+// ---------------------------------------------------------------------------
|
|
|
+// 契约 C4:模板结构(video-only 用 video 全屏,否则 mp-html 富文本)
|
|
|
+// ---------------------------------------------------------------------------
|
|
|
+
|
|
|
+describe('契约C4 article-detail.vue:模板结构', function () {
|
|
|
+ test('模板含 v-if="singleVideoSrc"、article-single-video video 节点', function () {
|
|
|
+ var template = loadPage().template
|
|
|
+ expect(template).toMatch(/v-if="singleVideoSrc"/)
|
|
|
+ expect(template).toMatch(/id="article-single-video"/)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('模板 video 绑定 play / loadedmetadata 触发 onSingleVideoReady', function () {
|
|
|
+ var template = loadPage().template
|
|
|
+ expect(template).toMatch(/@play="onSingleVideoReady"/)
|
|
|
+ expect(template).toMatch(/@loadedmetadata="onSingleVideoReady"/)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('模板非单视频回退 mp-html v-else 并绑定 article.content', function () {
|
|
|
+ var template = loadPage().template
|
|
|
+ expect(template).toMatch(/<mp-html v-else/)
|
|
|
+ expect(template).toMatch(/:content="article.content"/)
|
|
|
+ })
|
|
|
+})
|