Files
OpenFrame/tests/component/comment-rich-text.test.tsx
T
yusufipk b51e690062 fix: close the findings the test suite surfaced
The suite that landed in #43/#44 was written against existing behaviour, so a
number of tests pinned bugs rather than asserting correct behaviour. This fixes
the production code and moves each of those tests onto the fixed behaviour in
the same change.

Security:

- project-download: derive the archive entry extension from the last path
  segment and restrict it to a short alphanumeric run, so an extensionless
  allowlisted url can no longer contribute a path separator; validate the r2
  branch against the strict proxy-path pattern instead of a `startsWith`, which
  let `/api/upload/video/clip.mp4/../../etc/passwd` through verbatim.
- rate-limit: hash a key or action wider than its column instead of skipping the
  query. Both the guard and the failing INSERT used to answer "allowed", so the
  limit stopped applying entirely. Warn at startup when TRUSTED_PROXY_MODE is
  unset in production.
- video uploads: the file name decides the content type; a client-declared video
  mime no longer makes `payload.exe` acceptable.
- email templates: escape in the helpers rather than relying on every caller,
  with an explicit `rawEmailHtml()` opt-out for the one call site that builds
  markup. `escapeHtml` now covers the single quote.
- CSP: allow loopback object storage outside production only.
- route-access: reach the billing redirect only for the workspace owner. Keying
  it off the owner's billing status alone made the redirect target an oracle for
  whose subscription had lapsed, and sent members to a page they cannot act on.
- search: carry the same billing condition every other read path carries.
- logger: check `err.name` as well as `err.constructor.name`, so a re-thrown,
  deserialised or minified Prisma error is still redacted.
- upload tokens: resolve the signing secret outside the try, so a server booted
  without one fails loudly instead of reporting every grant as a forgery.
- invitations: never downgrade an existing membership, and report a scoped
  invitation that points at nothing as not_found rather than accepted.
- auth: resolve the workspace role for every signed-in caller, so
  checkProjectAccess and computeProjectAccess stop disagreeing about the owner
  who also owns the workspace. The `intent` option is gone with it.
- r2-media-proxy: validate the object key inside the proxy so the guard travels
  with the function; delete the unused, unanchored `mediaUrlToR2Key`.
- r2: sign the content type into presigned PUT grants.

Correctness:

- frame rate snapping picks the nearest standard, not the first within
  tolerance, so 24, 30 and 60 fps are reachable at all.
- a version upload registers its Bunny cleanup as soon as bunny-init answers, so
  a failed tus upload no longer leaves a billed video behind.
- deleting videos clears storage before the rows, so a refused DELETE leaves a
  retryable row rather than an orphaned object.
- an expired upload session can be cancelled, which is what releases its quota.
- `voice/` joins the delete allowlist, so a voice note can be removed by the
  module that wrote it.
- a failed CORS write propagates instead of being mistaken for an empty config
  and replacing the bucket's rules.
- filtering projects by workspace no longer hides projects the unfiltered call
  returns.
- upload retries skip aborts and permanent 4xx; progress no longer divides by
  zero.
- reply edits no longer clear the comment's tag; optimistic resolve rolls back
  to the state it replaced; the delete snapshot is captured once.
- assorted UI fixes: duplicate React keys, double-click guards reading stale
  closures, the tag list fetched twice per load, a failed member list rendering
  as an empty one, a stale "Initializing upload..." beside a failure, and a
  registration banner pointing at an email that never arrives.

Consistency and access:

- the two download routes answer 404 for an id belonging to another tenant, as
  the comment export route already did. A caller who does belong still gets 403.
- accessible names for the share-link password field, the guest name gates, the
  version dialog inputs and the comment-tag controls.

Repository health:

- the runner image installs production dependencies only.
- a setup file for the unit project restores stubbed env centrally.
- native tsconfig path resolution replaces vite-tsconfig-paths.
- `uploadBytesWithProgress` exists once.
- admin stats bill Bunny storage to the workspace owner like every other
  quota, gate on the configured flag, wire up the single-flight guard and count
  the statuses that belonged to no bucket.
- `r2Client.destroy()` releases the presign client too.
- `prepare` tolerates a production install, where husky is absent.
2026-07-26 18:53:54 +07:00

262 lines
9.7 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { CommentRichText } from '@/components/video-page/comment-rich-text';
import type { VideoAsset } from '@/components/video-page/types';
function makeAsset(overrides: Partial<VideoAsset> = {}): VideoAsset {
return {
id: 'a1b2c3',
videoId: 'vid1',
kind: 'IMAGE',
provider: 'R2_IMAGE',
displayName: 'Reference frame.png',
sourceUrl: null,
providerVideoId: null,
thumbnailUrl: null,
uploadedByUserId: 'user1',
uploadedByGuestName: null,
createdAt: '2026-01-01T00:00:00.000Z',
updatedAt: '2026-01-01T00:00:00.000Z',
uploadedByUser: null,
canDelete: true,
...overrides,
};
}
let consoleError: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
// Mentions mixed with text make React complain about duplicate keys (see the
// pinned bug at the bottom of this file). Silence it so the suite output stays
// readable; that one test asserts the warning is still there.
consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('CommentRichText linkification', () => {
it('renders a bare http(s) URL as a new-tab link', () => {
render(<CommentRichText text="See https://example.com/shot-3 for the grade" />);
const link = screen.getByRole('link', { name: 'https://example.com/shot-3' });
expect(link).toHaveAttribute('href', 'https://example.com/shot-3');
expect(link).toHaveAttribute('target', '_blank');
expect(link).toHaveAttribute('rel', 'noopener noreferrer');
});
it('keeps the surrounding text intact', () => {
const { container } = render(
<CommentRichText text="See https://example.com/shot-3 for the grade" />
);
expect(container).toHaveTextContent('See https://example.com/shot-3 for the grade');
});
it('links every URL in the comment', () => {
render(<CommentRichText text="http://a.test/1 and https://b.test/2 and https://c.test/3" />);
expect(screen.getAllByRole('link').map((a) => a.getAttribute('href'))).toEqual([
'http://a.test/1',
'https://b.test/2',
'https://c.test/3',
]);
});
it('renders text with no URL as plain text', () => {
render(<CommentRichText text="Just a note about the cut" />);
expect(screen.queryAllByRole('link')).toHaveLength(0);
expect(screen.getByText('Just a note about the cut')).toBeInTheDocument();
});
it('renders nothing for an empty comment', () => {
const { container } = render(<CommentRichText text="" />);
expect(container).toBeEmptyDOMElement();
});
});
describe('CommentRichText URL scheme safety', () => {
it('does not turn a javascript: URL into a link', () => {
const { container } = render(<CommentRichText text="javascript:alert(document.cookie)" />);
expect(screen.queryAllByRole('link')).toHaveLength(0);
expect(container.querySelector('a')).toBeNull();
expect(container).toHaveTextContent('javascript:alert(document.cookie)');
});
it('does not turn a javascript: URL into a link mid-sentence either', () => {
const { container } = render(
<CommentRichText text="click javascript:alert(1) now, or JavaScript:alert(1)" />
);
expect(container.querySelector('a')).toBeNull();
});
it('does not turn a data: URL into a link', () => {
const { container } = render(
<CommentRichText text="data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==" />
);
expect(container.querySelector('a')).toBeNull();
expect(container).toHaveTextContent('data:text/html;base64');
});
it('does not link vbscript:, file: or protocol-relative URLs', () => {
const { container } = render(
<CommentRichText text="vbscript:msgbox(1) file:///etc/passwd //evil.test/x" />
);
expect(container.querySelector('a')).toBeNull();
});
it('never injects markup from the comment body', () => {
const { container } = render(
<CommentRichText text={'<img src=x onerror="alert(1)"> <script>alert(2)</script>'} />
);
expect(container.querySelector('img')).toBeNull();
expect(container.querySelector('script')).toBeNull();
expect(container).toHaveTextContent('<img src=x onerror="alert(1)">');
});
it('is case sensitive about the scheme, so HTTPS:// is left as text', () => {
// Pinning current behaviour: the regex has no `i` flag, so an uppercase
// scheme is not linkified. Harmless, but worth knowing before someone
// "fixes" the regex and widens what becomes clickable.
const { container } = render(<CommentRichText text="HTTPS://EXAMPLE.COM/a" />);
expect(container.querySelector('a')).toBeNull();
});
it('swallows trailing punctuation into the href', () => {
// Pinning current behaviour: `[^\s]+` is greedy to the next whitespace, so
// the sentence-ending period lands inside the link.
render(<CommentRichText text="Fixed in https://example.com/pr/12." />);
expect(screen.getByRole('link')).toHaveAttribute('href', 'https://example.com/pr/12.');
});
});
describe('CommentRichText asset mentions', () => {
it('renders a mention as a button labelled with the asset name', () => {
render(<CommentRichText text="Compare with @[Reference frame.png](asset:a1b2c3)" />);
expect(screen.getByRole('button', { name: '@Reference frame.png' })).toBeInTheDocument();
});
it('reports the mentioned asset id when clicked', async () => {
const onAssetMentionClick = vi.fn();
render(
<CommentRichText
text="Compare with @[Reference frame.png](asset:a1b2c3)"
onAssetMentionClick={onAssetMentionClick}
/>
);
await userEvent.click(screen.getByRole('button', { name: '@Reference frame.png' }));
expect(onAssetMentionClick).toHaveBeenCalledWith('a1b2c3');
});
it('prefers the current asset name over the name stored in the comment', () => {
render(
<CommentRichText
text="Compare with @[old-name.png](asset:a1b2c3)"
assets={[makeAsset({ displayName: 'Renamed.png' })]}
/>
);
expect(screen.getByRole('button', { name: '@Renamed.png' })).toBeInTheDocument();
expect(screen.queryByRole('button', { name: '@old-name.png' })).not.toBeInTheDocument();
});
it('falls back to the stored name when the asset is gone', () => {
render(<CommentRichText text="@[deleted.png](asset:zzz999)" assets={[makeAsset()]} />);
expect(screen.getByRole('button', { name: '@deleted.png' })).toBeInTheDocument();
});
it('does not throw when clicked without a handler', async () => {
render(<CommentRichText text="@[Reference frame.png](asset:a1b2c3)" />);
await userEvent.click(screen.getByRole('button', { name: '@Reference frame.png' }));
expect(screen.getByRole('button', { name: '@Reference frame.png' })).toBeInTheDocument();
});
it('renders text, mentions and links together in reading order', () => {
const { container } = render(
<CommentRichText text="Before @[One](asset:aaa111) middle https://example.com/x after" />
);
expect(container).toHaveTextContent('Before @One middle https://example.com/x after');
expect(screen.getByRole('button', { name: '@One' })).toBeInTheDocument();
expect(screen.getByRole('link', { name: 'https://example.com/x' })).toBeInTheDocument();
});
it('renders several mentions in one comment', () => {
render(<CommentRichText text="@[One](asset:aaa111) then @[Two](asset:bbb222)" />);
expect(screen.getAllByRole('button').map((b) => b.textContent)).toEqual(['@One', '@Two']);
});
it('accepts an uppercase asset id', () => {
const onAssetMentionClick = vi.fn();
render(
<CommentRichText text="@[One](asset:AAA111)" onAssetMentionClick={onAssetMentionClick} />
);
expect(screen.getByRole('button', { name: '@One' })).toBeInTheDocument();
});
it('leaves a mention with a non-alphanumeric id as plain text', () => {
const { container } = render(<CommentRichText text="@[One](asset:aa-11)" />);
expect(screen.queryAllByRole('button')).toHaveLength(0);
expect(container).toHaveTextContent('@[One](asset:aa-11)');
});
it('leaves a malformed mention as plain text', () => {
const { container } = render(<CommentRichText text="@[One](assets:aaa111) @[Two] (asset:b)" />);
expect(screen.queryAllByRole('button')).toHaveLength(0);
expect(container).toHaveTextContent('@[One](assets:aaa111)');
});
it('does not inject markup through the mention label', () => {
const { container } = render(
<CommentRichText text={'@[<img src=x onerror="alert(1)">](asset:aaa111)'} />
);
expect(container.querySelector('img')).toBeNull();
expect(container).toHaveTextContent('@<img src=x onerror="alert(1)">');
});
it('does not linkify a URL used as a mention label', () => {
const { container } = render(<CommentRichText text="@[https://evil.test/x](asset:aaa111)" />);
expect(container.querySelector('a')).toBeNull();
expect(screen.getByRole('button', { name: '@https://evil.test/x' })).toBeInTheDocument();
});
// `renderUrls` used to key its fragments by the index within its own slice, and
// CommentRichText calls it once per gap between mentions, so the same key ("txt-0") was
// emitted for several siblings and React warned that children may be duplicated or
// omitted. The keys carry the slice offset now.
it('emits no duplicate React keys when text surrounds a mention', () => {
const { container } = render(
<CommentRichText text="Before @[One](asset:aaa111) middle @[Two](asset:bbb222) after" />
);
expect(container).toHaveTextContent('Before @One middle @Two after');
expect(consoleError).not.toHaveBeenCalledWith(
expect.stringContaining('same key'),
expect.anything()
);
});
});