Files
Piik/tests/browser-encoding-pool.test.ts
AAA 1b9f5bd2a2 feat: add room interactions and media sharing improvements
## Problem and outcome / 问题与结果

This phase adds lightweight room interaction without making media depend on chat,
and adds Windows source-audio exclusion and optional SFU-only hosting at their
existing owners. It also corrects confirmed encoder/capture ownership and native
color-conversion defects. Host microphone commentary remains on the shared media
path; room voice and 4K are outside this phase.

## Release notes

### 中文

这是一次功能更新:Piik 加入了文字聊天与表情互动,并完善了 Windows 声音采集、画面颜色和自部署选项。

#### 新功能

- **边看边聊。** 在可移动、可缩放的聊天窗口中发送消息,或开启弹幕。停止分享后,房间中的朋友仍可继续聊天。
- **表情互动。** 点击沙发上的人物,给自己发表情,或把番茄等表情送给朋友;各端按同一事件显示动画。
- **排除应用声音。** Windows App 分享整个屏幕时,可排除一个应用及其子进程的声音,适合同时使用语音软件。所选应用退出后,来源声音停止,避免恢复为未过滤的系统声音。
- **可选的纯 SFU 模式。** 自部署管理员可让共享画面和声音统一通过媒体服务器转发。默认仍为优先 P2P;App 公网邀请保持纯 P2P。
- **Windows HDR 转 SDR。** 原生采集与缩略图自动将 HDR 来源转换为 SDR,沿用现有观看链路。

#### 修复与改进

- 修复原生颜色转换的范围与矩阵处理,避免已复现的暗部丢失、高光裁切和颜色偏差。
- 修复共享编码器切换时使用过期需求或过期帧信息的问题,保护正在观看的画面;修复换源后旧采集事件可能影响新来源的问题。
- 统一新控件、浮窗与来源选择文案,补充中英文使用说明。

#### 使用与升级

聊天与表情需要新版站点页面和 Server;声音排除需要新版页面与支持此能力的 Windows App。聊天只保留本页最近收到的消息,刷新后清空,不补发历史,且不提供端到端加密。

HDR 转换输出为 SDR,不代表端到端 HDR 或多声道支持。既有房主麦克风与立体声能力保留。

### English

This feature update brings text chat and reactions to Piik, alongside improvements to Windows audio capture, picture colors and self-hosting options.

#### New features

- **Chat while watching.** Send messages in a movable, resizable window, or enable on-screen chat. Friends can keep chatting after the host stops sharing.
- **Reactions.** Select a person on the sofa to react above yourself or send a tomato and other reactions to a friend. Participants animate the same shared event.
- **Exclude application audio.** When sharing a whole screen with Windows App, exclude one application and its child processes, useful when using a separate voice app. Source audio stops if the selected app exits, preventing a return to unfiltered system sound.
- **Optional SFU-only hosting.** Site operators can require shared video and audio to pass through the media server. P2P-first remains the default; App Public invite remains P2P-only.
- **Windows HDR-to-SDR conversion.** Native capture and source thumbnails automatically convert HDR sources to SDR for the existing viewing path.

#### Fixes and improvements

- Correct native color-conversion ranges and matrices, addressing reproduced crushed shadows, clipped highlights and color shifts.
- Correct stale demand and frame evidence during shared-encoder handoffs to protect the current picture. Prevent events from a replaced capture from affecting its successor.
- Align new controls, floating panels and source-picker copy, with updated Chinese and English guides.

#### Using this update

Chat and reactions require updated site pages and Server. Audio exclusion requires updated pages and a Windows App that supports the capability. Chat retains only recent messages received by the current page, clears on refresh and does not replay history. It is not end-to-end encrypted.

HDR conversion produces SDR; it does not add end-to-end HDR or surround sound. Existing host microphone commentary and stereo support remain available.

## Verification / 验证

- Web type checks, 78 test files / 1275 tests and production build: PASS.
- Go core tests, formatting/vet and target builds: PASS; Darwin cgo remains platform-limited on this Windows runner.
- Native build/check/color probes and synthetic HDR-to-SDR H.264/VP8 browser reception: PASS.
- Access/privacy gate, Native H.264 SFU-only media gate and local Auto/H.264 Host gate: PASS.
- Three real production pages: chat/reactions, stop/resume, idle-room join, host departure/return, offline draft recovery and invitation revocation: PASS.
- Synthetic parent/child process-audio exclusion and target lifetime: PASS; no raw audio retained.
- Windows candidate archive extraction and packaged Local startup/shutdown: PASS.
- Documentation and website build: PASS.
- [Candidate CI](https://github.com/TNTcraftHIM/Piik/actions/runs/36873766646) at
  `f26cd208d03bc0dcbe353f8cac217f993216b0a3`: Go core and race checks, Web tests,
  Server/container checks and Windows/macOS/Linux App packaging all PASS.
  Publication was skipped as intended.

The owner accepted the UI/model and bounded local walkthrough for phase closure.
Physical HDR/mixed displays, mobile keyboard,
real voice applications across output devices and macOS/Linux capture remain
unverified. Existing field reports without matched evidence are not claimed
resolved. The owner authorized v1.7.0 publication on 2026-10-02. The final
documentation-only commit records that authorization; product code matches
the verified candidate above.

## Impact / 影响

Authorized version: **v1.7.0**, matching the local release planner. Current wire
contracts remain Browser/server 23, Native control 9 and capture 7. Optional
capabilities gate new functionality; existing room/media authority remains owned
by the existing services. No new ports, message persistence, room voice or
automatic room creation on page visit. Updated README, guides, standards,
research evidence and TODO stay with the candidate phase.
2026-10-02 01:36:45 +08:00

551 lines
22 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { BrowserEncodingPool } from "../src/client/media/browser-encoding-pool";
import { BrowserEncodingProducer } from "../src/client/media/browser-encoding-producer";
import type { BrowserEncodingOutput } from "../src/client/media/browser-encoding-output";
import { QUALITY_PROFILES } from "../src/client/media/quality";
const codec = { mimeType: "video/VP8", clockRate: 90_000 };
const profile = QUALITY_PROFILES["1080p30"];
const source = { kind: "video", readyState: "live", enabled: true, id: "source" } as MediaStreamTrack;
const producers = new Map<BrowserEncodingProducer, {
budget: number; bitrate: number; bytes: number; frames: number; fps: number; width: number; height: number; reason: string;
}>();
let pool: BrowserEncodingPool;
function stats(row: Record<string, unknown>): RTCStatsReport {
return new Map([
["video", { id: "video", timestamp: Date.now(), type: "outbound-rtp", kind: "video", codecId: "codec", ...row }],
["codec", { id: "codec", timestamp: Date.now(), type: "codec", ...codec }],
]) as unknown as RTCStatsReport;
}
function member(budget = 2_000_000) {
const demand = { budget };
let producerId: string | null = null;
const select = vi.fn((id: string, _requestKey: () => Promise<void>, selected: () => void,
accept: (metadata: RTCEncodedVideoFrameMetadata) => boolean) => {
const [, output] = [...producers].find(([producer]) => producer.id === id)!;
if (!accept({ width: output.width, height: output.height })) return;
producerId = id;
selected();
});
const cancelSelection = vi.fn(() => true);
const encoded = {
select, cancelSelection, push: vi.fn(), passthrough: vi.fn(), setPaused: vi.fn(),
snapshot: () => ({ frames: producerId ? 30 : 0, width: 1920, height: 1080, timestamp: Date.now(), lastProducerId: producerId }),
} as unknown as BrowserEncodingOutput;
const connection = { connectionState: "connected", getStats: async () => stats({ targetBitrate: demand.budget }) };
const fatal = vi.fn();
const handle = pool.create(source, {} as RTCRtpSender, connection as RTCPeerConnection, profile,
encoded, async () => true, async () => undefined, fatal)!;
return { demand, select, cancelSelection, fatal, handle, producerId: () => producerId };
}
async function sample(count = 1) {
await vi.advanceTimersByTimeAsync(500 * count);
}
function holdNextProducerStart(): () => void {
let ready!: () => void;
const start = vi.mocked(BrowserEncodingProducer.prototype.start).getMockImplementation()!;
vi.mocked(BrowserEncodingProducer.prototype.start).mockImplementationOnce(async function (this: BrowserEncodingProducer, budget) {
await start.call(this, budget);
await new Promise<void>((resolve) => { ready = resolve; });
});
return () => ready();
}
beforeEach(() => {
vi.useFakeTimers();
vi.stubGlobal("RTCRtpSender", { getCapabilities: () => ({ codecs: [codec] }) });
producers.clear();
vi.spyOn(BrowserEncodingProducer.prototype, "start").mockImplementation(async function (this: BrowserEncodingProducer, budget) {
producers.set(this, { budget: budget!, bitrate: budget! / 2, bytes: 0, frames: 0, fps: 30,
width: 1920, height: 1080, reason: "none" });
});
vi.spyOn(BrowserEncodingProducer.prototype, "report").mockImplementation(async function (this: BrowserEncodingProducer) {
const output = producers.get(this)!;
output.bytes += output.bitrate / 16;
output.frames += output.fps / 2;
return stats({ bytesSent: output.bytes, framesEncoded: output.frames, frameWidth: output.width, frameHeight: output.height,
qualityLimitationReason: output.reason });
});
vi.spyOn(BrowserEncodingProducer.prototype, "update").mockImplementation(async function (this: BrowserEncodingProducer, _profile, budget) {
producers.get(this)!.budget = budget!;
});
vi.spyOn(BrowserEncodingProducer.prototype, "setPaused").mockImplementation(() => undefined);
vi.spyOn(BrowserEncodingProducer.prototype, "dispose").mockImplementation(() => undefined);
pool = new BrowserEncodingPool();
});
afterEach(() => {
pool.dispose();
vi.restoreAllMocks();
vi.unstubAllGlobals();
vi.useRealTimers();
});
describe("Browser encoding pool ownership", () => {
it("pauses a shared producer only when all of its members pause", async () => {
const first = member(), second = member();
await sample(4);
expect(producers.size).toBe(1);
const producer = [...producers.keys()][0]!;
first.handle.setPaused(true);
expect(producer.setPaused).toHaveBeenLastCalledWith(false);
second.handle.setPaused(true);
expect(producer.setPaused).toHaveBeenLastCalledWith(true);
first.handle.setPaused(false);
expect(producer.setPaused).toHaveBeenLastCalledWith(false);
expect(first.producerId()).toBe(second.producerId());
expect(producer.dispose).not.toHaveBeenCalled();
});
it("selects a prepared producer without waiting for another budget sample", async () => {
const ready = holdNextProducerStart();
const first = member();
await sample();
expect(first.select).not.toHaveBeenCalled();
ready();
await vi.advanceTimersByTimeAsync(0);
expect(first.producerId()).toBe([...producers.keys()][0]!.id);
});
it("keeps a shared encoder through a bitrate spike with unchanged native demand", async () => {
const first = member(), second = member();
await sample(4);
expect(producers.size).toBe(1);
const [producer, output] = [...producers][0];
expect(first.producerId()).toBe(producer.id);
expect(second.producerId()).toBe(producer.id);
output.bitrate = output.budget + 16;
await sample();
output.bitrate = output.budget / 2;
await sample(2);
expect(producers.size).toBe(1);
expect(first.select).toHaveBeenCalledTimes(1);
expect(second.select).toHaveBeenCalledTimes(1);
expect(producer.dispose).not.toHaveBeenCalled();
});
it("updates shared demand in place when all children need a lower rate", async () => {
const first = member(), second = member();
await sample(4);
const [producer, output] = [...producers][0];
first.demand.budget = second.demand.budget = 500_000;
await sample(2);
expect(producers.size).toBe(1);
expect(output.budget).toBe(500_000);
expect(producer.update).toHaveBeenCalledWith(profile, 500_000);
expect(first.select).toHaveBeenCalledTimes(1);
expect(second.select).toHaveBeenCalledTimes(1);
});
it("gives a weaker child its own rate owner and reunites it after recovery", async () => {
const strong = member(), weak = member();
await sample(4);
const [original, output] = [...producers][0];
weak.demand.budget = 500_000;
await sample(3);
expect(producers.size).toBe(2);
expect(strong.producerId()).toBe(original.id);
expect(weak.producerId()).not.toBe(original.id);
expect(output.budget).toBe(2_000_000);
expect(strong.select).toHaveBeenCalledTimes(1);
weak.demand.budget = 2_000_000;
await sample(3);
expect(weak.producerId()).toBe(original.id);
expect(producers.size).toBe(2);
expect(strong.fatal).not.toHaveBeenCalled();
expect(weak.fatal).not.toHaveBeenCalled();
});
it("does not move a healthy child to a lower-budget output with briefly higher FPS", async () => {
const strong = member(), weak = member();
await sample(4);
const [original, originalOutput] = [...producers][0];
weak.demand.budget = 500_000;
await sample(3);
const [candidate, candidateOutput] = [...producers][1];
originalOutput.fps = 27;
candidateOutput.fps = 29;
await sample(4);
expect(strong.producerId()).toBe(original.id);
expect(weak.producerId()).toBe(candidate.id);
expect(strong.select).toHaveBeenCalledTimes(1);
expect(originalOutput.budget).toBe(2_000_000);
expect(candidateOutput.budget).toBe(500_000);
});
it("keeps unequal rate owners through scene changes until native demand recovers", async () => {
const strong = member(), weak = member();
await sample(4);
const [original, originalOutput] = [...producers][0];
weak.demand.budget = 500_000;
await sample(3);
const [lower] = [...producers][1];
// A quiet scene fits both paths, but the shared encoder is still free to
// consume the stronger child's allocation when motion returns.
for (const bitrate of [250_000, 1_000_000, 250_000, 1_000_000]) {
originalOutput.bitrate = bitrate;
await sample(3);
expect(weak.producerId()).toBe(lower.id);
expect(strong.producerId()).toBe(original.id);
}
expect(producers.size).toBe(2);
expect(weak.select).toHaveBeenCalledTimes(2);
weak.demand.budget = 2_000_000;
await sample(3);
expect(weak.producerId()).toBe(original.id);
});
it("does not admit a new weak child to a quiet higher-rate producer", async () => {
const strong = member();
await sample(4);
const [original, output] = [...producers][0];
output.bitrate = 250_000;
await sample(2);
const weak = member(500_000);
await sample(4);
expect(strong.producerId()).toBe(original.id);
expect(weak.producerId()).not.toBe(original.id);
expect(producers.size).toBe(2);
expect([...producers.values()][1].budget).toBe(500_000);
});
it("keeps an existing weaker child's encoder when a stronger child joins", async () => {
const weak = member(1_000_000);
await sample(4);
const [original, output] = [...producers][0];
const strong = member(2_000_000);
await sample(4);
output.bitrate = 1_500_000;
await sample(2);
expect(weak.producerId()).toBe(original.id);
expect(strong.producerId()).not.toBe(original.id);
expect(output.budget).toBe(1_000_000);
expect(producers.size).toBe(2);
});
it.each(["rising newcomer", "falling incumbent"])("uses current demand for optional reuse with a %s", async (change) => {
const first = member(1_000_000), second = member(2_000_000);
await sample(4);
const [[original, originalOutput], [candidate, candidateOutput]] = [...producers];
originalOutput.fps = 27;
if (change === "falling incumbent") {
candidateOutput.reason = "bandwidth";
first.demand.budget = 2_000_000;
await sample(2);
second.demand.budget = 1_000_000;
} else first.demand.budget = 3_000_000;
candidateOutput.fps = 30;
candidateOutput.reason = "none";
candidateOutput.bitrate = second.demand.budget + 100_000;
await sample(4);
expect(first.producerId()).toBe(original.id);
expect(second.producerId()).toBe(candidate.id);
expect(originalOutput.budget).toBe(first.demand.budget);
expect(candidateOutput.budget).toBe(second.demand.budget);
expect(producers.size).toBe(2);
});
it("shares a group's current demand while its previous budget is being reduced", async () => {
const first = member(2_000_000);
await sample(4);
const [original, output] = [...producers][0];
first.demand.budget = 1_000_000;
const second = member(1_000_000);
await sample(4);
expect(first.producerId()).toBe(original.id);
expect(second.producerId()).toBe(original.id);
expect(output.budget).toBe(1_000_000);
expect(producers.size).toBe(1);
});
it.each(["preparation", "recovery frame"])("keeps a necessary downgrade through quiet output during %s", async (stage) => {
const strong = member(), weak = member();
await sample(4);
const [original, originalOutput] = [...producers][0];
let resume!: () => void;
if (stage === "preparation") resume = holdNextProducerStart();
else {
const select = weak.select.getMockImplementation()!;
weak.select.mockImplementationOnce((...args) => { resume = () => select(...args); });
}
weak.demand.budget = 500_000;
await sample();
const [candidate, candidateOutput] = [...producers][1];
candidateOutput.width = 640; candidateOutput.height = 360;
originalOutput.bitrate = 250_000;
await sample(2);
resume();
await sample(2);
expect(vi.mocked(BrowserEncodingProducer.prototype.dispose).mock.contexts).not.toContain(candidate);
expect(weak.producerId()).toBe(candidate.id);
expect(strong.producerId()).toBe(original.id);
originalOutput.bitrate = 1_000_000;
await sample(3);
expect(producers.size).toBe(2);
expect(weak.producerId()).toBe(candidate.id);
expect(weak.fatal).not.toHaveBeenCalled();
});
it.each(["demand recovers", "stronger sibling leaves"])("retires an unnecessary downgrade when %s", async (change) => {
const strong = member(), weak = member();
await sample(4);
const [original] = [...producers][0];
const ready = holdNextProducerStart();
weak.demand.budget = 500_000;
await sample();
const [candidate] = [...producers][1];
if (change === "demand recovers") weak.demand.budget = 2_000_000;
else strong.handle.dispose();
await sample(2);
ready();
await sample(2);
expect(vi.mocked(BrowserEncodingProducer.prototype.dispose).mock.contexts).toEqual([candidate]);
expect(weak.producerId()).toBe(original.id);
expect(producers.size).toBe(2);
expect(producers.get(original)!.budget).toBe(weak.demand.budget);
});
it("replans a waiting child when another member raises a quiet producer's budget", async () => {
const ready = holdNextProducerStart();
const strong = member(), weak = member();
await sample();
weak.handle.setPaused(true);
const [original, output] = [...producers][0];
output.bitrate = 250_000;
strong.demand.budget = 4_000_000;
ready();
await sample(3);
expect(output.budget).toBe(4_000_000);
expect(weak.select).not.toHaveBeenCalled();
weak.handle.setPaused(false);
await sample(4);
expect(weak.producerId()).not.toBe(original.id);
expect(weak.producerId()).toBe([...producers.keys()][1].id);
expect(weak.select).toHaveBeenCalledTimes(1);
expect(strong.producerId()).toBe(original.id);
});
it.each([
{ currentFps: 30, candidateFps: 32, improves: false },
{ currentFps: 20, candidateFps: 30, improves: true },
])("compares $currentFps to $candidateFps fps within the Host ceiling", async ({ currentFps, candidateFps, improves }) => {
const strong = member(), weak = member();
await sample(4);
const [original, originalOutput] = [...producers][0];
weak.demand.budget = 500_000;
await sample(3);
const [candidate, candidateOutput] = [...producers][1];
expect(strong.producerId()).toBe(original.id);
expect(weak.producerId()).toBe(candidate.id);
originalOutput.fps = currentFps;
candidateOutput.fps = candidateFps;
weak.demand.budget = 2_000_000;
await sample(4);
const selected = improves ? candidate : original;
expect(strong.producerId()).toBe(selected.id);
expect(weak.producerId()).toBe(selected.id);
expect(strong.select).toHaveBeenCalledTimes(improves ? 2 : 1);
expect(weak.select).toHaveBeenCalledTimes(improves ? 2 : 3);
expect(vi.mocked(BrowserEncodingProducer.prototype.dispose).mock.contexts)
.toEqual([improves ? original : candidate]);
});
it.each([
{ change: "frame rate", output: { fps: 10 } },
{ change: "dimensions", output: { width: 1280, height: 720 } },
{ change: "bitrate", output: { bitrate: 3_000_000 } },
{ change: "native limitation", output: { reason: "bandwidth" } },
])("rechecks a pending improvement's $change before selection", async ({ output }) => {
const strong = member(), weak = member();
await sample(4);
const [original, originalOutput] = [...producers][0];
weak.demand.budget = 500_000;
await sample(3);
const [candidate, candidateOutput] = [...producers][1];
originalOutput.fps = 20;
weak.demand.budget = 2_000_000;
await sample();
expect(candidateOutput.budget).toBe(2_000_000);
expect(strong.producerId()).toBe(original.id);
expect(weak.producerId()).toBe(candidate.id);
Object.assign(candidateOutput, output);
await sample();
expect(strong.producerId()).toBe(original.id);
expect(strong.select).toHaveBeenCalledTimes(1);
expect(weak.fatal).not.toHaveBeenCalled();
});
it.each(["key dimensions", "quiet current", "unknown current", "sampled limitation", "budget", "target demand", "profile"])(
"rejects an optional handoff when %s changes while waiting for its key", async (change) => {
const strong = member(), weak = member();
await sample(4);
const [original, originalOutput] = [...producers][0];
weak.demand.budget = 500_000;
await sample(3);
const [candidate, candidateOutput] = [...producers][1];
let accept!: (metadata: RTCEncodedVideoFrameMetadata) => boolean;
strong.select.mockImplementationOnce((_id, _key, _commit, validate) => { accept = validate; });
originalOutput.fps = 20;
weak.demand.budget = 2_000_000;
await sample(2);
expect(accept).toBeDefined();
expect(strong.producerId()).toBe(original.id);
if (change === "sampled limitation") { candidateOutput.reason = "bandwidth"; await sample(); }
if (change === "quiet current") { originalOutput.fps = 0; await sample(); }
if (change === "unknown current") {
vi.spyOn(original, "report").mockResolvedValueOnce(new Map() as unknown as RTCStatsReport);
await sample();
}
if (change === "budget") { strong.demand.budget = 500_000; await sample(); }
if (change === "target demand") { weak.demand.budget = 500_000; await sample(); }
if (change === "profile") await strong.handle.updateProfile({ ...profile, resolution: "720p" });
const key = ["key dimensions", "quiet current", "unknown current"].includes(change)
? { width: 960, height: 540 } : { width: 1920, height: 1080 };
expect(accept(key)).toBe(false);
// No pending reference may keep a rejected producer alive after its real
// user leaves, nor may the healthy current producer have been retired.
weak.handle.dispose();
expect(vi.mocked(BrowserEncodingProducer.prototype.dispose).mock.contexts).toEqual([candidate]);
expect(strong.producerId()).toBe(original.id);
expect(strong.fatal).not.toHaveBeenCalled();
});
it("accepts a necessary lower-resolution handoff at the recovery frame", async () => {
const strong = member(), weak = member();
await sample(4);
const original = [...producers.keys()][0]!;
let accept!: (metadata: RTCEncodedVideoFrameMetadata) => boolean, commit!: () => void;
weak.select.mockImplementationOnce((_id, _key, selected, validate) => { accept = validate; commit = selected; });
weak.demand.budget = 500_000;
await sample(3);
expect(accept({ width: 320, height: 180 })).toBe(true);
commit();
expect(strong.producerId()).toBe(original.id);
expect(original.dispose).not.toHaveBeenCalled();
expect(weak.fatal).not.toHaveBeenCalled();
});
it("retains the owner of an accepted recovery frame until its write completes", async () => {
const strong = member(), weak = member();
await sample(4);
const [, originalOutput] = [...producers][0];
weak.demand.budget = 500_000;
await sample(3);
const [candidate] = [...producers][1];
const select = strong.select.getMockImplementation()!;
let complete!: () => void;
strong.select.mockImplementationOnce((id, key, selected, accept) => select(id, key, () => {
complete = selected;
strong.cancelSelection.mockReturnValue(false);
}, accept));
originalOutput.fps = 20;
weak.demand.budget = 2_000_000;
await sample(2);
expect(complete).toBeDefined();
weak.demand.budget = 500_000;
await sample(2);
weak.handle.dispose();
expect(vi.mocked(BrowserEncodingProducer.prototype.dispose).mock.contexts).not.toContain(candidate);
complete();
await sample(2);
expect(strong.producerId()).toBe(candidate.id);
expect(strong.select).toHaveBeenCalledTimes(2);
expect(strong.fatal).not.toHaveBeenCalled();
});
it("starts protection at committed output, ignoring retired selection callbacks", async () => {
const begin = vi.spyOn(BrowserEncodingProducer.prototype, "beginOutput");
const first = member();
let selected: (() => void) | undefined;
first.select.mockImplementation((_id, _requestKey, commit) => { selected = commit; });
await sample(3);
expect(selected).toBeDefined();
expect(begin).not.toHaveBeenCalled();
selected!();
expect(begin).toHaveBeenCalledOnce();
const late = member();
let retired: (() => void) | undefined;
late.select.mockImplementation((_id, _requestKey, commit) => { retired = commit; });
await sample(2);
expect(retired).toBeDefined();
late.handle.dispose();
retired!();
expect(begin).toHaveBeenCalledOnce();
});
it("rechecks a ready handoff after applying a falling budget, without another stats tick", async () => {
const strong = member(), weak = member();
await sample(4);
const original = [...producers.keys()][0]!;
const ready = holdNextProducerStart();
weak.demand.budget = 500_000;
await sample();
const candidate = [...producers.keys()][1]!;
let complete!: () => void;
vi.mocked(candidate.update).mockImplementationOnce(async (_profile, budget) => {
await new Promise<void>((resolve) => { complete = resolve; });
producers.get(candidate)!.budget = budget;
});
weak.demand.budget = 100_000;
await sample();
ready();
await vi.advanceTimersByTimeAsync(0);
expect(candidate.update).toHaveBeenCalledWith(profile, 100_000);
expect(weak.producerId()).toBe(original.id);
complete();
await vi.advanceTimersByTimeAsync(0);
expect(weak.producerId()).toBe(candidate.id);
expect(strong.producerId()).toBe(original.id);
expect(producers.get(original)!.budget).toBe(2_000_000);
});
it.each(["failed", "retired"])("does not install a %s budget update's candidate", async (outcome) => {
member();
const weak = member();
await sample(4);
const ready = holdNextProducerStart();
weak.demand.budget = 500_000;
await sample();
const candidate = [...producers.keys()][1]!;
let resolve!: () => void, reject!: (error: Error) => void;
vi.mocked(candidate.update).mockImplementationOnce(() => new Promise<void>((yes, no) => { resolve = yes; reject = no; }));
weak.demand.budget = 100_000;
await sample();
ready();
await vi.advanceTimersByTimeAsync(0);
const selections = weak.select.mock.calls.length;
if (outcome === "retired") { weak.handle.dispose(); resolve(); }
else reject(new Error("encoder settings failed"));
await vi.advanceTimersByTimeAsync(0);
expect(weak.select).toHaveBeenCalledTimes(selections);
expect(weak.fatal).not.toHaveBeenCalled();
});
});