mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-26 20:06:26 -08:00
changeset and tests
This commit is contained in:
4 files changed
+170
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
default: patch
|
||||
---
|
||||
|
||||
Fixed per-message profile proxies not unwrapping and generally just not working.
|
||||
@@ -0,0 +1,85 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import {
|
||||
plainToEditorInput,
|
||||
toMatrixCustomHTML,
|
||||
toPlainText,
|
||||
trimCustomHtml,
|
||||
} from '$components/editor';
|
||||
import { outgoingMessageTransforms } from './outgoingMessageTransforms';
|
||||
import { buildSettingsLink, getSettingsLinkLabel } from '$features/settings/settingsLink';
|
||||
|
||||
function runOutgoingPipeline(input: string, settingsLinkBaseUrl = 'https://app.example') {
|
||||
let children = plainToEditorInput(input);
|
||||
const context = { settingsLinkBaseUrl };
|
||||
|
||||
outgoingMessageTransforms.forEach((t) => {
|
||||
if (!t.shouldApply(children, context)) return;
|
||||
children = t.apply(children, context);
|
||||
});
|
||||
|
||||
const plain = toPlainText(children, true).trim();
|
||||
const html = trimCustomHtml(
|
||||
toMatrixCustomHTML(children, {
|
||||
stripNickname: true,
|
||||
nickNameReplacement: new Map(),
|
||||
})
|
||||
);
|
||||
|
||||
return { children, plain, html };
|
||||
}
|
||||
|
||||
describe('PMP proxy outgoing pipeline parity', () => {
|
||||
it('renders markdown like normal messages (bold)', () => {
|
||||
const { plain, html } = runOutgoingPipeline('**bold**');
|
||||
expect(plain).toBe('**bold**');
|
||||
// Our markdown pipeline injects `data-md` markers for round-tripping.
|
||||
expect(html).toMatch(/<strong[^>]*>bold<\/strong>/);
|
||||
});
|
||||
|
||||
it('preserves markdown links in plaintext and renders anchors in html', () => {
|
||||
const { plain, html } = runOutgoingPipeline('[Sable](https://example.com)');
|
||||
expect(plain).toBe('[Sable](https://example.com)');
|
||||
expect(html).toContain('<a href="https://example.com"');
|
||||
expect(html).toContain('>Sable</a>');
|
||||
});
|
||||
|
||||
it('escapes raw html so it is not treated as markup', () => {
|
||||
const { plain, html } = runOutgoingPipeline('<b>nope</b>');
|
||||
expect(plain).toBe('<b>nope</b>');
|
||||
// markdownToHtml sanitizes/strips raw tags; ensure it does not render as actual <b>.
|
||||
expect(html).toContain('nope');
|
||||
expect(html).not.toContain('<b>nope</b>');
|
||||
});
|
||||
|
||||
it('applies outgoing transforms (settings link rewrite) like normal messages', () => {
|
||||
const base = 'https://app.example';
|
||||
const url = buildSettingsLink(base, 'appearance', 'message-link-preview');
|
||||
const label = getSettingsLinkLabel('appearance', 'message-link-preview');
|
||||
|
||||
const { plain, html } = runOutgoingPipeline(`see ${url}`, base);
|
||||
|
||||
expect(plain).toContain(`[${label}](${url})`);
|
||||
// HTML encodes & as & in attributes
|
||||
const encodedUrl = url.replaceAll('&', '&');
|
||||
expect(html).toContain(`<a href="${encodedUrl}"`);
|
||||
const encodedLabel = label.replaceAll('>', '>');
|
||||
expect(html).toContain(`>${encodedLabel}</a>`);
|
||||
});
|
||||
|
||||
it('supports multi-paragraph messages (keeps line breaks)', () => {
|
||||
const { plain, html } = runOutgoingPipeline('first line\n\nsecond line');
|
||||
// Plaintext keeps the blank line separation
|
||||
expect(plain).toBe('first line\n\nsecond line');
|
||||
// HTML should contain two paragraphs
|
||||
expect(html).toMatch(/<p>first line<\/p>[\s\S]*<p>second line<\/p>/);
|
||||
});
|
||||
|
||||
it('supports fenced code blocks and renders them as code', () => {
|
||||
const md = ['```ts', 'const x = 1', '```'].join('\n');
|
||||
const { plain, html } = runOutgoingPipeline(md);
|
||||
expect(plain).toBe(md);
|
||||
// Allow flexibility in the exact HTML produced by the markdown pipeline
|
||||
expect(html).toMatch(/<pre[\s\S]*><code[\s\S]*>[\s\S]*const x = 1[\s\S]*<\/code><\/pre>/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { parsePerMessageProfileProxyAssociation } from './usePerMessageProfile';
|
||||
|
||||
describe('parsePerMessageProfileProxyAssociation', () => {
|
||||
it('parses a regex string with flags (RegExp#toString form)', () => {
|
||||
const assoc = {
|
||||
profileId: 'p1',
|
||||
regexString: '/^\\[text\\] (.+)$/i',
|
||||
setAt: 123,
|
||||
};
|
||||
|
||||
const parsed = parsePerMessageProfileProxyAssociation(assoc);
|
||||
expect(parsed.profileId).toBe('p1');
|
||||
expect(parsed.setAt).toBe(123);
|
||||
expect(parsed.regex.test('[text] Hello')).toBe(true);
|
||||
expect(parsed.regex.test('[TEXT] hello')).toBe(true); // i flag
|
||||
});
|
||||
|
||||
it('parses a regex string without flags', () => {
|
||||
const assoc = {
|
||||
profileId: 'p1',
|
||||
regexString: '/^\\[(.+)\\]$/',
|
||||
};
|
||||
|
||||
const parsed = parsePerMessageProfileProxyAssociation(assoc);
|
||||
expect(parsed.regex.test('[ok]')).toBe(true);
|
||||
expect(parsed.regex.test('[no] trailing')).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
import type { Mock } from 'vitest';
|
||||
import { describe, expect, it, vi, beforeEach } from 'vitest';
|
||||
|
||||
import { PKitProxyMessageHandler } from './PKitProxyMessageHandler';
|
||||
import type { MatrixClient } from '$types/matrix-sdk';
|
||||
|
||||
// Mock the hook module that provides proxy associations + profile lookup
|
||||
vi.mock('$hooks/usePerMessageProfile', () => ({
|
||||
getAllPerMessageProfileProxies: vi.fn<() => Promise<unknown[]>>(),
|
||||
getPerMessageProfileById: vi.fn<() => Promise<unknown>>(),
|
||||
parsePerMessageProfileProxyAssociation: vi.fn<() => unknown>(),
|
||||
}));
|
||||
|
||||
const mocked = await import('$hooks/usePerMessageProfile');
|
||||
|
||||
describe('PKitProxyMessageHandler', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
it('returns false for isAProxiedMessage before init', () => {
|
||||
const handler = new PKitProxyMessageHandler({} as unknown as MatrixClient);
|
||||
expect(handler.isAProxiedMessage('[test] hi')).toBe(false);
|
||||
});
|
||||
|
||||
it('matches a proxied message, returns pmp, and strips content', async () => {
|
||||
const proxyRegex = /^\[(.+)\]$/;
|
||||
|
||||
(mocked.getAllPerMessageProfileProxies as unknown as Mock).mockResolvedValueOnce([
|
||||
{ profileId: 'p1', regexString: proxyRegex.toString() },
|
||||
]);
|
||||
(mocked.parsePerMessageProfileProxyAssociation as unknown as Mock).mockReturnValueOnce({
|
||||
profileId: 'p1',
|
||||
regex: proxyRegex,
|
||||
});
|
||||
(mocked.getPerMessageProfileById as unknown as Mock).mockResolvedValueOnce({
|
||||
id: 'p1',
|
||||
name: 'Test',
|
||||
});
|
||||
|
||||
const handler = new PKitProxyMessageHandler({} as unknown as MatrixClient);
|
||||
|
||||
const pmp = await handler.getPmpBasedOnMessage('[hello]');
|
||||
expect(pmp).toEqual({ id: 'p1', name: 'Test' });
|
||||
|
||||
// getPmpBasedOnMessage refreshes/init() so we should be inited now
|
||||
expect(handler.isAProxiedMessage('[hello]')).toBe(true);
|
||||
expect(handler.stripProxyFromMessage('[hello]')).toBe('hello');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user