@linxin666/dsh-pet 0.4.7 → 0.4.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.i18n.yaml +2 -2
- package/README.md +8 -2
- package/README.zh.md +8 -2
- package/contracts/voice-pack-v1.schema.json +27 -0
- package/lib/client.js +144 -68
- package/lib/client.js.map +1 -1
- package/lib/index.js +160 -28
- package/lib/types/affinity.d.ts +26 -3
- package/lib/types/affinity.d.ts.map +1 -1
- package/lib/types/affinity.js +27 -4
- package/lib/types/client/PetDockEntry.d.ts +5 -0
- package/lib/types/client/PetDockEntry.d.ts.map +1 -1
- package/lib/types/client/PetDockEntry.js +1 -1
- package/lib/types/client/PetSprite.d.ts +11 -4
- package/lib/types/client/PetSprite.d.ts.map +1 -1
- package/lib/types/client/PetSprite.js +120 -58
- package/lib/types/client/index.d.ts.map +1 -1
- package/lib/types/client/index.js +15 -1
- package/lib/types/client/sequences.d.ts +9 -2
- package/lib/types/client/sequences.d.ts.map +1 -1
- package/lib/types/client/sequences.js +25 -14
- package/lib/types/client/spritesheet.d.ts +22 -1
- package/lib/types/client/spritesheet.d.ts.map +1 -1
- package/lib/types/client/spritesheet.js +26 -2
- package/lib/types/index.d.ts +1 -1
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/ledger.d.ts +10 -1
- package/lib/types/ledger.d.ts.map +1 -1
- package/lib/types/ledger.js +11 -1
- package/lib/types/pair-access.d.ts.map +1 -1
- package/lib/types/pair-access.js +26 -3
- package/lib/types/service.d.ts +14 -12
- package/lib/types/service.d.ts.map +1 -1
- package/lib/types/service.js +22 -17
- package/lib/types/voice-pack.d.ts +20 -1
- package/lib/types/voice-pack.d.ts.map +1 -1
- package/lib/types/voice-pack.js +86 -3
- package/package.json +24 -24
- package/src/affinity.test.ts +42 -0
- package/src/affinity.ts +46 -3
- package/src/client/PetDockEntry.tsx +6 -0
- package/src/client/PetSprite.test.tsx +304 -26
- package/src/client/PetSprite.tsx +123 -58
- package/src/client/index.test.tsx +4 -0
- package/src/client/index.ts +15 -1
- package/src/client/pet-css.test.ts +12 -0
- package/src/client/pet.module.css +9 -3
- package/src/client/sequences.test.ts +37 -1
- package/src/client/sequences.ts +42 -16
- package/src/client/spritesheet.test.ts +81 -0
- package/src/client/spritesheet.ts +27 -2
- package/src/index.ts +2 -0
- package/src/ledger.test.ts +19 -0
- package/src/ledger.ts +15 -1
- package/src/pair-access.ts +26 -3
- package/src/registry.test.ts +2 -1
- package/src/service.test.ts +61 -0
- package/src/service.ts +27 -18
- package/src/voice-pack.test.ts +112 -0
- package/src/voice-pack.ts +101 -3
|
@@ -11,7 +11,8 @@ import { t } from './locales.ts'
|
|
|
11
11
|
import type { PetStateView } from '../service.ts'
|
|
12
12
|
import type { PetAnnouncement } from '../announce.ts'
|
|
13
13
|
import type { PetDefinition, PetTrackDef } from '../registry.ts'
|
|
14
|
-
import type {
|
|
14
|
+
import type { GameplayBus } from './gameplay-hud.ts'
|
|
15
|
+
import { rowOf, type PetAnimation } from '../state.ts'
|
|
15
16
|
import type { DecorationView } from '../contracts/status-decoration.ts'
|
|
16
17
|
|
|
17
18
|
/** A minimal pet definition (geometry + tracks) as served by the host. */
|
|
@@ -71,8 +72,8 @@ const snapshot: PetStateView = {
|
|
|
71
72
|
beforeAll(() => {
|
|
72
73
|
// Deterministic zh copy for button labels.
|
|
73
74
|
document.documentElement.lang = 'zh'
|
|
74
|
-
// Prefer-reduced-motion matches: the sprite loop then never schedules
|
|
75
|
-
//
|
|
75
|
+
// Prefer-reduced-motion matches: the sprite loop then never schedules a
|
|
76
|
+
// frame timer, keeping the test free of animation timers.
|
|
76
77
|
Object.defineProperty(window, 'matchMedia', {
|
|
77
78
|
configurable: true,
|
|
78
79
|
value: (query: string) => ({
|
|
@@ -194,6 +195,48 @@ describe('PetSprite affinity rank localization (#1226)', () => {
|
|
|
194
195
|
document.documentElement.lang = 'zh'
|
|
195
196
|
}
|
|
196
197
|
})
|
|
198
|
+
|
|
199
|
+
it('uses a pet-declared English rank name when the UI locale is en', () => {
|
|
200
|
+
document.documentElement.lang = 'en'
|
|
201
|
+
try {
|
|
202
|
+
renderPet({
|
|
203
|
+
lang: 'en',
|
|
204
|
+
snapshot: {
|
|
205
|
+
...snapshot,
|
|
206
|
+
affinity: { ...snapshot.affinity, rank: '小煤球', rankEn: 'Little Coal Ball' },
|
|
207
|
+
},
|
|
208
|
+
})
|
|
209
|
+
fireEvent.pointerOver(screen.getByRole('button', { name: '鲸鱼娘' }))
|
|
210
|
+
expect(screen.getByText('Affinity Little Coal Ball')).toBeTruthy()
|
|
211
|
+
} finally {
|
|
212
|
+
document.documentElement.lang = 'zh'
|
|
213
|
+
}
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
it('uses the default declared rank name outside English', () => {
|
|
217
|
+
renderPet({
|
|
218
|
+
lang: 'zh',
|
|
219
|
+
snapshot: {
|
|
220
|
+
...snapshot,
|
|
221
|
+
affinity: { ...snapshot.affinity, rank: '小煤球', rankEn: 'Little Coal Ball' },
|
|
222
|
+
},
|
|
223
|
+
})
|
|
224
|
+
fireEvent.pointerOver(screen.getByRole('button', { name: '鲸鱼娘' }))
|
|
225
|
+
expect(screen.getByText('亲密度 小煤球')).toBeTruthy()
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
it('feeds the effective rank name into a custom panel stat format', () => {
|
|
229
|
+
renderPet({
|
|
230
|
+
lang: 'en',
|
|
231
|
+
definition: { ...petDefinition(), panel: { stats: { rank: 'Bond: {rank}' } } },
|
|
232
|
+
snapshot: {
|
|
233
|
+
...snapshot,
|
|
234
|
+
affinity: { ...snapshot.affinity, rank: '小煤球', rankEn: 'Little Coal Ball' },
|
|
235
|
+
},
|
|
236
|
+
})
|
|
237
|
+
fireEvent.pointerOver(screen.getByRole('button', { name: '鲸鱼娘' }))
|
|
238
|
+
expect(screen.getByText('Bond: Little Coal Ball')).toBeTruthy()
|
|
239
|
+
})
|
|
197
240
|
})
|
|
198
241
|
|
|
199
242
|
describe('PetSprite rename input', () => {
|
|
@@ -587,13 +630,15 @@ describe('PetSprite definition-driven render', () => {
|
|
|
587
630
|
removeListener: () => {},
|
|
588
631
|
dispatchEvent: () => false,
|
|
589
632
|
})
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
633
|
+
let now = 0
|
|
634
|
+
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
635
|
+
const timers: { at: number; callback: () => void }[] = []
|
|
636
|
+
let timerId = 0
|
|
637
|
+
vi.spyOn(window, 'setTimeout').mockImplementation(((callback: () => void, delay = 0) => {
|
|
638
|
+
timers.push({ at: now + delay, callback })
|
|
639
|
+
return ++timerId
|
|
640
|
+
}) as typeof window.setTimeout)
|
|
641
|
+
vi.spyOn(window, 'clearTimeout').mockImplementation(() => {})
|
|
597
642
|
const definition = petDefinition()
|
|
598
643
|
definition.sequences = {
|
|
599
644
|
thinking: ['running', 'waiting', 'running', 'waiting', 'running'],
|
|
@@ -604,7 +649,11 @@ describe('PetSprite definition-driven render', () => {
|
|
|
604
649
|
})
|
|
605
650
|
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
606
651
|
expect(sprite.style.backgroundPosition).toBe('0px -1120px')
|
|
607
|
-
|
|
652
|
+
// The loop sleeps for the frame's remaining time; jump the clock past the
|
|
653
|
+
// first item of the sequence (running frames are 250ms each) and fire the
|
|
654
|
+
// one pending tick.
|
|
655
|
+
now = 1_500
|
|
656
|
+
act(() => { for (const timer of timers.splice(0)) timer.callback() })
|
|
608
657
|
expect(sprite.style.backgroundPosition).toBe('0px -960px')
|
|
609
658
|
})
|
|
610
659
|
|
|
@@ -619,23 +668,30 @@ describe('PetSprite definition-driven render', () => {
|
|
|
619
668
|
removeListener: () => {},
|
|
620
669
|
dispatchEvent: () => false,
|
|
621
670
|
})
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
671
|
+
let now = 0
|
|
672
|
+
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
673
|
+
const timers: { at: number; callback: () => void }[] = []
|
|
674
|
+
let timerId = 0
|
|
675
|
+
vi.spyOn(window, 'setTimeout').mockImplementation(((callback: () => void, delay = 0) => {
|
|
676
|
+
timers.push({ at: now + delay, callback })
|
|
677
|
+
return ++timerId
|
|
678
|
+
}) as typeof window.setTimeout)
|
|
679
|
+
vi.spyOn(window, 'clearTimeout').mockImplementation(() => {})
|
|
629
680
|
renderPet({
|
|
630
681
|
snapshot: { ...snapshot, animation: 'idle', phase: 'idle' },
|
|
631
682
|
})
|
|
632
683
|
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
633
684
|
const styleSetterSpy = vi.spyOn(sprite.style, 'backgroundPosition', 'set')
|
|
634
|
-
// Idle frame 0 duration is 400ms. An intermediate tick at 16ms does not
|
|
635
|
-
|
|
685
|
+
// Idle frame 0 duration is 400ms. An intermediate tick at 16ms does not
|
|
686
|
+
// advance the frame, writes nothing, and the loop sleeps to the boundary
|
|
687
|
+
// (t=400) rather than polling every screen refresh.
|
|
688
|
+
now = 16
|
|
689
|
+
act(() => { for (const timer of timers.splice(0)) timer.callback() })
|
|
636
690
|
expect(styleSetterSpy).not.toHaveBeenCalled()
|
|
691
|
+
expect(timers[0]?.at).toBe(400)
|
|
637
692
|
// A tick past 400ms advances to frame 1 and should set backgroundPosition once.
|
|
638
|
-
|
|
693
|
+
now = 410
|
|
694
|
+
act(() => { for (const timer of timers.splice(0)) timer.callback() })
|
|
639
695
|
expect(styleSetterSpy).toHaveBeenCalledTimes(1)
|
|
640
696
|
})
|
|
641
697
|
})
|
|
@@ -814,8 +870,8 @@ describe('PetSprite status decoration (pet-center M5, #567)', () => {
|
|
|
814
870
|
})
|
|
815
871
|
let now = 0
|
|
816
872
|
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
817
|
-
// The ornament
|
|
818
|
-
//
|
|
873
|
+
// The ornament and the sprite both schedule by frame duration
|
|
874
|
+
// (setTimeout), so one capture and one stepping helper drive both loops.
|
|
819
875
|
const timers: { at: number; callback: () => void }[] = []
|
|
820
876
|
let timerId = 0
|
|
821
877
|
vi.spyOn(window, 'setTimeout').mockImplementation(((callback: () => void, delay = 0) => {
|
|
@@ -948,7 +1004,12 @@ describe('PetSprite status decoration (pet-center M5, #567)', () => {
|
|
|
948
1004
|
return frames.length
|
|
949
1005
|
})
|
|
950
1006
|
vi.spyOn(window, 'cancelAnimationFrame').mockImplementation(() => {})
|
|
951
|
-
|
|
1007
|
+
// A custom visual keeps the sprite's own frame loop out of the timer
|
|
1008
|
+
// capture, so a pending timer can only be the ornament's.
|
|
1009
|
+
renderPet({
|
|
1010
|
+
visual: <span data-testid="custom-visual" />,
|
|
1011
|
+
snapshot: { ...snapshot, bubble: '完成', phase: 'done', decoration: { ...decoration, loop: false } },
|
|
1012
|
+
})
|
|
952
1013
|
const el = ornament()!
|
|
953
1014
|
const step = (ms: number): void => {
|
|
954
1015
|
for (const callback of frames.splice(0)) callback(now)
|
|
@@ -967,7 +1028,7 @@ describe('PetSprite status decoration (pet-center M5, #567)', () => {
|
|
|
967
1028
|
expect(el.style.backgroundPosition).toBe('-48px 0px')
|
|
968
1029
|
act(() => { step(161) })
|
|
969
1030
|
expect(el.style.backgroundPosition).toBe('-72px 0px')
|
|
970
|
-
// The ornament stopped scheduling timers
|
|
1031
|
+
// The ornament stopped scheduling timers.
|
|
971
1032
|
expect(timers).toHaveLength(0)
|
|
972
1033
|
act(() => { step(161) })
|
|
973
1034
|
// The last frame holds.
|
|
@@ -1000,7 +1061,9 @@ describe('PetSprite status decoration (pet-center M5, #567)', () => {
|
|
|
1000
1061
|
})
|
|
1001
1062
|
vi.spyOn(window, 'cancelAnimationFrame').mockImplementation(() => {})
|
|
1002
1063
|
// failed binds the single frame 3 of a 24px-wide frame; loop stays true.
|
|
1064
|
+
// The custom visual keeps the sprite's own frame loop out of the capture.
|
|
1003
1065
|
renderPet({
|
|
1066
|
+
visual: <span data-testid="custom-visual" />,
|
|
1004
1067
|
snapshot: {
|
|
1005
1068
|
...snapshot,
|
|
1006
1069
|
bubble: '失败',
|
|
@@ -1010,7 +1073,7 @@ describe('PetSprite status decoration (pet-center M5, #567)', () => {
|
|
|
1010
1073
|
})
|
|
1011
1074
|
const el = ornament()!
|
|
1012
1075
|
// The ornament settles on its only frame, exactly like the reduced-motion
|
|
1013
|
-
// hold — no timer may start
|
|
1076
|
+
// hold — no timer may start.
|
|
1014
1077
|
expect(el.style.backgroundPosition).toBe('-72px 0px')
|
|
1015
1078
|
expect(timers).toHaveLength(0)
|
|
1016
1079
|
const step = (ts: number): void => { for (const callback of frames.splice(0)) callback(ts) }
|
|
@@ -1399,3 +1462,218 @@ describe('PetSprite announcement bubbles from several publishers (issue #1812)',
|
|
|
1399
1462
|
})
|
|
1400
1463
|
})
|
|
1401
1464
|
|
|
1465
|
+
/**
|
|
1466
|
+
* Capture every timer the pet schedules onto a virtual clock. The sprite frame
|
|
1467
|
+
* loop sleeps until the next frame boundary (issue #17), so a test steps it by
|
|
1468
|
+
* moving the clock and firing whatever became due.
|
|
1469
|
+
*/
|
|
1470
|
+
function captureTimers(clock: () => number): { at: number; callback: () => void }[] {
|
|
1471
|
+
const timers: { at: number; callback: () => void }[] = []
|
|
1472
|
+
let timerId = 0
|
|
1473
|
+
vi.spyOn(window, 'setTimeout').mockImplementation(((callback: () => void, delay = 0) => {
|
|
1474
|
+
timers.push({ at: clock() + delay, callback })
|
|
1475
|
+
return ++timerId
|
|
1476
|
+
}) as typeof window.setTimeout)
|
|
1477
|
+
vi.spyOn(window, 'clearTimeout').mockImplementation(() => {})
|
|
1478
|
+
return timers
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
/** Fire every captured timer once; each one may schedule the next wake. */
|
|
1482
|
+
function fireDue(timers: { at: number; callback: () => void }[]): void {
|
|
1483
|
+
for (const timer of timers.splice(0)) timer.callback()
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
describe('PetSprite render cost (issue #17)', () => {
|
|
1487
|
+
it('snaps the atlas grid and the sprite box to whole pixels', () => {
|
|
1488
|
+
renderPet()
|
|
1489
|
+
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
1490
|
+
// 192x208 cells at display.size 160 are 147.69px and 160px unrounded. The
|
|
1491
|
+
// box, the atlas grid and every frame offset share the whole-pixel cell,
|
|
1492
|
+
// so a frame change is a whole-pixel translate of the scaled atlas instead
|
|
1493
|
+
// of a fresh resample of the 1536x1872 source.
|
|
1494
|
+
expect(sprite.style.width).toBe('148px')
|
|
1495
|
+
expect(sprite.style.height).toBe('160px')
|
|
1496
|
+
expect(sprite.style.backgroundSize).toBe('1184px 1440px')
|
|
1497
|
+
})
|
|
1498
|
+
|
|
1499
|
+
it('advances frames on a duration timer instead of the screen refresh', () => {
|
|
1500
|
+
vi.spyOn(window, 'matchMedia').mockReturnValue({
|
|
1501
|
+
matches: false,
|
|
1502
|
+
media: '(prefers-reduced-motion: reduce)',
|
|
1503
|
+
onchange: null,
|
|
1504
|
+
addEventListener: () => {},
|
|
1505
|
+
removeEventListener: () => {},
|
|
1506
|
+
addListener: () => {},
|
|
1507
|
+
removeListener: () => {},
|
|
1508
|
+
dispatchEvent: () => false,
|
|
1509
|
+
})
|
|
1510
|
+
let now = 0
|
|
1511
|
+
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
1512
|
+
const timers = captureTimers(() => now)
|
|
1513
|
+
const raf = vi.spyOn(window, 'requestAnimationFrame')
|
|
1514
|
+
renderPet()
|
|
1515
|
+
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
1516
|
+
// Nothing drives the frame loop off the screen refresh any more.
|
|
1517
|
+
expect(raf).not.toHaveBeenCalled()
|
|
1518
|
+
// One pending wake: the loop's first tick, which then sleeps to the end of
|
|
1519
|
+
// the frame in play instead of re-arming for the next screen refresh.
|
|
1520
|
+
expect(timers).toHaveLength(1)
|
|
1521
|
+
act(() => { fireDue(timers) })
|
|
1522
|
+
expect(timers[0]?.at).toBe(400)
|
|
1523
|
+
now = 410
|
|
1524
|
+
act(() => { fireDue(timers) })
|
|
1525
|
+
expect(sprite.style.backgroundPosition).toBe('-148px 0px')
|
|
1526
|
+
// Idle frame 1 is already 10ms in, so the next wake is 390ms away.
|
|
1527
|
+
expect(timers[0]?.at).toBe(800)
|
|
1528
|
+
})
|
|
1529
|
+
|
|
1530
|
+
it('stops waking once a non-looping track holds its final frame', () => {
|
|
1531
|
+
vi.spyOn(window, 'matchMedia').mockReturnValue({
|
|
1532
|
+
matches: false,
|
|
1533
|
+
media: '(prefers-reduced-motion: reduce)',
|
|
1534
|
+
onchange: null,
|
|
1535
|
+
addEventListener: () => {},
|
|
1536
|
+
removeEventListener: () => {},
|
|
1537
|
+
addListener: () => {},
|
|
1538
|
+
removeListener: () => {},
|
|
1539
|
+
dispatchEvent: () => false,
|
|
1540
|
+
})
|
|
1541
|
+
let now = 0
|
|
1542
|
+
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
1543
|
+
const timers = captureTimers(() => now)
|
|
1544
|
+
renderPet({ snapshot: { ...snapshot, animation: 'failed', phase: 'failed' } })
|
|
1545
|
+
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
1546
|
+
// 'failed' is a non-looping 8-frame track at 450ms per frame; the 8th frame
|
|
1547
|
+
// holds once the host owns the switch to its fallback.
|
|
1548
|
+
now = 8 * 450
|
|
1549
|
+
act(() => { fireDue(timers) })
|
|
1550
|
+
expect(sprite.style.backgroundPosition).toBe('-' + (7 * 148) + 'px -' + (rowOf('failed') * 160) + 'px')
|
|
1551
|
+
// The track is settled: no timer is pending, so the pet stops waking the
|
|
1552
|
+
// main thread for a frame that cannot change.
|
|
1553
|
+
expect(timers).toHaveLength(0)
|
|
1554
|
+
const styleSetterSpy = vi.spyOn(sprite.style, 'backgroundPosition', 'set')
|
|
1555
|
+
now = 60_000
|
|
1556
|
+
act(() => { fireDue(timers) })
|
|
1557
|
+
expect(styleSetterSpy).not.toHaveBeenCalled()
|
|
1558
|
+
expect(timers).toHaveLength(0)
|
|
1559
|
+
})
|
|
1560
|
+
|
|
1561
|
+
it('repaints a settled frame when the sprite scale changes', () => {
|
|
1562
|
+
vi.spyOn(window, 'matchMedia').mockReturnValue({
|
|
1563
|
+
matches: false,
|
|
1564
|
+
media: '(prefers-reduced-motion: reduce)',
|
|
1565
|
+
onchange: null,
|
|
1566
|
+
addEventListener: () => {},
|
|
1567
|
+
removeEventListener: () => {},
|
|
1568
|
+
addListener: () => {},
|
|
1569
|
+
removeListener: () => {},
|
|
1570
|
+
dispatchEvent: () => false,
|
|
1571
|
+
})
|
|
1572
|
+
let now = 0
|
|
1573
|
+
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
1574
|
+
const timers = captureTimers(() => now)
|
|
1575
|
+
const definition = petDefinition()
|
|
1576
|
+
const display = snapshot.display
|
|
1577
|
+
const failed = { ...snapshot, animation: 'failed' as const, phase: 'failed' as const }
|
|
1578
|
+
const first = render(<PetSprite {...petProps({ definition, display, snapshot: failed })} />)
|
|
1579
|
+
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
1580
|
+
now = 8 * 450
|
|
1581
|
+
act(() => { fireDue(timers) })
|
|
1582
|
+
// A display.size change re-renders without restarting the track, and the
|
|
1583
|
+
// settled loop is woken by nothing: the held frame must be repainted at the
|
|
1584
|
+
// new scale or the box would show a neighbouring cell.
|
|
1585
|
+
first.rerender(<PetSprite {...petProps({ definition, display: { ...display, size: 120 }, snapshot: failed })} />)
|
|
1586
|
+
expect(sprite.style.width).toBe('111px')
|
|
1587
|
+
expect(sprite.style.height).toBe('120px')
|
|
1588
|
+
expect(sprite.style.backgroundPosition).toBe('-' + (7 * 111) + 'px -' + (rowOf('failed') * 120) + 'px')
|
|
1589
|
+
})
|
|
1590
|
+
})
|
|
1591
|
+
|
|
1592
|
+
describe('PetSprite roaming (issue #17)', () => {
|
|
1593
|
+
/** Drive the walk loop with a captured requestAnimationFrame. */
|
|
1594
|
+
function captureFrame(clock: () => number): { fire: () => void } {
|
|
1595
|
+
let pending: FrameRequestCallback | undefined
|
|
1596
|
+
vi.spyOn(window, 'requestAnimationFrame').mockImplementation(callback => {
|
|
1597
|
+
pending = callback
|
|
1598
|
+
return 1
|
|
1599
|
+
})
|
|
1600
|
+
vi.spyOn(window, 'cancelAnimationFrame').mockImplementation(() => {})
|
|
1601
|
+
return {
|
|
1602
|
+
fire: () => {
|
|
1603
|
+
const callback = pending
|
|
1604
|
+
pending = undefined
|
|
1605
|
+
callback?.(clock())
|
|
1606
|
+
},
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
it('writes the walk position to the element and renders only on landing', () => {
|
|
1611
|
+
let now = 0
|
|
1612
|
+
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
1613
|
+
const frame = captureFrame(() => now)
|
|
1614
|
+
const onDragEnd = vi.fn()
|
|
1615
|
+
const tSpy = vi.fn(t)
|
|
1616
|
+
const bus: GameplayBus = {}
|
|
1617
|
+
renderPet({ onDragEnd, t: tSpy, bus })
|
|
1618
|
+
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
1619
|
+
const float = sprite.parentElement!.parentElement as HTMLDivElement
|
|
1620
|
+
// Open the hover panel: every render then resolves panel copy through t, so
|
|
1621
|
+
// the t call count is a render probe the walk must not disturb.
|
|
1622
|
+
fireEvent.pointerOver(sprite)
|
|
1623
|
+
const renders = tSpy.mock.calls.length
|
|
1624
|
+
// 24 + 120 right, 60 px/s, so 2s of motion toward right = 144.
|
|
1625
|
+
expect(bus.walk?.('left', 120, 60)).toBe(120)
|
|
1626
|
+
now = 1_000
|
|
1627
|
+
act(() => { frame.fire() })
|
|
1628
|
+
expect(float.style.right).toBe('84px')
|
|
1629
|
+
expect(float.style.bottom).toBe('20px')
|
|
1630
|
+
// Mid-walk movement is a DOM write, not a React state update.
|
|
1631
|
+
expect(tSpy.mock.calls.length).toBe(renders)
|
|
1632
|
+
now = 2_000
|
|
1633
|
+
act(() => { frame.fire() })
|
|
1634
|
+
expect(float.style.right).toBe('144px')
|
|
1635
|
+
expect(onDragEnd).toHaveBeenCalledTimes(1)
|
|
1636
|
+
expect(onDragEnd).toHaveBeenCalledWith(144, 20)
|
|
1637
|
+
// The landing frame hands the spot to React state, which keeps rendering it.
|
|
1638
|
+
expect(tSpy.mock.calls.length).toBeGreaterThan(renders)
|
|
1639
|
+
expect(float.style.right).toBe('144px')
|
|
1640
|
+
})
|
|
1641
|
+
|
|
1642
|
+
it('keeps the walked position when a render lands mid-walk', () => {
|
|
1643
|
+
let now = 0
|
|
1644
|
+
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
1645
|
+
const frame = captureFrame(() => now)
|
|
1646
|
+
const definition = petDefinition()
|
|
1647
|
+
const display = snapshot.display
|
|
1648
|
+
const bus: GameplayBus = {}
|
|
1649
|
+
const first = render(<PetSprite {...petProps({ definition, display, bus })} />)
|
|
1650
|
+
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
1651
|
+
const float = sprite.parentElement!.parentElement as HTMLDivElement
|
|
1652
|
+
bus.walk?.('left', 120, 60)
|
|
1653
|
+
now = 1_000
|
|
1654
|
+
act(() => { frame.fire() })
|
|
1655
|
+
expect(float.style.right).toBe('84px')
|
|
1656
|
+
// A host poll re-renders the sprite mid-walk; the walked spot rides a ref,
|
|
1657
|
+
// so the sprite must not snap back to its saved position.
|
|
1658
|
+
first.rerender(<PetSprite {...petProps({ definition, display, bus })} />)
|
|
1659
|
+
expect(float.style.right).toBe('84px')
|
|
1660
|
+
})
|
|
1661
|
+
|
|
1662
|
+
it('starts a drag from the position the pet reached mid-walk', () => {
|
|
1663
|
+
let now = 0
|
|
1664
|
+
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
1665
|
+
const frame = captureFrame(() => now)
|
|
1666
|
+
const onDragEnd = vi.fn()
|
|
1667
|
+
const bus: GameplayBus = {}
|
|
1668
|
+
renderPet({ onDragEnd, bus })
|
|
1669
|
+
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
1670
|
+
bus.walk?.('left', 120, 60)
|
|
1671
|
+
now = 1_000
|
|
1672
|
+
act(() => { frame.fire() })
|
|
1673
|
+
// Grab the pet where it actually is, not where the walk started.
|
|
1674
|
+
fireEvent.pointerDown(sprite, { clientX: 100, clientY: 100 })
|
|
1675
|
+
fireEvent.pointerUp(sprite)
|
|
1676
|
+
expect(onDragEnd).toHaveBeenCalledWith(84, 20)
|
|
1677
|
+
})
|
|
1678
|
+
})
|
|
1679
|
+
|