dance-channel.test.js
12.7 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Integration tests for dance.html's backend (T11 独立播出状态机, T12
// GET /api/dance/wardrobe, T15 POST /api/dance/pick, T17 点赞/在线人数).
// Same mock-H3-Console approach as dressup-generate.test.js so the full
// submit -> generate -> land-in-schedule path runs offline and fast.
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const os = require("node:os");
const http = require("node:http");
const { spawn } = require("node:child_process");
const PORT = 22934 + Math.floor(Math.random() * 1000);
const BASE = `http://localhost:${PORT}`;
const ADMIN_PASSWORD = "test-password-only-for-this-run";
let cookie;
let child;
let cwd;
let mockH3;
let mockH3Port;
let videoPollCounts;
function waitForServer(url, timeoutMs = 10000) {
const deadline = Date.now() + timeoutMs;
return (async function poll() {
while (Date.now() < deadline) {
try {
await fetch(url);
return;
} catch {
await new Promise((r) => setTimeout(r, 100));
}
}
throw new Error("server did not come up in time");
})();
}
function authed(opts = {}) {
return { ...opts, headers: { "Content-Type": "application/json", Cookie: cookie, ...opts.headers } };
}
// Same shape as dressup-generate.test.js's mock — enough of /v1/assets,
// /v1/videos and /v1/videos/:id for generateDanceVideo to run end to end.
function startMockH3() {
videoPollCounts = new Map();
let nextId = 1;
return new Promise((resolve) => {
mockH3 = http.createServer((req, res) => {
req.url = req.url.replace(/^\/+/, "/");
const chunks = [];
req.on("data", (c) => chunks.push(c));
req.on("end", () => {
if (req.method === "POST" && req.url === "/v1/assets") {
const id = `asset-${nextId++}`;
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ id, url: `http://mock-h3/asset/${id}`, kind: "image" }));
return;
}
if (req.method === "POST" && req.url === "/v1/videos") {
const id = `job-${nextId++}`;
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ id }));
return;
}
// /lib/video/:id's cache-warming (ensureVideoCached, server.js) hits
// this same mock's GET /v1/videos/:id/file — must not match the
// status-poll regex below, or its background requests get counted
// as extra "job polls" and trip the dedupe test's job-count assert.
if (req.method === "GET" && /^\/v1\/videos\/[^/]+\/file$/.test(req.url)) {
res.writeHead(200, { "Content-Type": "video/mp4" });
res.end("fake-mp4-bytes");
return;
}
const pollMatch = req.method === "GET" && req.url.match(/^\/v1\/videos\/([^/]+)$/);
if (pollMatch) {
const id = pollMatch[1];
const seen = (videoPollCounts.get(id) || 0) + 1;
videoPollCounts.set(id, seen);
if (seen < 2) {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ id, status: "generating" }));
return;
}
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ id, status: "done", partition: "ref2va", req: { width: 480, height: 832, frames: 240 } }));
return;
}
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "not found in mock" }));
});
});
mockH3.listen(0, () => {
mockH3Port = mockH3.address().port;
resolve();
});
});
}
test.before(async () => {
await startMockH3();
cwd = fs.mkdtempSync(path.join(os.tmpdir(), "dance-channel-"));
for (const f of ["server.js", "ingest.js", "uploads.js", "store.js", "dressup-store.js", "prompt-template.js", "db.js", "pricing.js"]) {
fs.copyFileSync(path.join(__dirname, f), path.join(cwd, f));
}
fs.mkdirSync(path.join(cwd, "uploads"), { recursive: true });
fs.writeFileSync(path.join(cwd, "uploads", "person.png"), "fake-png-bytes");
fs.writeFileSync(path.join(cwd, "uploads", "garment.png"), "fake-png-bytes");
fs.writeFileSync(path.join(cwd, "uploads", "ref.mp4"), "fake-mp4-bytes");
child = spawn(process.execPath, ["server.js"], {
cwd,
env: {
...process.env,
PORT: String(PORT),
H3_CONSOLE_BASE: `http://localhost:${mockH3Port}`,
H3_CONSOLE_TOKEN: "mock-token",
ADMIN_PASSWORD,
GENERATION_POLL_INTERVAL_MS: "400",
SCHEDULE_TICK_MS: "150",
},
stdio: "pipe",
});
await waitForServer(BASE);
const loginRes = await fetch(`${BASE}/api/admin/login`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ password: ADMIN_PASSWORD }),
});
cookie = (loginRes.headers.get("set-cookie") || "").split(";")[0];
});
test.after(() => {
child.kill();
mockH3.close();
fs.rmSync(cwd, { recursive: true, force: true });
});
async function waitUntil(fn, timeoutMs = 8000, intervalMs = 100) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const v = await fn();
if (v) return v;
await new Promise((r) => setTimeout(r, intervalMs));
}
throw new Error("condition not met in time");
}
let character, activeGarment, inactiveGarment, neverGeneratedGarment, danceRef;
test("seed: a character (its room) + active/inactive garments + dance-ref", async () => {
character = await fetch(
`${BASE}/api/admin/characters`,
authed({ method: "POST", body: JSON.stringify({ name: "衣柜小美", imageUrl: "/uploads/person.png", appearanceDescriptor: "black hair" }) })
).then((r) => r.json());
assert.ok(character.id);
activeGarment = await fetch(
`${BASE}/api/admin/garments`,
authed({ method: "POST", body: JSON.stringify({ name: "上架红裙", imageUrl: "/uploads/garment.png", appearanceDescriptor: "red dress", isActive: true }) })
).then((r) => r.json());
inactiveGarment = await fetch(
`${BASE}/api/admin/garments`,
authed({ method: "POST", body: JSON.stringify({ name: "下架旗袍", imageUrl: "/uploads/garment.png", appearanceDescriptor: "qipao", isActive: false }) })
).then((r) => r.json());
neverGeneratedGarment = await fetch(
`${BASE}/api/admin/garments`,
authed({ method: "POST", body: JSON.stringify({ name: "从没生成过", imageUrl: "/uploads/garment.png", appearanceDescriptor: "never rendered", isActive: true }) })
).then((r) => r.json());
danceRef = await fetch(
`${BASE}/api/admin/dance-refs`,
authed({ method: "POST", body: JSON.stringify({ name: "衣柜拉丁舞", videoUrl: "/uploads/ref.mp4", motionDescriptor: "hip sways" }) })
).then((r) => r.json());
assert.ok(danceRef.id);
});
test("T15 POST /api/dance/pick: rejects unknown characterId/garmentId, and inactive garments", async () => {
const badCharacter = await fetch(`${BASE}/api/dance/pick`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ characterId: "nope", garmentId: activeGarment.id }),
});
assert.equal(badCharacter.status, 400);
const missingGarment = await fetch(`${BASE}/api/dance/pick`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ characterId: character.id, garmentId: "nope" }),
});
assert.equal(missingGarment.status, 400);
const inactive = await fetch(`${BASE}/api/dance/pick`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ characterId: character.id, garmentId: inactiveGarment.id }),
});
assert.equal(inactive.status, 400);
});
test("T15/T11 pick a garment for this character's room (no login/cookie needed) -> generates -> lands in that room's schedule; T17 vote + viewer count along the way", async () => {
const pickRes = await fetch(`${BASE}/api/dance/pick`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ characterId: character.id, garmentId: activeGarment.id }),
});
assert.equal(pickRes.status, 200);
const picked = await pickRes.json();
assert.equal(picked.status, "generating");
assert.ok(picked.id);
// While it's still generating, it should show up in this room's public
// queue and accept a vote (T17) — separate viewer/vote namespace per room.
const stateWhileGenerating = await fetch(`${BASE}/api/dance/live/state?characterId=${character.id}&cid=test-cid-1`).then((r) => r.json());
assert.ok(stateWhileGenerating.queue.some((q) => q.id === picked.id && q.status === "generating"));
assert.equal(stateWhileGenerating.watching, 1);
const voteRes = await fetch(`${BASE}/api/dance/queue/vote/${picked.id}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ uid: "viewer-1" }),
});
assert.equal(voteRes.status, 200);
const vote = await voteRes.json();
assert.equal(vote.voted, true);
assert.equal(vote.votes, 1);
// Eventually the mock generation finishes and this room's ensureRoomSchedule
// sweeps it into the schedule — current or upNext should carry the right
// character/garment traceability, and the entry should drop out of queue.
const finalState = await waitUntil(async () => {
const state = await fetch(`${BASE}/api/dance/live/state?characterId=${character.id}&cid=test-cid-1`).then((r) => r.json());
const inQueue = state.queue.some((q) => q.id === picked.id);
const landed = (state.current && state.current.clip && state.current.clip.garmentId === activeGarment.id) || state.upNext.some((c) => c.garmentId === activeGarment.id);
return !inQueue && landed ? state : null;
});
const landedClip = finalState.current && finalState.current.clip && finalState.current.clip.garmentId === activeGarment.id ? finalState.current.clip : finalState.upNext.find((c) => c.garmentId === activeGarment.id);
assert.equal(landedClip.characterId, character.id);
assert.equal(landedClip.refDanceVideoId, danceRef.id);
assert.equal(landedClip.channel, "dance");
});
test("T15 dedupe: re-picking the same garment returns ready immediately (M.7 cache hit), no new H3 submission", async () => {
const before = videoPollCounts.size;
const res = await fetch(`${BASE}/api/dance/pick`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ characterId: character.id, garmentId: activeGarment.id }),
});
assert.equal(res.status, 200);
const data = await res.json();
assert.equal(data.status, "ready");
assert.ok(data.videoId);
assert.equal(videoPollCounts.size, before); // no new /v1/videos/:id job started
});
// T12's contract changed with the one-room-per-character redesign: the
// wardrobe is no longer "every active garment" (a request-anything catalog)
// — it's "which of this character's already-rendered garments can a viewer
// queue up next", so it only ever lists (characterId, garmentId) pairs with
// an existing dance clip (same M.7 index /api/dance/pick's dedupe uses).
test("GET /api/dance/wardrobe?characterId=: only THIS character's already-generated, active garments, no admin auth needed", async () => {
const res = await fetch(`${BASE}/api/dance/wardrobe?characterId=${character.id}`); // deliberately no cookie
assert.equal(res.status, 200);
const list = await res.json();
assert.ok(list.some((g) => g.id === activeGarment.id), "has a generated video for this character -> listed");
assert.ok(!list.some((g) => g.id === inactiveGarment.id), "inactive -> never listed even if rendered");
assert.ok(!list.some((g) => g.id === neverGeneratedGarment.id), "no video for this character yet -> not listed");
const missingCharacterId = await fetch(`${BASE}/api/dance/wardrobe`);
assert.equal(missingCharacterId.status, 400);
});
test("T17: two different characters' rooms keep independent viewer counts, and live.html's is separate again", async () => {
const otherCharacter = await fetch(
`${BASE}/api/admin/characters`,
authed({ method: "POST", body: JSON.stringify({ name: "另一个房间", imageUrl: "/uploads/person.png", appearanceDescriptor: "black hair" }) })
).then((r) => r.json());
await fetch(`${BASE}/api/live/state?cid=live-viewer-a`);
await fetch(`${BASE}/api/live/state?cid=live-viewer-b`);
const danceState = await fetch(`${BASE}/api/dance/live/state?characterId=${character.id}&cid=dance-viewer-only`).then((r) => r.json());
assert.equal(danceState.watching, 2); // test-cid-1 (still within TTL) + dance-viewer-only, not the two live viewers
const otherRoomState = await fetch(`${BASE}/api/dance/live/state?characterId=${otherCharacter.id}&cid=other-room-viewer`).then((r) => r.json());
assert.equal(otherRoomState.watching, 1); // this room has never seen character's viewers
});