import { describe, expect, test } from 'vitest'; import { currentVersionResourceBindingIds, formatIterationVersionLabel, isResourceUsedByCurrentVersion, } from '../src/features/resource-canvas/resourceCanvasVersionBindingModel'; const versions = [ { versionId: 'version-root', parentVersionId: null, projectRevision: 3, resourceBindings: [ { slotId: 'asset:asset-player', resourceId: 'asset-player' }, ], createdReason: 'initial' as const, createdAt: 1_760_000_000_000, }, { versionId: 'version-child', parentVersionId: 'version-root', projectRevision: 4, resourceBindings: [ { slotId: 'asset:asset-town', resourceId: 'asset-town' }, // 悬空绑定的 slotId 不是恒等映射,不参与「当前使用」判定。 { slotId: 'player', resourceId: 'asset-player' }, ], createdReason: 'agent-revision' as const, createdAt: 1_760_003_600_000, }, ]; describe('运行模块版本绑定判定', () => { test('falls back to the newest manifest version when no version is selected', () => { expect(Array.from(currentVersionResourceBindingIds({ versions }))).toEqual([ 'asset-town', ]); }); test('resolves the binding ids of the explicitly selected version', () => { expect( Array.from( currentVersionResourceBindingIds({ versions }, 'version-root'), ), ).toEqual(['asset-player']); }); test('returns no bindings for a missing version or a manifest without versions', () => { expect( currentVersionResourceBindingIds({ versions }, 'version-missing').size, ).toBe(0); expect(currentVersionResourceBindingIds({}).size).toBe(0); expect(currentVersionResourceBindingIds({ versions: [] }).size).toBe(0); }); test('marks only registered assets bound by the current version as in use', () => { const bindingIds = currentVersionResourceBindingIds( { versions }, 'version-root', ); expect( isResourceUsedByCurrentVersion( { manifestAssetId: 'asset-player' }, bindingIds, ), ).toBe(true); expect( isResourceUsedByCurrentVersion( { manifestAssetId: 'asset-town' }, bindingIds, ), ).toBe(false); // 未登记为 manifest 素材的资源永远不算「当前使用」。 expect( isResourceUsedByCurrentVersion({ manifestAssetId: null }, bindingIds), ).toBe(false); }); test('labels a version with its Chinese created reason and creation time', () => { expect(formatIterationVersionLabel(versions[0])).toBe( `初始版本 · ${new Date(1_760_000_000_000).toLocaleString('zh-CN')}`, ); expect(formatIterationVersionLabel(versions[1])).toBe( `智能体修订 · ${new Date(1_760_003_600_000).toLocaleString('zh-CN')}`, ); }); });