@linxin666/dsh-pet 0.4.6 → 0.4.8
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 +3 -1
- package/README.zh.md +3 -1
- package/lib/client.js +223 -79
- package/lib/client.js.map +1 -1
- package/lib/index.js +454 -1
- package/lib/live2d-vendor.js +129 -95
- package/lib/live2d-vendor.js.map +1 -1
- package/lib/types/client/PetSprite.d.ts +6 -4
- package/lib/types/client/PetSprite.d.ts.map +1 -1
- package/lib/types/client/PetSprite.js +100 -50
- package/lib/types/client/index.js +1 -1
- package/lib/types/client/renderers/frames2d.d.ts +5 -3
- package/lib/types/client/renderers/frames2d.d.ts.map +1 -1
- package/lib/types/client/renderers/frames2d.js +139 -33
- 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/package.json +26 -30
- package/src/client/PetSprite.test.tsx +262 -26
- package/src/client/PetSprite.tsx +99 -50
- package/src/client/index.ts +1 -1
- package/src/client/pet-css.test.ts +12 -0
- package/src/client/pet.module.css +9 -3
- package/src/client/renderers/frames2d.test.ts +103 -1
- package/src/client/renderers/frames2d.ts +143 -30
- 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/registry.test.ts +4 -2
- package/lib/invariant.js +0 -34
- package/lib/state-NvDEoCln.js +0 -455
- package/lib/types/invariant.d.ts +0 -10
- package/lib/types/invariant.d.ts.map +0 -1
- package/lib/types/invariant.js +0 -27
- package/src/invariant.ts +0 -40
|
@@ -12,12 +12,33 @@ import type { PetCell, PetTrackDef } from '../registry.ts';
|
|
|
12
12
|
export type TrackDef = PetTrackDef;
|
|
13
13
|
/** Row index of one animation track (the fixed 9-row contract). */
|
|
14
14
|
export declare function rowOfTrack(animation: PetAnimation): number;
|
|
15
|
+
/**
|
|
16
|
+
* Integer size of one cell after `scale`, in device-independent px.
|
|
17
|
+
*
|
|
18
|
+
* Rounding happens here, once, so the rendered atlas grid is integral end to
|
|
19
|
+
* end (issue #17): `background-size` is this size times the column/row count
|
|
20
|
+
* and every frame offset is an exact multiple of it. A fractional cell size
|
|
21
|
+
* leaves each frame on a sub-pixel boundary — 192px cells at `display.size`
|
|
22
|
+
* 160 scale to 147.69px — and the compositor then has to resample the whole
|
|
23
|
+
* scaled atlas on every frame change instead of translating an already-scaled
|
|
24
|
+
* bitmap by whole pixels. The pet advances a frame every 300-700 ms, so that
|
|
25
|
+
* resample was running continuously for a background surface the size of the
|
|
26
|
+
* sprite. A floor of 1px keeps a legally tiny display.size from collapsing the
|
|
27
|
+
* cell to zero and rendering nothing.
|
|
28
|
+
*/
|
|
29
|
+
export declare function scaledCellSize(cell: PetCell, scale: number): {
|
|
30
|
+
width: number;
|
|
31
|
+
height: number;
|
|
32
|
+
};
|
|
15
33
|
/**
|
|
16
34
|
* Background-position (px) of one frame cell within the scaled atlas.
|
|
17
35
|
* The background image is scaled by `scale` (element size ÷ cell size), and
|
|
18
36
|
* background-position offsets are applied in SCALED coordinates — using raw
|
|
19
37
|
* atlas coordinates here would drift each frame by the scale factor and
|
|
20
|
-
* render torn/overlapping frames.
|
|
38
|
+
* render torn/overlapping frames. The scaled cell is quantized by
|
|
39
|
+
* {@link scaledCellSize}, so the offset of frame N is exactly N whole cells:
|
|
40
|
+
* the atlas is snapped to the pixel grid and a frame change is a plain
|
|
41
|
+
* translate of the scaled bitmap.
|
|
21
42
|
*/
|
|
22
43
|
export declare function framePosition(cell: PetCell, row: number, col: number, scale?: number): {
|
|
23
44
|
x: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spritesheet.d.ts","sourceRoot":"","sources":["../../../src/client/spritesheet.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAS,KAAK,YAAY,EAAE,MAAM,aAAa,CAAA;AACtD,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAE1D,qDAAqD;AACrD,MAAM,MAAM,QAAQ,GAAG,WAAW,CAAA;AAElC,mEAAmE;AACnE,wBAAgB,UAAU,CAAC,SAAS,EAAE,YAAY,GAAG,MAAM,CAG1D;AAED
|
|
1
|
+
{"version":3,"file":"spritesheet.d.ts","sourceRoot":"","sources":["../../../src/client/spritesheet.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAS,KAAK,YAAY,EAAE,MAAM,aAAa,CAAA;AACtD,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAE1D,qDAAqD;AACrD,MAAM,MAAM,QAAQ,GAAG,WAAW,CAAA;AAElC,mEAAmE;AACnE,wBAAgB,UAAU,CAAC,SAAS,EAAE,YAAY,GAAG,MAAM,CAG1D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAK9F;AAED;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,SAAI,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAG1G;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,GAAG,QAAQ,CAQvE"}
|
|
@@ -12,15 +12,39 @@ export function rowOfTrack(animation) {
|
|
|
12
12
|
// The table itself lives in state.ts (rowOf) — the single source of truth.
|
|
13
13
|
return rowOf(animation);
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Integer size of one cell after `scale`, in device-independent px.
|
|
17
|
+
*
|
|
18
|
+
* Rounding happens here, once, so the rendered atlas grid is integral end to
|
|
19
|
+
* end (issue #17): `background-size` is this size times the column/row count
|
|
20
|
+
* and every frame offset is an exact multiple of it. A fractional cell size
|
|
21
|
+
* leaves each frame on a sub-pixel boundary — 192px cells at `display.size`
|
|
22
|
+
* 160 scale to 147.69px — and the compositor then has to resample the whole
|
|
23
|
+
* scaled atlas on every frame change instead of translating an already-scaled
|
|
24
|
+
* bitmap by whole pixels. The pet advances a frame every 300-700 ms, so that
|
|
25
|
+
* resample was running continuously for a background surface the size of the
|
|
26
|
+
* sprite. A floor of 1px keeps a legally tiny display.size from collapsing the
|
|
27
|
+
* cell to zero and rendering nothing.
|
|
28
|
+
*/
|
|
29
|
+
export function scaledCellSize(cell, scale) {
|
|
30
|
+
return {
|
|
31
|
+
width: Math.max(1, Math.round(cell.width * scale)),
|
|
32
|
+
height: Math.max(1, Math.round(cell.height * scale)),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
15
35
|
/**
|
|
16
36
|
* Background-position (px) of one frame cell within the scaled atlas.
|
|
17
37
|
* The background image is scaled by `scale` (element size ÷ cell size), and
|
|
18
38
|
* background-position offsets are applied in SCALED coordinates — using raw
|
|
19
39
|
* atlas coordinates here would drift each frame by the scale factor and
|
|
20
|
-
* render torn/overlapping frames.
|
|
40
|
+
* render torn/overlapping frames. The scaled cell is quantized by
|
|
41
|
+
* {@link scaledCellSize}, so the offset of frame N is exactly N whole cells:
|
|
42
|
+
* the atlas is snapped to the pixel grid and a frame change is a plain
|
|
43
|
+
* translate of the scaled bitmap.
|
|
21
44
|
*/
|
|
22
45
|
export function framePosition(cell, row, col, scale = 1) {
|
|
23
|
-
|
|
46
|
+
const scaled = scaledCellSize(cell, scale);
|
|
47
|
+
return { x: -col * scaled.width, y: -row * scaled.height };
|
|
24
48
|
}
|
|
25
49
|
/**
|
|
26
50
|
* Trim a track to the actual frame count of its row (the manifest's per-row
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@linxin666/dsh-pet",
|
|
3
3
|
"description": "Multi-pet companion plugin for the dsh web GUI: a registry-driven floating pet that reacts to model activity, with per-pet naming, petting/feeding interactions and an affinity score",
|
|
4
|
-
"version": "0.4.
|
|
4
|
+
"version": "0.4.8",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -25,10 +25,6 @@
|
|
|
25
25
|
"types": "./lib/types/index.d.ts",
|
|
26
26
|
"default": "./lib/index.js"
|
|
27
27
|
},
|
|
28
|
-
"./invariant": {
|
|
29
|
-
"types": "./lib/types/invariant.d.ts",
|
|
30
|
-
"default": "./lib/invariant.js"
|
|
31
|
-
},
|
|
32
28
|
"./client": {
|
|
33
29
|
"types": "./lib/types/client/index.d.ts",
|
|
34
30
|
"default": "./lib/client.js"
|
|
@@ -54,7 +50,7 @@
|
|
|
54
50
|
"license": "Apache-2.0",
|
|
55
51
|
"dsh": {
|
|
56
52
|
"engines": {
|
|
57
|
-
"dsh": ">=0.1
|
|
53
|
+
"dsh": ">=0.2.1-rc.2"
|
|
58
54
|
},
|
|
59
55
|
"bundle": {
|
|
60
56
|
"patch": "./cordis.patch.yml"
|
|
@@ -71,37 +67,37 @@
|
|
|
71
67
|
}
|
|
72
68
|
},
|
|
73
69
|
"dependencies": {
|
|
74
|
-
"@deepseek-ai/schemastery": "
|
|
70
|
+
"@deepseek-ai/schemastery": "~3.18.5",
|
|
75
71
|
"clsx": "^2.1.1"
|
|
76
72
|
},
|
|
77
73
|
"peerDependencies": {
|
|
78
|
-
"@deepseek-ai/dsh": ">=0.1
|
|
74
|
+
"@deepseek-ai/dsh": ">=0.2.1-rc.2",
|
|
79
75
|
"react": "^18.2.0",
|
|
80
76
|
"react-dom": "^18.2.0"
|
|
81
77
|
},
|
|
82
78
|
"devDependencies": {
|
|
83
|
-
"@deepseek-ai/cordis": "
|
|
84
|
-
"@deepseek-ai/dsh-agent": "^0.1
|
|
85
|
-
"@deepseek-ai/dsh-api-remotes": "^0.1
|
|
86
|
-
"@deepseek-ai/dsh-api-session-controller": "^0.1
|
|
87
|
-
"@deepseek-ai/dsh-api-workspace-controller": "^0.1
|
|
88
|
-
"@deepseek-ai/dsh-client-locale": "^0.1
|
|
89
|
-
"@deepseek-ai/dsh-client-store": "^0.1
|
|
90
|
-
"@deepseek-ai/dsh-client-ui-conversation": "^0.1
|
|
91
|
-
"@deepseek-ai/dsh-client-ui-renderer": "^0.1
|
|
92
|
-
"@deepseek-ai/dsh-client-ui-settings": "^0.1
|
|
93
|
-
"@deepseek-ai/dsh-client-ui-slots": "^0.1
|
|
94
|
-
"@deepseek-ai/dsh-client-ui-workspace": "^0.1
|
|
95
|
-
"@deepseek-ai/dsh-host-webserver": "^0.1
|
|
96
|
-
"@deepseek-ai/dsh-llm": "^0.1
|
|
97
|
-
"@deepseek-ai/dsh-scope": "^0.1
|
|
98
|
-
"@deepseek-ai/dsh-session": "^0.1
|
|
99
|
-
"@deepseek-ai/dsh-settings": "^0.1
|
|
100
|
-
"@deepseek-ai/dsh-subagent": "^0.1
|
|
101
|
-
"@deepseek-ai/dsh-timeout": "^0.1
|
|
102
|
-
"@deepseek-ai/dsh-tools": "^0.1
|
|
103
|
-
"@deepseek-ai/dsh-util-time": "^0.1
|
|
104
|
-
"@deepseek-ai/dsh-util-values": "^0.1
|
|
79
|
+
"@deepseek-ai/cordis": "~4.0.5",
|
|
80
|
+
"@deepseek-ai/dsh-agent": "^0.2.1-rc.2",
|
|
81
|
+
"@deepseek-ai/dsh-api-remotes": "^0.2.1-rc.2",
|
|
82
|
+
"@deepseek-ai/dsh-api-session-controller": "^0.2.1-rc.2",
|
|
83
|
+
"@deepseek-ai/dsh-api-workspace-controller": "^0.2.1-rc.2",
|
|
84
|
+
"@deepseek-ai/dsh-client-locale": "^0.2.1-rc.2",
|
|
85
|
+
"@deepseek-ai/dsh-client-store": "^0.2.1-rc.2",
|
|
86
|
+
"@deepseek-ai/dsh-client-ui-conversation": "^0.2.1-rc.2",
|
|
87
|
+
"@deepseek-ai/dsh-client-ui-renderer": "^0.2.1-rc.2",
|
|
88
|
+
"@deepseek-ai/dsh-client-ui-settings": "^0.2.1-rc.2",
|
|
89
|
+
"@deepseek-ai/dsh-client-ui-slots": "^0.2.1-rc.2",
|
|
90
|
+
"@deepseek-ai/dsh-client-ui-workspace": "^0.2.1-rc.2",
|
|
91
|
+
"@deepseek-ai/dsh-host-webserver": "^0.2.1-rc.2",
|
|
92
|
+
"@deepseek-ai/dsh-llm": "^0.2.1-rc.2",
|
|
93
|
+
"@deepseek-ai/dsh-scope": "^0.2.1-rc.2",
|
|
94
|
+
"@deepseek-ai/dsh-session": "^0.2.1-rc.2",
|
|
95
|
+
"@deepseek-ai/dsh-settings": "^0.2.1-rc.2",
|
|
96
|
+
"@deepseek-ai/dsh-subagent": "^0.2.1-rc.2",
|
|
97
|
+
"@deepseek-ai/dsh-timeout": "^0.2.1-rc.2",
|
|
98
|
+
"@deepseek-ai/dsh-tools": "^0.2.1-rc.2",
|
|
99
|
+
"@deepseek-ai/dsh-util-time": "^0.2.1-rc.2",
|
|
100
|
+
"@deepseek-ai/dsh-util-values": "^0.2.1-rc.2",
|
|
105
101
|
"@testing-library/dom": "^10.4.1",
|
|
106
102
|
"@testing-library/react": "^16.3.2",
|
|
107
103
|
"@types/node": "^22.20.0",
|
|
@@ -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) => ({
|
|
@@ -587,13 +588,15 @@ describe('PetSprite definition-driven render', () => {
|
|
|
587
588
|
removeListener: () => {},
|
|
588
589
|
dispatchEvent: () => false,
|
|
589
590
|
})
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
591
|
+
let now = 0
|
|
592
|
+
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
593
|
+
const timers: { at: number; callback: () => void }[] = []
|
|
594
|
+
let timerId = 0
|
|
595
|
+
vi.spyOn(window, 'setTimeout').mockImplementation(((callback: () => void, delay = 0) => {
|
|
596
|
+
timers.push({ at: now + delay, callback })
|
|
597
|
+
return ++timerId
|
|
598
|
+
}) as typeof window.setTimeout)
|
|
599
|
+
vi.spyOn(window, 'clearTimeout').mockImplementation(() => {})
|
|
597
600
|
const definition = petDefinition()
|
|
598
601
|
definition.sequences = {
|
|
599
602
|
thinking: ['running', 'waiting', 'running', 'waiting', 'running'],
|
|
@@ -604,7 +607,11 @@ describe('PetSprite definition-driven render', () => {
|
|
|
604
607
|
})
|
|
605
608
|
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
606
609
|
expect(sprite.style.backgroundPosition).toBe('0px -1120px')
|
|
607
|
-
|
|
610
|
+
// The loop sleeps for the frame's remaining time; jump the clock past the
|
|
611
|
+
// first item of the sequence (running frames are 250ms each) and fire the
|
|
612
|
+
// one pending tick.
|
|
613
|
+
now = 1_500
|
|
614
|
+
act(() => { for (const timer of timers.splice(0)) timer.callback() })
|
|
608
615
|
expect(sprite.style.backgroundPosition).toBe('0px -960px')
|
|
609
616
|
})
|
|
610
617
|
|
|
@@ -619,23 +626,30 @@ describe('PetSprite definition-driven render', () => {
|
|
|
619
626
|
removeListener: () => {},
|
|
620
627
|
dispatchEvent: () => false,
|
|
621
628
|
})
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
+
let now = 0
|
|
630
|
+
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
631
|
+
const timers: { at: number; callback: () => void }[] = []
|
|
632
|
+
let timerId = 0
|
|
633
|
+
vi.spyOn(window, 'setTimeout').mockImplementation(((callback: () => void, delay = 0) => {
|
|
634
|
+
timers.push({ at: now + delay, callback })
|
|
635
|
+
return ++timerId
|
|
636
|
+
}) as typeof window.setTimeout)
|
|
637
|
+
vi.spyOn(window, 'clearTimeout').mockImplementation(() => {})
|
|
629
638
|
renderPet({
|
|
630
639
|
snapshot: { ...snapshot, animation: 'idle', phase: 'idle' },
|
|
631
640
|
})
|
|
632
641
|
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
633
642
|
const styleSetterSpy = vi.spyOn(sprite.style, 'backgroundPosition', 'set')
|
|
634
|
-
// Idle frame 0 duration is 400ms. An intermediate tick at 16ms does not
|
|
635
|
-
|
|
643
|
+
// Idle frame 0 duration is 400ms. An intermediate tick at 16ms does not
|
|
644
|
+
// advance the frame, writes nothing, and the loop sleeps to the boundary
|
|
645
|
+
// (t=400) rather than polling every screen refresh.
|
|
646
|
+
now = 16
|
|
647
|
+
act(() => { for (const timer of timers.splice(0)) timer.callback() })
|
|
636
648
|
expect(styleSetterSpy).not.toHaveBeenCalled()
|
|
649
|
+
expect(timers[0]?.at).toBe(400)
|
|
637
650
|
// A tick past 400ms advances to frame 1 and should set backgroundPosition once.
|
|
638
|
-
|
|
651
|
+
now = 410
|
|
652
|
+
act(() => { for (const timer of timers.splice(0)) timer.callback() })
|
|
639
653
|
expect(styleSetterSpy).toHaveBeenCalledTimes(1)
|
|
640
654
|
})
|
|
641
655
|
})
|
|
@@ -814,8 +828,8 @@ describe('PetSprite status decoration (pet-center M5, #567)', () => {
|
|
|
814
828
|
})
|
|
815
829
|
let now = 0
|
|
816
830
|
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
817
|
-
// The ornament
|
|
818
|
-
//
|
|
831
|
+
// The ornament and the sprite both schedule by frame duration
|
|
832
|
+
// (setTimeout), so one capture and one stepping helper drive both loops.
|
|
819
833
|
const timers: { at: number; callback: () => void }[] = []
|
|
820
834
|
let timerId = 0
|
|
821
835
|
vi.spyOn(window, 'setTimeout').mockImplementation(((callback: () => void, delay = 0) => {
|
|
@@ -948,7 +962,12 @@ describe('PetSprite status decoration (pet-center M5, #567)', () => {
|
|
|
948
962
|
return frames.length
|
|
949
963
|
})
|
|
950
964
|
vi.spyOn(window, 'cancelAnimationFrame').mockImplementation(() => {})
|
|
951
|
-
|
|
965
|
+
// A custom visual keeps the sprite's own frame loop out of the timer
|
|
966
|
+
// capture, so a pending timer can only be the ornament's.
|
|
967
|
+
renderPet({
|
|
968
|
+
visual: <span data-testid="custom-visual" />,
|
|
969
|
+
snapshot: { ...snapshot, bubble: '完成', phase: 'done', decoration: { ...decoration, loop: false } },
|
|
970
|
+
})
|
|
952
971
|
const el = ornament()!
|
|
953
972
|
const step = (ms: number): void => {
|
|
954
973
|
for (const callback of frames.splice(0)) callback(now)
|
|
@@ -967,7 +986,7 @@ describe('PetSprite status decoration (pet-center M5, #567)', () => {
|
|
|
967
986
|
expect(el.style.backgroundPosition).toBe('-48px 0px')
|
|
968
987
|
act(() => { step(161) })
|
|
969
988
|
expect(el.style.backgroundPosition).toBe('-72px 0px')
|
|
970
|
-
// The ornament stopped scheduling timers
|
|
989
|
+
// The ornament stopped scheduling timers.
|
|
971
990
|
expect(timers).toHaveLength(0)
|
|
972
991
|
act(() => { step(161) })
|
|
973
992
|
// The last frame holds.
|
|
@@ -1000,7 +1019,9 @@ describe('PetSprite status decoration (pet-center M5, #567)', () => {
|
|
|
1000
1019
|
})
|
|
1001
1020
|
vi.spyOn(window, 'cancelAnimationFrame').mockImplementation(() => {})
|
|
1002
1021
|
// failed binds the single frame 3 of a 24px-wide frame; loop stays true.
|
|
1022
|
+
// The custom visual keeps the sprite's own frame loop out of the capture.
|
|
1003
1023
|
renderPet({
|
|
1024
|
+
visual: <span data-testid="custom-visual" />,
|
|
1004
1025
|
snapshot: {
|
|
1005
1026
|
...snapshot,
|
|
1006
1027
|
bubble: '失败',
|
|
@@ -1010,7 +1031,7 @@ describe('PetSprite status decoration (pet-center M5, #567)', () => {
|
|
|
1010
1031
|
})
|
|
1011
1032
|
const el = ornament()!
|
|
1012
1033
|
// The ornament settles on its only frame, exactly like the reduced-motion
|
|
1013
|
-
// hold — no timer may start
|
|
1034
|
+
// hold — no timer may start.
|
|
1014
1035
|
expect(el.style.backgroundPosition).toBe('-72px 0px')
|
|
1015
1036
|
expect(timers).toHaveLength(0)
|
|
1016
1037
|
const step = (ts: number): void => { for (const callback of frames.splice(0)) callback(ts) }
|
|
@@ -1399,3 +1420,218 @@ describe('PetSprite announcement bubbles from several publishers (issue #1812)',
|
|
|
1399
1420
|
})
|
|
1400
1421
|
})
|
|
1401
1422
|
|
|
1423
|
+
/**
|
|
1424
|
+
* Capture every timer the pet schedules onto a virtual clock. The sprite frame
|
|
1425
|
+
* loop sleeps until the next frame boundary (issue #17), so a test steps it by
|
|
1426
|
+
* moving the clock and firing whatever became due.
|
|
1427
|
+
*/
|
|
1428
|
+
function captureTimers(clock: () => number): { at: number; callback: () => void }[] {
|
|
1429
|
+
const timers: { at: number; callback: () => void }[] = []
|
|
1430
|
+
let timerId = 0
|
|
1431
|
+
vi.spyOn(window, 'setTimeout').mockImplementation(((callback: () => void, delay = 0) => {
|
|
1432
|
+
timers.push({ at: clock() + delay, callback })
|
|
1433
|
+
return ++timerId
|
|
1434
|
+
}) as typeof window.setTimeout)
|
|
1435
|
+
vi.spyOn(window, 'clearTimeout').mockImplementation(() => {})
|
|
1436
|
+
return timers
|
|
1437
|
+
}
|
|
1438
|
+
|
|
1439
|
+
/** Fire every captured timer once; each one may schedule the next wake. */
|
|
1440
|
+
function fireDue(timers: { at: number; callback: () => void }[]): void {
|
|
1441
|
+
for (const timer of timers.splice(0)) timer.callback()
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
describe('PetSprite render cost (issue #17)', () => {
|
|
1445
|
+
it('snaps the atlas grid and the sprite box to whole pixels', () => {
|
|
1446
|
+
renderPet()
|
|
1447
|
+
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
1448
|
+
// 192x208 cells at display.size 160 are 147.69px and 160px unrounded. The
|
|
1449
|
+
// box, the atlas grid and every frame offset share the whole-pixel cell,
|
|
1450
|
+
// so a frame change is a whole-pixel translate of the scaled atlas instead
|
|
1451
|
+
// of a fresh resample of the 1536x1872 source.
|
|
1452
|
+
expect(sprite.style.width).toBe('148px')
|
|
1453
|
+
expect(sprite.style.height).toBe('160px')
|
|
1454
|
+
expect(sprite.style.backgroundSize).toBe('1184px 1440px')
|
|
1455
|
+
})
|
|
1456
|
+
|
|
1457
|
+
it('advances frames on a duration timer instead of the screen refresh', () => {
|
|
1458
|
+
vi.spyOn(window, 'matchMedia').mockReturnValue({
|
|
1459
|
+
matches: false,
|
|
1460
|
+
media: '(prefers-reduced-motion: reduce)',
|
|
1461
|
+
onchange: null,
|
|
1462
|
+
addEventListener: () => {},
|
|
1463
|
+
removeEventListener: () => {},
|
|
1464
|
+
addListener: () => {},
|
|
1465
|
+
removeListener: () => {},
|
|
1466
|
+
dispatchEvent: () => false,
|
|
1467
|
+
})
|
|
1468
|
+
let now = 0
|
|
1469
|
+
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
1470
|
+
const timers = captureTimers(() => now)
|
|
1471
|
+
const raf = vi.spyOn(window, 'requestAnimationFrame')
|
|
1472
|
+
renderPet()
|
|
1473
|
+
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
1474
|
+
// Nothing drives the frame loop off the screen refresh any more.
|
|
1475
|
+
expect(raf).not.toHaveBeenCalled()
|
|
1476
|
+
// One pending wake: the loop's first tick, which then sleeps to the end of
|
|
1477
|
+
// the frame in play instead of re-arming for the next screen refresh.
|
|
1478
|
+
expect(timers).toHaveLength(1)
|
|
1479
|
+
act(() => { fireDue(timers) })
|
|
1480
|
+
expect(timers[0]?.at).toBe(400)
|
|
1481
|
+
now = 410
|
|
1482
|
+
act(() => { fireDue(timers) })
|
|
1483
|
+
expect(sprite.style.backgroundPosition).toBe('-148px 0px')
|
|
1484
|
+
// Idle frame 1 is already 10ms in, so the next wake is 390ms away.
|
|
1485
|
+
expect(timers[0]?.at).toBe(800)
|
|
1486
|
+
})
|
|
1487
|
+
|
|
1488
|
+
it('stops waking once a non-looping track holds its final frame', () => {
|
|
1489
|
+
vi.spyOn(window, 'matchMedia').mockReturnValue({
|
|
1490
|
+
matches: false,
|
|
1491
|
+
media: '(prefers-reduced-motion: reduce)',
|
|
1492
|
+
onchange: null,
|
|
1493
|
+
addEventListener: () => {},
|
|
1494
|
+
removeEventListener: () => {},
|
|
1495
|
+
addListener: () => {},
|
|
1496
|
+
removeListener: () => {},
|
|
1497
|
+
dispatchEvent: () => false,
|
|
1498
|
+
})
|
|
1499
|
+
let now = 0
|
|
1500
|
+
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
1501
|
+
const timers = captureTimers(() => now)
|
|
1502
|
+
renderPet({ snapshot: { ...snapshot, animation: 'failed', phase: 'failed' } })
|
|
1503
|
+
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
1504
|
+
// 'failed' is a non-looping 8-frame track at 450ms per frame; the 8th frame
|
|
1505
|
+
// holds once the host owns the switch to its fallback.
|
|
1506
|
+
now = 8 * 450
|
|
1507
|
+
act(() => { fireDue(timers) })
|
|
1508
|
+
expect(sprite.style.backgroundPosition).toBe('-' + (7 * 148) + 'px -' + (rowOf('failed') * 160) + 'px')
|
|
1509
|
+
// The track is settled: no timer is pending, so the pet stops waking the
|
|
1510
|
+
// main thread for a frame that cannot change.
|
|
1511
|
+
expect(timers).toHaveLength(0)
|
|
1512
|
+
const styleSetterSpy = vi.spyOn(sprite.style, 'backgroundPosition', 'set')
|
|
1513
|
+
now = 60_000
|
|
1514
|
+
act(() => { fireDue(timers) })
|
|
1515
|
+
expect(styleSetterSpy).not.toHaveBeenCalled()
|
|
1516
|
+
expect(timers).toHaveLength(0)
|
|
1517
|
+
})
|
|
1518
|
+
|
|
1519
|
+
it('repaints a settled frame when the sprite scale changes', () => {
|
|
1520
|
+
vi.spyOn(window, 'matchMedia').mockReturnValue({
|
|
1521
|
+
matches: false,
|
|
1522
|
+
media: '(prefers-reduced-motion: reduce)',
|
|
1523
|
+
onchange: null,
|
|
1524
|
+
addEventListener: () => {},
|
|
1525
|
+
removeEventListener: () => {},
|
|
1526
|
+
addListener: () => {},
|
|
1527
|
+
removeListener: () => {},
|
|
1528
|
+
dispatchEvent: () => false,
|
|
1529
|
+
})
|
|
1530
|
+
let now = 0
|
|
1531
|
+
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
1532
|
+
const timers = captureTimers(() => now)
|
|
1533
|
+
const definition = petDefinition()
|
|
1534
|
+
const display = snapshot.display
|
|
1535
|
+
const failed = { ...snapshot, animation: 'failed' as const, phase: 'failed' as const }
|
|
1536
|
+
const first = render(<PetSprite {...petProps({ definition, display, snapshot: failed })} />)
|
|
1537
|
+
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
1538
|
+
now = 8 * 450
|
|
1539
|
+
act(() => { fireDue(timers) })
|
|
1540
|
+
// A display.size change re-renders without restarting the track, and the
|
|
1541
|
+
// settled loop is woken by nothing: the held frame must be repainted at the
|
|
1542
|
+
// new scale or the box would show a neighbouring cell.
|
|
1543
|
+
first.rerender(<PetSprite {...petProps({ definition, display: { ...display, size: 120 }, snapshot: failed })} />)
|
|
1544
|
+
expect(sprite.style.width).toBe('111px')
|
|
1545
|
+
expect(sprite.style.height).toBe('120px')
|
|
1546
|
+
expect(sprite.style.backgroundPosition).toBe('-' + (7 * 111) + 'px -' + (rowOf('failed') * 120) + 'px')
|
|
1547
|
+
})
|
|
1548
|
+
})
|
|
1549
|
+
|
|
1550
|
+
describe('PetSprite roaming (issue #17)', () => {
|
|
1551
|
+
/** Drive the walk loop with a captured requestAnimationFrame. */
|
|
1552
|
+
function captureFrame(clock: () => number): { fire: () => void } {
|
|
1553
|
+
let pending: FrameRequestCallback | undefined
|
|
1554
|
+
vi.spyOn(window, 'requestAnimationFrame').mockImplementation(callback => {
|
|
1555
|
+
pending = callback
|
|
1556
|
+
return 1
|
|
1557
|
+
})
|
|
1558
|
+
vi.spyOn(window, 'cancelAnimationFrame').mockImplementation(() => {})
|
|
1559
|
+
return {
|
|
1560
|
+
fire: () => {
|
|
1561
|
+
const callback = pending
|
|
1562
|
+
pending = undefined
|
|
1563
|
+
callback?.(clock())
|
|
1564
|
+
},
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
it('writes the walk position to the element and renders only on landing', () => {
|
|
1569
|
+
let now = 0
|
|
1570
|
+
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
1571
|
+
const frame = captureFrame(() => now)
|
|
1572
|
+
const onDragEnd = vi.fn()
|
|
1573
|
+
const tSpy = vi.fn(t)
|
|
1574
|
+
const bus: GameplayBus = {}
|
|
1575
|
+
renderPet({ onDragEnd, t: tSpy, bus })
|
|
1576
|
+
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
1577
|
+
const float = sprite.parentElement!.parentElement as HTMLDivElement
|
|
1578
|
+
// Open the hover panel: every render then resolves panel copy through t, so
|
|
1579
|
+
// the t call count is a render probe the walk must not disturb.
|
|
1580
|
+
fireEvent.pointerOver(sprite)
|
|
1581
|
+
const renders = tSpy.mock.calls.length
|
|
1582
|
+
// 24 + 120 right, 60 px/s, so 2s of motion toward right = 144.
|
|
1583
|
+
expect(bus.walk?.('left', 120, 60)).toBe(120)
|
|
1584
|
+
now = 1_000
|
|
1585
|
+
act(() => { frame.fire() })
|
|
1586
|
+
expect(float.style.right).toBe('84px')
|
|
1587
|
+
expect(float.style.bottom).toBe('20px')
|
|
1588
|
+
// Mid-walk movement is a DOM write, not a React state update.
|
|
1589
|
+
expect(tSpy.mock.calls.length).toBe(renders)
|
|
1590
|
+
now = 2_000
|
|
1591
|
+
act(() => { frame.fire() })
|
|
1592
|
+
expect(float.style.right).toBe('144px')
|
|
1593
|
+
expect(onDragEnd).toHaveBeenCalledTimes(1)
|
|
1594
|
+
expect(onDragEnd).toHaveBeenCalledWith(144, 20)
|
|
1595
|
+
// The landing frame hands the spot to React state, which keeps rendering it.
|
|
1596
|
+
expect(tSpy.mock.calls.length).toBeGreaterThan(renders)
|
|
1597
|
+
expect(float.style.right).toBe('144px')
|
|
1598
|
+
})
|
|
1599
|
+
|
|
1600
|
+
it('keeps the walked position when a render lands mid-walk', () => {
|
|
1601
|
+
let now = 0
|
|
1602
|
+
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
1603
|
+
const frame = captureFrame(() => now)
|
|
1604
|
+
const definition = petDefinition()
|
|
1605
|
+
const display = snapshot.display
|
|
1606
|
+
const bus: GameplayBus = {}
|
|
1607
|
+
const first = render(<PetSprite {...petProps({ definition, display, bus })} />)
|
|
1608
|
+
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
1609
|
+
const float = sprite.parentElement!.parentElement as HTMLDivElement
|
|
1610
|
+
bus.walk?.('left', 120, 60)
|
|
1611
|
+
now = 1_000
|
|
1612
|
+
act(() => { frame.fire() })
|
|
1613
|
+
expect(float.style.right).toBe('84px')
|
|
1614
|
+
// A host poll re-renders the sprite mid-walk; the walked spot rides a ref,
|
|
1615
|
+
// so the sprite must not snap back to its saved position.
|
|
1616
|
+
first.rerender(<PetSprite {...petProps({ definition, display, bus })} />)
|
|
1617
|
+
expect(float.style.right).toBe('84px')
|
|
1618
|
+
})
|
|
1619
|
+
|
|
1620
|
+
it('starts a drag from the position the pet reached mid-walk', () => {
|
|
1621
|
+
let now = 0
|
|
1622
|
+
vi.spyOn(performance, 'now').mockImplementation(() => now)
|
|
1623
|
+
const frame = captureFrame(() => now)
|
|
1624
|
+
const onDragEnd = vi.fn()
|
|
1625
|
+
const bus: GameplayBus = {}
|
|
1626
|
+
renderPet({ onDragEnd, bus })
|
|
1627
|
+
const sprite = screen.getByRole('button', { name: '鲸鱼娘' })
|
|
1628
|
+
bus.walk?.('left', 120, 60)
|
|
1629
|
+
now = 1_000
|
|
1630
|
+
act(() => { frame.fire() })
|
|
1631
|
+
// Grab the pet where it actually is, not where the walk started.
|
|
1632
|
+
fireEvent.pointerDown(sprite, { clientX: 100, clientY: 100 })
|
|
1633
|
+
fireEvent.pointerUp(sprite)
|
|
1634
|
+
expect(onDragEnd).toHaveBeenCalledWith(84, 20)
|
|
1635
|
+
})
|
|
1636
|
+
})
|
|
1637
|
+
|