Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { PHP } from '@php-wasm/universal';
import { LatestMinifiedWordPressVersion } from '@wp-playground/wordpress-builds';

describe('PlaygroundWorkerEndpointBlueprints', () => {
beforeEach(() => {
Expand Down Expand Up @@ -352,6 +353,60 @@ describe('PlaygroundWorkerEndpointBlueprints', () => {
expect(fetch).toHaveBeenCalledTimes(1);
}, 10000);

it.each([
{ wpVersion: 'latest', expected: LatestMinifiedWordPressVersion },
{ wpVersion: 'nightly', expected: 'trunk' },
{ wpVersion: '6.8', expected: '6.8' },
])(
'resolves the requested WordPress version $wpVersion to a build version',
async ({ wpVersion, expected }) => {
let endpoint:
| {
boot(options: Record<string, unknown>): Promise<void>;
requestedWordPressVersion?: string;
}
| undefined;
vi.doMock('@wp-playground/wordpress', () => ({
bootWordPress: vi.fn(),
}));
vi.doMock('@php-wasm/web', () => ({
certificateToPEM: vi.fn(),
createDirectoryHandleMountHandler: vi.fn(),
exposeAPI: vi.fn((api) => {
endpoint = api;
return [vi.fn(), vi.fn()];
}),
loadWebRuntime: vi.fn(),
}));
await import('./playground-worker-endpoint-blueprints');
if (!endpoint) {
throw new Error('Expected exposeAPI to receive an endpoint');
Comment on lines +356 to +383

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vi.resetModules() already runs in the describe-level beforeEach (line 7), which Vitest applies before every case, it.each rows included. beforeEach also stubs a fresh self, so the once-per-global guard flag is cleared too — both conditions for re-evaluating the entrypoint are met per case.

The last test in this file demonstrates it: it imports the entrypoint, calls vi.resetModules(), imports again, and asserts the guard throws. That only happens if the module body re-runs, so the module isn't served from cache after a reset. The existing 4-row handles a concrete WordPress release ... it.each uses the same doMock + dynamic-import pattern.

Verified all three rows pass in isolation, and each has an explicit if (!endpoint) throw guard, so the failure mode described here couldn't pass silently.

On cleanup: afterEach already doUnmocks @php-wasm/web, @wp-playground/blueprints and @wp-playground/wordpress, plus vi.unstubAllGlobals().

}
vi.spyOn(endpoint as any, 'computeSiteUrl').mockReturnValue(
'http://playground.test'
);
vi.spyOn(endpoint as any, 'createRequestHandler').mockResolvedValue(
{
getPrimaryPhp: vi.fn(async () => ({}) as PHP),
}
);
vi.spyOn(endpoint as any, 'finalizeAfterBoot').mockResolvedValue(
undefined
);

await endpoint.boot({
scope: 'test',
phpVersion: '8.3',
wpVersion,
wordpressInstallMode: 'do-not-attempt-installing',
withNetworking: false,
});

expect(endpoint.requestedWordPressVersion).toBe(expected);
},
10000
);

it('throws a diagnostic error if the worker entrypoint is evaluated twice in the same worker global', async () => {
vi.doMock('@php-wasm/web', () => ({
certificateToPEM: vi.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,16 @@ class PlaygroundWorkerEndpointBlueprints extends PlaygroundWorkerEndpoint {
pathAliases,
});

this.requestedWordPressVersion =
wpVersion === 'nightly' ? 'trunk' : wpVersion;
// `nightly` and `latest` are aliases for a concrete build. Resolve
// them here so the loaded-version check in finalizeAfterBoot()
// compares two build versions instead of an alias and a version.
if (wpVersion === 'nightly') {
this.requestedWordPressVersion = 'trunk';
} else if (wpVersion === 'latest') {
this.requestedWordPressVersion = LatestMinifiedWordPressVersion;
} else {
this.requestedWordPressVersion = wpVersion;
}
const isMinifiedVersion = MinifiedWordPressVersionsList.includes(
this.requestedWordPressVersion
);
Expand Down Expand Up @@ -163,9 +171,8 @@ class PlaygroundWorkerEndpointBlueprints extends PlaygroundWorkerEndpoint {
) {
// Non-minified release like "4.9", "6.8.0", or
// "7.0-RC1": download directly from wordpress.org.
// Sentinel values like "latest" fall through to the
// minified-bundle branch below and resolve to
// LatestMinifiedWordPressVersion.
// Aliases like "latest" were already resolved to a
// minified build above, so they never reach here.
const normalizedVersion = normalizeWordPressVersion(
this.requestedWordPressVersion!
);
Expand Down
Loading