Files
geo/apps/desktop-client/src/main/adapters/toutiao.test.ts
T
root a9591143e8 fix(toutiao): fail publish when body images fail to upload
Route content/cover images through the desktop media-image asset
pipeline and abort before submitting when any body image was not
replaced with a Toutiao-hosted URL, so we don't publish articles
that reference our own private asset endpoints.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 20:24:28 +08:00

67 lines
2.3 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
vi.mock('../../../../../packages/publisher-platforms/src/toutiao', () => ({
publishToutiaoArticle: vi.fn(async () => ({
success: true,
status: 'success',
articleId: 'pgc-1',
mediaName: '头条号测试账号',
externalManageUrl: 'https://mp.toutiao.com/profile_v4/index',
externalArticleUrl: 'https://www.toutiao.com/article/pgc-1/',
message: '头条号发布成功。',
})),
}))
vi.mock('./media-image', () => ({
fetchImageAssetBlob: vi.fn(async () => ({
blob: new Blob(['image'], { type: 'image/png' }),
width: 1,
height: 1,
fileName: 'image.png',
})),
}))
import { publishToutiaoArticle } from '../../../../../packages/publisher-platforms/src/toutiao'
import { toutiaoAdapter } from './toutiao'
import { fetchImageAssetBlob } from './media-image'
describe('toutiao adapter', () => {
it('fetches relative public assets through the desktop asset pipeline before platform upload', async () => {
const result = await toutiaoAdapter.publish(
{
taskId: 'task-1',
accountId: 'account-1',
session: {} as never,
view: null,
playwright: null,
signal: new AbortController().signal,
phase: 'initial',
reportProgress: vi.fn(),
article: {
article_id: 889,
title: '2026年室内门锁怎么选?五款高适配产品参考',
html_content: null,
markdown_content:
'<p class="article-editor-image article-editor-image--center" align="center"><img src="/api/public/assets/body-token" alt="" /></p>',
cover_asset_url: '/api/public/assets/cover-token',
},
},
{},
)
expect(result.status).toBe('succeeded')
expect(publishToutiaoArticle).toHaveBeenCalledOnce()
const transport = vi.mocked(publishToutiaoArticle).mock.calls[0]?.[1]
expect(transport).toBeTruthy()
const bodyBlob = await transport?.fetchImageBlob('/api/public/assets/body-token')
const coverBlob = await transport?.fetchImageBlob('/api/public/assets/cover-token')
expect(bodyBlob?.type).toBe('image/png')
expect(coverBlob?.type).toBe('image/png')
expect(fetchImageAssetBlob).toHaveBeenCalledWith('/api/public/assets/body-token')
expect(fetchImageAssetBlob).toHaveBeenCalledWith('/api/public/assets/cover-token')
})
})