fix: polish bark battle creation flow

This commit is contained in:
kdletters
2026-05-22 05:00:07 +08:00
parent 01da85a577
commit bf82f04b64
73 changed files with 9362 additions and 2663 deletions

View File

@@ -0,0 +1,108 @@
import { expect, test } from 'vitest';
import type { BarkBattleWorkSummary } from '../../../packages/shared/src/contracts/barkBattle';
import {
mergeBarkBattleWorksByWorkId,
mergeBarkBattleWorkSummary,
shouldPreserveLocalBarkBattleWorkOnRefresh,
} from './barkBattleWorkCache';
function buildBarkBattleWork(
overrides: Partial<BarkBattleWorkSummary> = {},
): BarkBattleWorkSummary {
return {
workId: 'BB-cache-race-12345678',
draftId: 'bark-battle-draft-1',
ownerUserId: 'user-1',
authorDisplayName: '测试玩家',
title: '汪汪测试杯',
summary: '测试声浪赛',
themeDescription: '阳光草坪声浪竞技场',
playerImageDescription: '戴红色围巾的柯基选手',
opponentImageDescription: '蓝色护目镜哈士奇对手',
playerCharacterImageSrc: '/generated-bark-battle/player.png',
opponentCharacterImageSrc: '/generated-bark-battle/opponent.png',
uiBackgroundImageSrc: '/generated-bark-battle/background.png',
difficultyPreset: 'normal',
status: 'draft',
generationStatus: 'ready',
publishReady: true,
playCount: 0,
updatedAt: '2026-05-21T10:00:00.000Z',
publishedAt: null,
...overrides,
};
}
test('preserves local published bark battle when refresh only returns same work draft', () => {
const published = buildBarkBattleWork({
status: 'published',
playCount: 3,
updatedAt: '2026-05-21T10:02:00.000Z',
publishedAt: '2026-05-21T10:02:00.000Z',
});
const refreshedDraft = buildBarkBattleWork({
status: 'draft',
playCount: 0,
updatedAt: '2026-05-21T10:01:00.000Z',
publishedAt: null,
});
expect(shouldPreserveLocalBarkBattleWorkOnRefresh(published, [refreshedDraft])).toBe(
true,
);
const [merged] = mergeBarkBattleWorksByWorkId([refreshedDraft, published]);
expect(merged?.status).toBe('published');
expect(merged?.publishedAt).toBe('2026-05-21T10:02:00.000Z');
expect(merged?.playCount).toBe(3);
});
test('does not let later draft cache updates downgrade an existing published bark battle', () => {
const published = buildBarkBattleWork({
status: 'published',
playCount: 4,
updatedAt: '2026-05-21T10:03:00.000Z',
publishedAt: '2026-05-21T10:03:00.000Z',
});
const staleDraft = buildBarkBattleWork({
title: '旧草稿标题',
status: 'draft',
playCount: 0,
updatedAt: '2026-05-21T10:01:00.000Z',
publishedAt: null,
});
const merged = mergeBarkBattleWorkSummary(published, staleDraft);
expect(merged.status).toBe('published');
expect(merged.title).toBe('汪汪测试杯');
expect(merged.playCount).toBe(4);
expect(merged.publishedAt).toBe('2026-05-21T10:03:00.000Z');
});
test('preserves local ready bark battle draft when refresh has not returned it yet', () => {
const readyDraft = buildBarkBattleWork({
status: 'draft',
generationStatus: 'ready',
publishReady: true,
playerCharacterImageSrc: '/generated-bark-battle/player-ready.png',
opponentCharacterImageSrc: '/generated-bark-battle/opponent-ready.png',
uiBackgroundImageSrc: '/generated-bark-battle/background-ready.png',
});
expect(shouldPreserveLocalBarkBattleWorkOnRefresh(readyDraft, [])).toBe(true);
const merged = mergeBarkBattleWorksByWorkId([
...[],
...(shouldPreserveLocalBarkBattleWorkOnRefresh(readyDraft, [])
? [readyDraft]
: []),
]);
expect(merged).toHaveLength(1);
expect(merged[0]?.workId).toBe('BB-cache-race-12345678');
expect(merged[0]?.generationStatus).toBe('ready');
});