wayBetter for X

This commit is contained in:
CRTkafa 2026-10-01 06:01:13 +07:00
commit 2f1e35ba20
720 changed files with 147465 additions and 0 deletions

View file

@ -0,0 +1,44 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import '../src/i18n/index.ts';
import { attachTarget as attachTargetCore, clampClipMax, clipPreview, folderLabel, isXUrl, relativeTime, withoutFolder } from '../src/shell/mediaModel.ts';
/** The framework's attach target with X's rule for pages that have a composer. */
const attachTarget = (tabs: Array<{ id: string; url: string }>, active: string | undefined, recent?: string[]) => attachTargetCore(tabs, active, isXUrl, recent);
test('only exact https://x.com tabs outside DMs/chat are attach targets', () => {
assert.ok(isXUrl('https://x.com/home'));
assert.ok(isXUrl('https://x.com/compose/post'));
for (const bad of ['http://x.com/home', 'https://x.com:8443/home', 'https://evil.x.com/', 'https://x.com.evil.io/', 'https://u:p@x.com/', 'https://twitter.com/home', 'https://x.com/messages/1', 'https://x.com/i/chat', 'https://x.com/i/grok', 'not a url']) {
assert.equal(isXUrl(bad), false, bad);
}
});
test('attach target prefers the active X tab, then recent, then any X tab', () => {
const tabs = [{ id: 'a', url: 'https://example.com/' }, { id: 'b', url: 'https://x.com/home' }, { id: 'c', url: 'https://x.com/explore' }];
assert.equal(attachTarget(tabs, 'c')?.id, 'c');
assert.equal(attachTarget(tabs, 'a', ['c', 'b'])?.id, 'c');
assert.equal(attachTarget(tabs, 'a')?.id, 'b');
assert.equal(attachTarget([{ id: 'a', url: 'https://example.com/' }], 'a'), null);
assert.equal(attachTarget([{ id: 'd', url: 'https://x.com/messages' }], 'd'), null);
});
test('settings helpers', () => {
assert.equal(folderLabel('C:\\Users\\you\\Pictures\\Screenshots\\'), 'Screenshots');
assert.equal(folderLabel('/Users/you/Desktop'), 'Desktop');
assert.equal(clampClipMax(0), 1);
assert.equal(clampClipMax(500), 100);
assert.equal(clampClipMax(Number.NaN), 30);
const settings = { folders: ['/a', '/b'], clipboardEnabled: false, clipboardMax: 30 };
assert.deepEqual(withoutFolder(settings, '/a').folders, ['/b']);
assert.deepEqual(settings.folders, ['/a', '/b']);
});
test('clip previews and times', () => {
assert.equal(clipPreview({ id: 'x', kind: 'text', text: ' hello \n world ', hash: 'h', at: 0 }), 'hello world');
assert.equal(clipPreview({ id: 'x', kind: 'text', text: 'a'.repeat(100), hash: 'h', at: 0 }, 10), `${'a'.repeat(9)}…`);
assert.equal(clipPreview({ id: 'x', kind: 'image', file: 'x.png', hash: 'h', at: 0 }), 'Image');
assert.equal(relativeTime(1000, 1000 + 30_000), 'now');
assert.equal(relativeTime(0, 5 * 60_000), '5m');
assert.equal(relativeTime(0, 3 * 3_600_000), '3h');
});