Files
OpenFrame/tests/component/linkify.test.tsx
yusufipk 1d099c68f2 test: add unit, API, component and end-to-end test suites
The repo had no automated tests. Every change was verified by hand.

Adds four layers, 2023 tests in total, runnable with one command:

- 1191 unit tests over the pure logic in lib/, including the full
  computeProjectAccess permission matrix and the billing gate
- 167 component and hook tests in jsdom, covering the hooks that hold
  real logic rather than presentational wrappers
- 647 API integration tests against a real Postgres, with only auth()
  mocked, including a data-driven sweep asserting that none of the 60
  route modules answers 2xx to an unauthenticated caller
- 18 Playwright specs driving a real browser against a real build

Infrastructure: vitest.config.ts with three projects, a disposable
Postgres and MinIO in docker-compose.test.yml, factories and helpers
under tests/, scripts/test.sh as the single entry point, a pre-push
hook running bun run verify, and CI split into check, test and e2e jobs.

The test database is built with prisma db push plus a replay of the
hand-written SQL, because prisma migrate deploy cannot build this schema
from empty: the migration history has no captured baseline. This mirrors
what scripts/docker-db-bootstrap.ts already does in production, and
tests/setup/db-global.ts carries a drift guard so a new migration fails
the run until someone reviews it.

Production code is unchanged apart from one pure-function extraction out
of use-video-player.ts, which was too large to test in jsdom.

Several tests pin behaviour that looks wrong, each marked KNOWN BUG in
place. TESTING.md section 12 records where the plan turned out to be
wrong, and AGENTS.md now states which layer a change needs a test in.
2026-07-26 11:17:26 +07:00

110 lines
3.8 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Linkify } from '@/components/linkify';
describe('Linkify', () => {
it('renders a bare http(s) URL as a new-tab link', () => {
render(<Linkify>{'Ticket at https://tracker.test/OF-12 please'}</Linkify>);
const link = screen.getByRole('link', { name: 'https://tracker.test/OF-12' });
expect(link).toHaveAttribute('href', 'https://tracker.test/OF-12');
expect(link).toHaveAttribute('target', '_blank');
expect(link).toHaveAttribute('rel', 'noopener noreferrer');
});
it('keeps the surrounding text intact', () => {
const { container } = render(
<Linkify>{'Ticket at https://tracker.test/OF-12 please'}</Linkify>
);
expect(container).toHaveTextContent('Ticket at https://tracker.test/OF-12 please');
});
it('links every URL in the string', () => {
render(<Linkify>{'http://a.test/1 then https://b.test/2'}</Linkify>);
expect(screen.getAllByRole('link').map((a) => a.getAttribute('href'))).toEqual([
'http://a.test/1',
'https://b.test/2',
]);
});
it('leaves plain text alone', () => {
render(<Linkify>{'No links in this sentence'}</Linkify>);
expect(screen.queryAllByRole('link')).toHaveLength(0);
expect(screen.getByText('No links in this sentence')).toBeInTheDocument();
});
it('passes non-string children straight through', () => {
render(
<Linkify>
<span data-testid="child">https://not-parsed.test/x</span>
</Linkify>
);
expect(screen.queryAllByRole('link')).toHaveLength(0);
expect(screen.getByTestId('child')).toHaveTextContent('https://not-parsed.test/x');
});
it('does not turn a javascript: URL into a link', () => {
const { container } = render(<Linkify>{'javascript:alert(document.cookie)'}</Linkify>);
expect(container.querySelector('a')).toBeNull();
expect(container).toHaveTextContent('javascript:alert(document.cookie)');
});
it('does not turn a data: URL into a link', () => {
const { container } = render(
<Linkify>{'data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg=='}</Linkify>
);
expect(container.querySelector('a')).toBeNull();
});
it('does not link vbscript:, file: or protocol-relative URLs', () => {
const { container } = render(
<Linkify>{'vbscript:msgbox(1) file:///etc/passwd //evil.test/x'}</Linkify>
);
expect(container.querySelector('a')).toBeNull();
});
it('never injects markup from the string', () => {
const { container } = render(<Linkify>{'<img src=x onerror="alert(1)">'}</Linkify>);
expect(container.querySelector('img')).toBeNull();
expect(container).toHaveTextContent('<img src=x onerror="alert(1)">');
});
it('is case sensitive about the scheme, matching CommentRichText', () => {
const { container } = render(<Linkify>{'HTTPS://EXAMPLE.COM/a'}</Linkify>);
expect(container.querySelector('a')).toBeNull();
});
it('swallows trailing punctuation into the href, matching CommentRichText', () => {
render(<Linkify>{'Fixed in https://example.com/pr/12.'}</Linkify>);
expect(screen.getByRole('link')).toHaveAttribute('href', 'https://example.com/pr/12.');
});
it('does not let a click on the link reach an enclosing handler', async () => {
const onRowClick = vi.fn();
render(
// Mirrors the real call sites, where Linkify sits inside a clickable
// comment row.
<div onClick={onRowClick}>
<Linkify>{'https://tracker.test/OF-12'}</Linkify>
</div>
);
const link = screen.getByRole('link');
link.addEventListener('click', (event) => event.preventDefault());
await userEvent.click(link);
expect(onRowClick).not.toHaveBeenCalled();
});
});