-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdance-tip.test.js
More file actions
262 lines (239 loc) · 8.6 KB
/
dance-tip.test.js
File metadata and controls
262 lines (239 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Tests for /api/x402/dance-tip — the Pole Club tip endpoint.
//
// Pure-logic only — we exercise the exported helpers (STYLES, pickStyle,
// pickDancer, buildTicket, BAZAAR_SCHEMA) rather than the paidEndpoint HTTP
// wrapper. That keeps tests off the network and off the database while still
// covering everything the wire format depends on.
//
// Coverage:
// • STYLES keys + shape (free-floor single-clip vs. pole sequence styles)
// • pickStyle resolves both shapes and rejects unknown styles
// • pickDancer enforces the slot allowlist
// • buildTicket emits `sequence` for sequence styles and omits it otherwise
// • The bazaar schema advertises the new `dance` enum values + sequence shape
import { describe, it, expect } from 'vitest';
import {
STYLES,
pickStyle,
pickDancer,
buildTicket,
BAZAAR_SCHEMA,
} from '../../api/x402/dance-tip.js';
const FREE_FLOOR_KEYS = ['hiphop', 'rumba', 'silly', 'thriller', 'capoeira'];
const POLE_KEYS = ['spin', 'climb', 'combo'];
describe('STYLES — registry', () => {
it('keeps the existing free-floor single-clip styles', () => {
for (const key of FREE_FLOOR_KEYS) {
const style = STYLES[key];
expect(style, `${key} missing from STYLES`).toBeTruthy();
expect(typeof style.clip).toBe('string');
expect(style.loop).toBe(true);
expect(typeof style.durationSec).toBe('number');
expect(style.sequence).toBeUndefined();
}
});
it('declares the three pole choreography sequence styles', () => {
for (const key of POLE_KEYS) {
const style = STYLES[key];
expect(style, `${key} missing from STYLES`).toBeTruthy();
expect(Array.isArray(style.sequence)).toBe(true);
expect(style.sequence.length).toBeGreaterThan(0);
expect(style.track).toBe('pole');
expect(typeof style.label).toBe('string');
// Each step must name a clip + a positive duration so playSequence
// has something to crossfade to and a non-zero sleep between steps.
for (const step of style.sequence) {
expect(typeof step.clip).toBe('string');
expect(step.clip.length).toBeGreaterThan(0);
expect(step.durationSec).toBeGreaterThan(0);
}
}
});
it('spin sequence matches the documented (pole-spin, pole-bow) chain', () => {
expect(STYLES.spin.sequence).toEqual([
{ clip: 'pole-spin', durationSec: 8 },
{ clip: 'pole-bow', durationSec: 2 },
]);
expect(STYLES.spin.durationSec).toBe(10);
});
it('climb sequence matches (pole-climb, pole-invert, pole-bow)', () => {
expect(STYLES.climb.sequence).toEqual([
{ clip: 'pole-climb', durationSec: 5 },
{ clip: 'pole-invert', durationSec: 6 },
{ clip: 'pole-bow', durationSec: 2 },
]);
expect(STYLES.climb.durationSec).toBe(13);
});
it('combo sequence chains spin → climb → invert → floorwork → bow', () => {
expect(STYLES.combo.sequence.map((s) => s.clip)).toEqual([
'pole-spin',
'pole-climb',
'pole-invert',
'pole-floorwork',
'pole-bow',
]);
expect(STYLES.combo.durationSec).toBe(18);
// Sum of step durations matches the advertised total.
const total = STYLES.combo.sequence.reduce((acc, s) => acc + s.durationSec, 0);
expect(total).toBe(18);
});
it('STYLES is frozen so the bazaar enum cannot drift at runtime', () => {
expect(Object.isFrozen(STYLES)).toBe(true);
});
});
describe('pickStyle', () => {
it('returns a normalized descriptor for a free-floor style', () => {
const s = pickStyle('rumba');
expect(s.key).toBe('rumba');
expect(s.clip).toBe('rumba');
expect(s.loop).toBe(true);
expect(s.durationSec).toBe(14);
expect(s.track).toBe('rumba');
expect(s.sequence).toBeUndefined();
});
it('returns sequence + first-step clip for a pole sequence style', () => {
const s = pickStyle('spin');
expect(s.key).toBe('spin');
expect(s.clip).toBe('pole-spin'); // first step lifted as legacy `clip`
expect(s.loop).toBe(false);
expect(s.track).toBe('pole');
expect(s.sequence).toEqual([
{ clip: 'pole-spin', durationSec: 8 },
{ clip: 'pole-bow', durationSec: 2 },
]);
});
it('lowercases + trims user input before lookup', () => {
expect(pickStyle(' SPIN ').key).toBe('spin');
expect(pickStyle('Rumba').key).toBe('rumba');
});
it('throws unknown_dance with status=400 for unregistered names', () => {
const calls = [
() => pickStyle('breakdance'),
() => pickStyle(''),
() => pickStyle(null),
() => pickStyle(undefined),
];
for (const fn of calls) {
let caught;
try { fn(); } catch (e) { caught = e; }
expect(caught).toBeInstanceOf(Error);
expect(caught.status).toBe(400);
expect(caught.code).toBe('unknown_dance');
// The error message enumerates valid styles so the caller can recover.
expect(caught.message).toMatch(/spin/);
expect(caught.message).toMatch(/rumba/);
}
});
});
describe('pickDancer', () => {
it('accepts the four stage slot ids', () => {
for (const id of ['1', '2', '3', '4']) {
expect(pickDancer(id)).toBe(id);
}
});
it('rejects anything outside the allowlist with status=400', () => {
const bad = ['0', '5', 'one', '', null, undefined, ' '];
for (const v of bad) {
let caught;
try { pickDancer(v); } catch (e) { caught = e; }
expect(caught, `expected throw for ${JSON.stringify(v)}`).toBeInstanceOf(Error);
expect(caught.status).toBe(400);
expect(caught.code).toBe('unknown_dancer');
}
});
it('trims numeric strings before validation', () => {
expect(pickDancer(' 2 ')).toBe('2');
});
});
describe('buildTicket', () => {
const REQUIREMENT = {
network: 'solana',
amount: '1000',
asset: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
};
it('emits a single-clip ticket without a sequence field for free-floor styles', () => {
const style = pickStyle('rumba');
const now = new Date('2026-05-21T00:00:00.000Z');
const ticket = buildTicket({
dancer: '2',
style,
now,
payer: 'wwwPqsM4',
requirement: REQUIREMENT,
ticketId: 'tkt-1',
});
expect(ticket.ok).toBe(true);
expect(ticket.ticketId).toBe('tkt-1');
expect(ticket.dance).toBe('rumba');
expect(ticket.clip).toBe('rumba');
expect(ticket.loop).toBe(true);
expect(ticket.durationSec).toBe(14);
expect(ticket.track).toBe('rumba');
expect(ticket.sequence).toBeUndefined();
expect(ticket.startsAt).toBe('2026-05-21T00:00:00.000Z');
expect(ticket.endsAt).toBe('2026-05-21T00:00:14.000Z');
expect(ticket.network).toBe('solana');
expect(ticket.amountAtomics).toBe('1000');
});
it('emits a sequence ticket for pole styles with the chain attached', () => {
const style = pickStyle('climb');
const ticket = buildTicket({
dancer: '3',
style,
now: new Date('2026-05-21T00:00:00.000Z'),
payer: 'wwwPqsM4',
requirement: REQUIREMENT,
ticketId: 'tkt-2',
});
expect(ticket.dance).toBe('climb');
// `clip` carries the first step's clip for legacy consumers + the
// club_tips ledger row.
expect(ticket.clip).toBe('pole-climb');
expect(ticket.loop).toBe(false);
expect(ticket.durationSec).toBe(13);
expect(ticket.track).toBe('pole');
expect(ticket.sequence).toEqual([
{ clip: 'pole-climb', durationSec: 5 },
{ clip: 'pole-invert', durationSec: 6 },
{ clip: 'pole-bow', durationSec: 2 },
]);
expect(ticket.endsAt).toBe('2026-05-21T00:00:13.000Z');
});
it('defaults payer + requirement fields to null when absent', () => {
const ticket = buildTicket({
dancer: '1',
style: pickStyle('hiphop'),
ticketId: 'tkt-3',
});
expect(ticket.payer).toBe(null);
expect(ticket.network).toBe(null);
expect(ticket.amountAtomics).toBe(null);
expect(ticket.asset).toBe(null);
});
});
describe('Bazaar discovery schema', () => {
it('marks the endpoint discoverable', () => {
expect(BAZAAR_SCHEMA.discoverable).toBe(true);
});
it("advertises every STYLES key in the input `dance` enum", () => {
// The bazaar schema wraps the inner queryParams JSON Schema — locate
// the `dance` property however the wrapper nests it.
const wireSchema = JSON.stringify(BAZAAR_SCHEMA.schema);
for (const key of [...FREE_FLOOR_KEYS, ...POLE_KEYS]) {
expect(wireSchema, `dance enum missing "${key}"`).toContain(`"${key}"`);
}
});
it('advertises the sequence ticket shape (clip + durationSec items)', () => {
const wireSchema = JSON.stringify(BAZAAR_SCHEMA.schema);
// The output schema lists a `sequence` array of {clip, durationSec}.
expect(wireSchema).toContain('"sequence"');
expect(wireSchema).toContain('"clip"');
expect(wireSchema).toContain('"durationSec"');
});
it('exposes a sequence-style OUTPUT_EXAMPLE so bazaar clients see the shape', () => {
const example = BAZAAR_SCHEMA.info.output.example;
expect(example.ok).toBe(true);
expect(Array.isArray(example.sequence)).toBe(true);
expect(example.sequence.length).toBeGreaterThan(0);
});
});