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
30 changes: 30 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,36 @@ jobs:
- uses: ./.github/actions/prepare-playground
- run: sudo npx playwright install
- run: sudo CI=true npx nx e2e php-wasm-web
test-reprint-bridge:
if: github.repository == 'WordPress/wordpress-playground' || github.event_name == 'pull_request'
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read # Required to clone the repo.
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
- uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: sqlite3, pdo_sqlite, curl, zip
tools: none
- name: Download and verify the pinned Reprint client
run: |
# Use the website's release lock and checksum verification.
node --input-type=module <<'JS'
import { copyFileSync, readFileSync } from 'node:fs';
import { bundleReprint } from './packages/playground/website/bin/reprint-assets.mts';
const release = JSON.parse(readFileSync('packages/playground/website/src/lib/reprint/release.json', 'utf8'));
const directory = new URL('file:///tmp/playground-reprint-assets/');
await bundleReprint(release, directory);
copyFileSync(new URL(`reprint-${release.sha256}.phar`, directory), '/tmp/playground-reprint.phar');
JS
- name: Run the native PHP bridge checks
run: php packages/playground/website/src/lib/reprint/bridge-test.php

# E2E Playwright tests - builds inline per runner for faster parallel execution
# Previously used a prepare job + artifact sharing, but inline builds allow
# all shards to start immediately in parallel, reducing total wall time.
Expand Down
432 changes: 432 additions & 0 deletions packages/playground/website/src/lib/reprint/bridge-test.php

Large diffs are not rendered by default.

542 changes: 542 additions & 0 deletions packages/playground/website/src/lib/reprint/bridge.php

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions packages/playground/website/src/lib/reprint/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { PlaygroundClient } from '@wp-playground/client';
import { joinPaths } from '@php-wasm/util';

/** The address travels with the saved site; its key stays in this browser tab. */
export async function readReprintConnection(
playground: PlaygroundClient
): Promise<string | null> {
try {
const path = joinPaths(
await playground.documentRoot,
'.playground-reprint/connection.json'
);
if (!(await playground.fileExists(path))) return null;
const connection = JSON.parse(await playground.readFileAsText(path));
return typeof connection.url === 'string' ? connection.url : null;
} catch {
// An unreadable saved address must not prevent a new connection.
return null;
}
}
143 changes: 134 additions & 9 deletions packages/playground/website/src/lib/reprint/reprint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
normalizeReprintUrl,
installReprint,
runBridge,
pullSite,
} from './reprint';
import { fetchWithCorsProxy } from '@php-wasm/web-service-worker';
import release from './release.json';
Expand Down Expand Up @@ -247,7 +248,7 @@ describe('Reprint command replies', () => {
expect(() => normalizeReprintUrl(url)).toThrow();
});

it('streams byte progress before the PHP request finishes and ignores text-only file updates', async () => {
it('reads native Reprint byte counts before PHP finishes and ignores text-only file updates', async () => {
const { playground } = createClient([]);
let output!: ReadableStreamDefaultController<Uint8Array>;
let finish!: (code: number) => void;
Expand Down Expand Up @@ -277,14 +278,21 @@ describe('Reprint command replies', () => {
await vi.waitFor(() => expect(output).toBeDefined());
const line =
JSON.stringify({
playgroundProgress: {
phase: 'files-pull',
message: 'Downloading site files',
bytesDone: 1024,
bytesTotal: 4096,
filesDone: 0,
filesTotal: 1,
path: 'private-token',
type: 'file_progress',
command: 'files-pull',
phase: 'fetch',
message: 'Downloading files',
files_done: 0,
files_total: 1,
path: 'private-token',
progress: {
items: { unit: 'files', done: 0, total: 1 },
bytes: { done: 1024, total: 4096 },
current_file: {
path_b64: btoa('/source/large.txt'),
bytes_done: 1024,
bytes_total: 4096,
},
},
}) + '\n';
output.enqueue(new TextEncoder().encode(line.slice(0, 30)));
Expand All @@ -300,6 +308,24 @@ describe('Reprint command replies', () => {
})
)
);
output.enqueue(
new TextEncoder().encode(
JSON.stringify({
heartbeat: true,
command: 'files-pull',
phase: 'fetch',
progress: {
items: { unit: 'files', done: 0, total: 1 },
bytes: { done: 2048, total: 4096 },
},
}) + '\n'
)
);
await vi.waitFor(() =>
expect(progress).toHaveBeenLastCalledWith(
expect.objectContaining({ bytesDone: 2048, bytesTotal: 4096 })
)
);
output.enqueue(
new TextEncoder().encode(
JSON.stringify({
Expand Down Expand Up @@ -404,6 +430,105 @@ describe('Reprint command replies', () => {
});
});

// The bridge loop and saved connection are independent from setup controls.
describe('Direct file pull', () => {
beforeEach(() => {
vi.restoreAllMocks();
vi.clearAllMocks();
vi.spyOn(crypto.subtle, 'digest').mockResolvedValue(
Uint8Array.from(release.sha256.match(/../g)!, (byte) =>
parseInt(byte, 16)
).buffer
);
});
it('advances stages on the primary PHP instance and saves the URL without its token', async () => {
const { playground, runStream, writeFile } = createClient([
{ status: 'continue', stage: 'files-pull' },
{ status: 'continue', stage: 'files-prepare' },
{ status: 'complete' },
]);
const updates: TransferProgressUpdate[] = [];
await pullSite(
playground,
'https://example.com',
'private-token',
(update) => updates.push(update)
);
expect(runStream).toHaveBeenCalledTimes(3);
for (const [options] of runStream.mock.calls)
expect(options).toMatchObject({ usePrimaryPhp: true });
expect(writeFile).toHaveBeenCalledWith(
'/wordpress/.playground-reprint/connection.json',
JSON.stringify({ url: 'https://example.com/?reprint-api' })
);
expect(JSON.stringify(writeFile.mock.calls)).not.toContain(
'private-token'
);
expect(updates.at(-1)?.overallPercent).toBe(100);
});
it('continues an unknown stage without losing the last streamed caption', async () => {
const { playground, runStream } = createClient([
{ status: 'complete' },
]);
runStream.mockImplementationOnce(async () =>
response(
JSON.stringify({
playgroundProgress: {
phase: 'files-pull',
message: 'Current transfer caption',
},
}) +
'\n' +
JSON.stringify({
playgroundReprint: {
status: 'continue',
stage: 'future-stage',
},
}) +
'\n'
)
);
const onProgress = vi.fn();
await pullSite(playground, 'https://example.com', 'token', onProgress);
expect(runStream).toHaveBeenCalledTimes(2);
expect(onProgress).toHaveBeenCalledWith(
expect.objectContaining({
phase: 'future-stage',
message: 'Current transfer caption',
})
);
expect(onProgress.mock.lastCall?.[0].overallPercent).toBe(100);
});
it('stops after the current stage and retries with the same connection', async () => {
const { playground, runStream } = createClient([
{ status: 'continue', stage: 'files-pull' },
{ status: 'complete' },
]);
const controller = new AbortController();
controller.abort();
await expect(
pullSite(
playground,
'https://example.com',
'token',
vi.fn(),
controller.signal
)
).rejects.toThrow('Pull stopped.');
expect(runStream).toHaveBeenCalledTimes(1);
await pullSite(
playground,
'https://example.com/?reprint-api',
'token',
vi.fn()
);
const requests = runStream.mock.calls.map(([options]) =>
JSON.parse(options.env.PLAYGROUND_REPRINT)
);
expect(requests[0]).toEqual(requests[1]);
});
});

/** Supply streamed PHP replies without booting WordPress. */
function createClient(results: unknown[], events: string[] = []) {
const runStream = vi.fn(
Expand Down
Loading