| 1 | import type { Provider } from '@nestjs/common' |
| 2 | import { AccountType } from '@yikart/common' |
| 3 | import { describe, expect, it, vi } from 'vitest' |
| 4 | import { PlatformStatus } from '../platforms.interface' |
| 5 | import { PlatformIntegrationRegistry } from '../platforms.registry' |
| 6 | import { KwaiAnalyticsProvider } from './kwai-analytics.provider' |
| 7 | import { KwaiAuthProvider } from './kwai-auth.provider' |
| 8 | import { KwaiPublishProvider } from './kwai-publish.provider' |
| 9 | import { KwaiWorkProvider } from './kwai-work.provider' |
| 10 | import { KwaiConfig } from './kwai.config' |
| 11 | import { KwaiModule } from './kwai.module' |
| 12 | |
| 13 | vi.mock('../platforms.registry.module', () => ({ |
| 14 | PlatformRegistryModule: class PlatformRegistryModule {}, |
| 15 | })) |
| 16 | |
| 17 | vi.mock('../../media/media.service', () => ({ |
| 18 | MediaService: class MediaService {}, |
| 19 | })) |
| 20 | |
| 21 | vi.mock('@yikart/mongodb', () => ({ |
| 22 | AssetType: { |
| 23 | AiImage: 'aiImage', |
| 24 | AiVideo: 'aiVideo', |
| 25 | AiCard: 'aiCard', |
| 26 | AiChatImage: 'aiChatImage', |
| 27 | AideoOutput: 'aideoOutput', |
| 28 | VideoEdit: 'videoEdit', |
| 29 | DramaRecap: 'dramaRecap', |
| 30 | StyleTransfer: 'styleTransfer', |
| 31 | ImageEdit: 'imageEdit', |
| 32 | Subtitle: 'subtitle', |
| 33 | UserMedia: 'userMedia', |
| 34 | UserFile: 'userFile', |
| 35 | PublishMedia: 'publishMedia', |
| 36 | Avatar: 'avatar', |
| 37 | AgentSession: 'agentSession', |
| 38 | VideoThumbnail: 'videoThumbnail', |
| 39 | GooglePlace: 'googlePlace', |
| 40 | Temp: 'temp', |
| 41 | }, |
| 42 | PublishRecordRepository: class PublishRecordRepository {}, |
| 43 | PublishStatus: { |
| 44 | WaitingForPublish: 'waiting_for_publish', |
| 45 | Queued: 'queued', |
| 46 | Publishing: 'publishing', |
| 47 | Published: 'published', |
| 48 | Failed: 'failed', |
| 49 | }, |
| 50 | })) |
| 51 | |
| 52 | vi.mock('@yikart/redis', () => ({ |
| 53 | EventStream: { Channels: 'channels' }, |
| 54 | EventStreamService: class EventStreamService {}, |
| 55 | EventTopic: { |
| 56 | ChannelsPublishTaskCreated: 'channels.publish.task.created', |
| 57 | ChannelsPublishTaskPublished: 'channels.publish.task.published', |
| 58 | ChannelsPublishTaskFailed: 'channels.publish.task.failed', |
| 59 | }, |
| 60 | })) |
| 61 | |
| 62 | interface KwaiIntegrationProvider { |
| 63 | provide: 'KWAI_INTEGRATION' |
| 64 | inject: unknown[] |
| 65 | useFactory: ( |
| 66 | registry: PlatformIntegrationRegistry, |
| 67 | auth: KwaiAuthProvider, |
| 68 | publish: KwaiPublishProvider, |
| 69 | work: KwaiWorkProvider, |
| 70 | analytics: KwaiAnalyticsProvider, |
| 71 | ) => void |
| 72 | } |
| 73 | |
| 74 | function getKwaiIntegrationProvider(providers: Provider[]): KwaiIntegrationProvider { |
| 75 | const provider = providers.find(candidate => |
| 76 | typeof candidate === 'object' |
| 77 | && candidate !== null |
| 78 | && 'provide' in candidate |
| 79 | && candidate.provide === 'KWAI_INTEGRATION', |
| 80 | ) |
| 81 | |
| 82 | return provider as KwaiIntegrationProvider |
| 83 | } |
| 84 | |
| 85 | describe('kwai module registration', () => { |
| 86 | it('registers the Kwai work provider capability', () => { |
| 87 | const moduleDefinition = KwaiModule.forRoot({ |
| 88 | status: PlatformStatus.Available, |
| 89 | clientId: 'client-id', |
| 90 | clientSecret: 'client-secret', |
| 91 | redirectUri: 'https://api.example.test/kwai/callback', |
| 92 | logoUrl: 'https://assets.example.test/kwai.svg', |
| 93 | scopes: ['user_info', 'user_video_publish'], |
| 94 | } as KwaiConfig) |
| 95 | const providers = moduleDefinition.providers ?? [] |
| 96 | |
| 97 | expect(providers).toContain(KwaiWorkProvider) |
| 98 | expect(providers).toContain(KwaiAnalyticsProvider) |
| 99 | |
| 100 | const integrationProvider = getKwaiIntegrationProvider(providers) |
| 101 | expect(integrationProvider.inject).toContain(KwaiWorkProvider) |
| 102 | expect(integrationProvider.inject).toContain(KwaiAnalyticsProvider) |
| 103 | |
| 104 | const registry = new PlatformIntegrationRegistry() |
| 105 | const workProvider = new KwaiWorkProvider({} as never) |
| 106 | const analyticsProvider = {} as KwaiAnalyticsProvider |
| 107 | integrationProvider.useFactory( |
| 108 | registry, |
| 109 | {} as KwaiAuthProvider, |
| 110 | {} as KwaiPublishProvider, |
| 111 | workProvider, |
| 112 | analyticsProvider, |
| 113 | ) |
| 114 | |
| 115 | expect(registry.get(AccountType.Kwai).work).toBe(workProvider) |
| 116 | expect(registry.get(AccountType.Kwai).analytics).toBe(analyticsProvider) |
| 117 | expect(registry.listMetadata()[0]?.capabilities.work.getLinkInfo).toBe(true) |
| 118 | expect(registry.listMetadata()[0]?.capabilities.work.listWorksPagination).toMatchObject({ |
| 119 | mode: 'cursor', |
| 120 | defaultLimit: 20, |
| 121 | maxLimit: 200, |
| 122 | supportsPrevious: false, |
| 123 | }) |
| 124 | expect(registry.listMetadata()[0]?.capabilities.analytics.account).toBe(true) |
| 125 | }) |
| 126 | }) |
| 127 |