Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/experiments/pixel/pixel.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="color-scheme" content="light dark"><link rel="icon" href="data:,">
<link rel="stylesheet" href="./style.css"><link rel="stylesheet" href="./pixel.css?v=first-letter-v1&amp;art=post-clerks-v2"><link rel="stylesheet" href="./pixel-recipient.css?v=recipient-1">
<link rel="stylesheet" href="./pixel-code.css?v=code-1">
<script type="module" src="./pixel.mjs?v=recipient-resume-v1&amp;art=post-clerks-v2"></script><title>算法游乐场 · 像素邮局</title>
<script type="module" src="./pixel.mjs?v=delivery-render-v1&amp;art=post-clerks-v2"></script><title>算法游乐场 · 像素邮局</title>
<link rel="stylesheet" href="./navigation.css?v=three-cat-brand-v1"><script type="module" src="./navigation.mjs?v=cats-in-scenes-v1"></script>
<link rel="stylesheet" href="./space-theme.css">
<link rel="stylesheet" href="./experiment-showcase.css?v=home-aligned-v1">
Expand Down
5 changes: 3 additions & 2 deletions src/experiments/pixel/pixel.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,9 @@ function playDelivery(current) {
function advance(time) {
if (job !== current || current.paused || current.epoch !== epoch) return;
if (current.start === null) current.start = time;
current.sent = Math.min(current.trace.bits.length,current.base+Math.floor(Math.max(0,time-current.start)/6000*current.trace.bits.length));
renderDelivery(current);
const sent = Math.min(current.trace.bits.length,current.base+Math.floor(Math.max(0,time-current.start)/6000*current.trace.bits.length));
// High-refresh frames may still show the same bit. Explicit controls render separately.
if (sent !== current.sent) { current.sent = sent; renderDelivery(current); }
if (current.sent === current.trace.bits.length) finishDelivery(current);
else frame = requestAnimationFrame(advance);
}
Expand Down
48 changes: 47 additions & 1 deletion tests/experiments/pixel/pixel-ui.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ async function boot({ reduced = false, missingCanvas = false, storage = memorySt
const html = await readFile(new URL('../../../dist/pixel.html', import.meta.url), 'utf8').catch(() => '');
assert.ok(html.includes('id="editor"'), 'playable pixel page must exist');
class Element extends EventTarget {
constructor() { super(); this.dataset = {}; this.style = {}; this.attributes = new Map(); this.hidden = false; this.disabled = false; this.open = false; this.textContent = ''; this.width = 512; this.height = 512; this.ctx = { pixels: new Map(), clearRect() { this.pixels.clear(); }, fillRect(x, y, w, h) { if (w === 32 && h === 32) this.pixels.set(`${x},${y}`, this.fillStyle); }, strokeRect() {}, beginPath() {}, moveTo() {}, lineTo() {}, stroke() {} }; }
constructor() { super(); this.dataset = {}; this.style = {}; this.attributes = new Map(); this.hidden = false; this.disabled = false; this.open = false; this.textContent = ''; this.width = 512; this.height = 512; this.ctx = { pixels: new Map(), paints: 0, clearRect() { this.paints++; this.pixels.clear(); }, fillRect(x, y, w, h) { if (w === 32 && h === 32) this.pixels.set(`${x},${y}`, this.fillStyle); }, strokeRect() {}, beginPath() {}, moveTo() {}, lineTo() {}, stroke() {} }; }
getContext() { return missingCanvas ? null : this.ctx; }
setAttribute(k, v) { this.attributes.set(k, String(v)); } getAttribute(k) { return this.attributes.get(k); }
getBoundingClientRect() { return { left: 0, top: 0, width: 256, height: 256 }; }
Expand Down Expand Up @@ -373,6 +373,52 @@ test('delivery never reveals the legacy cat green cells before their actual code
assert.equal(u.els.get('delivery-preview')?.dataset.decoded,'216');
}finally{u.restore();}
});
test('high-refresh delivery skips unchanged bits but still verifies and saves the complete postcard',async()=>{
const storage=memoryStorage(),u=await boot({storage});try{
u.click('send');
const count=u.els.get('delivery-count'),canvas=u.els.get('delivery-preview').ctx;
const checkpoints=new Map([[0,'0 / 400 bit'],[180,'100 / 400 bit'],[360,'200 / 400 bit'],[540,'300 / 400 bit'],[720,'400 / 400 bit']]);
let unchanged=0;
for(let n=0;n<=720;n++){
const before=count.textContent,paints=canvas.paints,writes=count.textWrites;
u.advance(n*1000/120);
if(checkpoints.has(n))assert.equal(count.textContent,checkpoints.get(n),'skipping duplicate paints must not slow the six-second transfer');
if(count.textContent===before){
unchanged++;
assert.equal(canvas.paints,paints,'another refresh of the same bit must not redraw the postcard');
assert.equal(count.textWrites,writes,'the same bit must not refresh delivery text');
}
}
assert.ok(unchanged>100,'the fixture exercises repeated display frames between actual bits');
assert.equal(u.frames.size,0);
assert.equal(u.els.get('delivery-preview').dataset.decoded,'256');
assert.equal(u.els.get('first-letter-read').disabled,false);
assert.deepEqual(u.pixels('receiver'),u.pixels('editor'));
assert.deepEqual(JSON.parse(storage.records.get(DRAFT_KEYS.pixel)).value.received,preset('heim'));
assert.equal(storage.writes,1,'only the verified delivery saves the new receipt');
}finally{u.restore();}
});
test('same-bit pause and resume still update controls, and replay cannot overwrite the successful letter',async()=>{
const storage=memoryStorage(),u=await boot({storage});try{
u.click('send');u.finish();u.click('preset-bobby');
const drawing=u.pixels('editor'),receipt=u.pixels('receiver'),writes=storage.writes;
u.click('delivery-replay');
const count=u.els.get('delivery-count'),canvas=u.els.get('delivery-preview').ctx;
const start=count.textContent;
assert.equal(u.els.get('delivery-toggle').textContent,'继续播放');
u.click('delivery-toggle');assert.equal(u.els.get('delivery-toggle').textContent,'暂停');
const paints=canvas.paints;
u.advance(100);u.advance(101);
assert.equal(count.textContent,start);assert.equal(canvas.paints,paints);
u.click('delivery-toggle');assert.equal(u.els.get('delivery-toggle').textContent,'继续播放');
u.click('delivery-bit');assert.equal(parseInt(count.textContent),parseInt(start)+1);
u.click('delivery-toggle');u.advance(10000);u.advance(16000);
assert.equal(u.frames.size,0);assert.equal(u.els.get('delivery-controls').hidden,true);
assert.match(u.els.get('delivery-explanation').textContent,/回放读完/);
assert.deepEqual(u.pixels('editor'),drawing);assert.deepEqual(u.pixels('receiver'),receipt);
assert.equal(storage.writes,writes);
}finally{u.restore();}
});
test('paused delivery reads one bit, guards queued frames, and resumes without catching up',async()=>{
const u=await boot();try{
assert.ok(u.els.has('delivery-toggle'),'pause control is in the real page');
Expand Down
Loading