Skip to content

An open source design system that's fully customizable and agent ready

TypeScript
5.9 KB
Raw Blame
// Copyright (c) Meta Platforms, Inc. and affiliates.

/**
 * @file blog-live-examples.spec.ts
 * @input The built Docsite (canary target), its live-example fixture post
 *   (`src/content/blog/fixtures/live-example-sample.md`), that post's
 *   plain-text route, the RSS feed, the sitemap, and the blog index.
 * @output Real-browser proof of spec:AST-033 FR22: each `tsx live` block runs
 *   in its own sandboxed preview frame (opaque origin, `allow-scripts` only);
 *   an example imports a published package and a post-local asset; a block
 *   that throws or fails to compile shows its error inside its frame while the
 *   post renders on; the plain-text route shows the source as an ordinary code
 *   block; and the fixture is never listed.
 * @position Docsite browser contracts (`pnpm test:docsite-browser`).
 */

import {expect, test, type Frame, type Page} from '@playwright/test';

const POST = '/blog/live-example-sample';

// The post shows its authors' GitHub avatars, and the root layout links the
// Google Fonts stylesheet. A slow or failing outside host must not hold these
// tests: they answer GitHub's hosts with a local image and Google Fonts with an
// empty stylesheet, and open the post without waiting for every image to load.
const GITHUB = /^https:\/\/(github\.com|avatars\.githubusercontent\.com)\//;
const GOOGLE_FONTS = /^https:\/\/fonts\.(googleapis|gstatic)\.com\//;
const PIXEL = Buffer.from(
  'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=',
  'base64',
);

test.beforeEach(async ({page}) => {
  await page.route(GITHUB, route =>
    route.fulfill({status: 200, contentType: 'image/png', body: PIXEL}),
  );
  await page.route(GOOGLE_FONTS, route =>
    route.fulfill({status: 200, contentType: 'text/css', body: ''}),
  );
});

/** Opens the fixture post once its document has loaded. */
async function openPost(page: Page) {
  await page.goto(POST, {waitUntil: 'domcontentloaded'});
}

// The frame loads the TypeScript compiler before it renders anything.
const POLL = {timeout: 45_000};

/** The live-example frames of the post, in document order. */
async function liveFrames(page: Page): Promise<Frame[]> {
  const figures = page.locator('figure[data-live-example]');
  await expect(figures).toHaveCount(4);
  const frames: Frame[] = [];
  for (let index = 0; index < 4; index += 1) {
    const iframe = figures.nth(index).locator('iframe');
    await expect(iframe).toHaveAttribute('sandbox', 'allow-scripts');
    const handle = await iframe.elementHandle();
    const frame = await handle?.contentFrame();
    if (frame == null) {
      throw new Error(`live example ${index} has no frame`);
    }
    frames.push(frame);
  }
  return frames;
}

test('runs each tsx live block in its own sandboxed frame and contains failures', async ({
  page,
}) => {
  const pageErrors: string[] = [];
  page.on('pageerror', error => pageErrors.push(error.message));
  await page.setViewportSize({width: 1280, height: 900});
  await openPost(page);
  const [counter, asset, broken, uncompilable] = await liveFrames(page);

  // A running example, importing a published package: it renders and it runs.
  const count = counter.getByTestId('count');
  await expect(count).toHaveText('Clicked 0 times', POLL);
  await counter.getByRole('button', {name: 'Add one'}).click();
  await expect(count).toHaveText('Clicked 1 times');

  // An example importing its post's own asset.
  await expect(asset.getByTestId('sample-card')).toContainText(
    'The Star',
    POLL,
  );

  // A runtime error and a compile error each stay inside their own frame.
  await expect(broken.locator('body')).toContainText(
    'This example fails on purpose',
    POLL,
  );
  // The compiler transpiles without diagnostics, so a syntax error surfaces
  // when the frame evaluates the output.
  await expect(uncompilable.locator('body')).toContainText(
    'Unexpected token',
    POLL,
  );

  // The frame has an opaque origin: the example cannot reach the post.
  expect(
    await counter.evaluate(() => {
      try {
        return window.parent.document == null;
      } catch {
        return 'blocked';
      }
    }),
  ).toBe('blocked');

  // The post around the examples keeps rendering, and nothing the examples
  // did reached it as an error.
  await expect(
    page.getByText('The post keeps rendering after every example above.'),
  ).toBeVisible();
  await expect(
    page.locator('pre, code').filter({hasText: 'NotLive'}).first(),
  ).toBeVisible();
  expect(pageErrors).toEqual([]);
});

test('opens the post and runs its examples while GitHub never answers', async ({
  page,
}) => {
  // Registered after the image above, so it answers GitHub's hosts first: a
  // request to them never completes.
  await page.route(GITHUB, () => {});
  await openPost(page);
  const [counter] = await liveFrames(page);
  await expect(counter.getByTestId('count')).toHaveText(
    'Clicked 0 times',
    POLL,
  );
});

test('shows each live block as its source in the plain-text route', async ({
  request,
}) => {
  const response = await request.get(`${POST}.txt`);
  expect(response.ok()).toBe(true);
  const text = await response.text();
  expect(text).not.toMatch(/^\s*(`{3,}|~{3,})\s*tsx\s+live/m);
  expect(text).toContain('```tsx\nimport {SampleCard}');
  expect(text).toContain("throw new Error('This example fails on purpose');");
});

test('never lists the fixture in the blog index, the feed, or the sitemap', async ({
  page,
  request,
}) => {
  for (const path of ['/rss.xml', '/sitemap.xml']) {
    const body = await (await request.get(path)).text();
    expect(body, path).not.toContain('live-example-sample');
  }
  await page.goto('/blog');
  await expect(page.locator('a[href*="live-example-sample"]')).toHaveCount(0);
  // The fixture page itself asks search engines not to index it.
  await openPost(page);
  await expect(page.locator('meta[name="robots"]')).toHaveAttribute(
    'content',
    /noindex/,
  );
});