Files
raucao f341c228b8 Fix site icons cached from non-image responses
A response with no Content-Type reached FileReader as application/octet-stream, which the browser refuses to render, and a mismatched mime field previously marked such records as usable. Judge cached icons by their data URL MIME, sniff image bytes when the content type is missing or generic, reject markup, and fall back to the initial when an image fails to load.
2026-10-30 10:20:37 +04:00

162 lines
5.4 KiB
Plaintext

import { module, test } from 'qunit';
import { setupRenderingTest } from 'dashtab/tests/helpers';
import { render } from '@ember/test-helpers';
import SiteLinksItem from 'dashtab/components/site-links/item';
const PNG_ICON =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==';
module('Integration | Component | site-links/item', function (hooks) {
setupRenderingTest(hooks);
test('it renders a link to the site with an unread count', async function (assert) {
this.set('item', {
id: 'discourse-abc',
type: 'discourse',
label: 'forum.example.com',
href: 'https://forum.example.com',
status: 'ready',
unreadCount: 3,
});
await render(<template><SiteLinksItem @item={{this.item}} /></template>);
assert.dom('a').hasAttribute('href', 'https://forum.example.com');
assert.dom('a').doesNotHaveAttribute('target', '_blank');
assert.dom('a').hasAttribute('title', 'forum.example.com');
assert.dom('a').hasAttribute('aria-label', 'forum.example.com');
assert.dom('[data-test-site-links-count]').hasText('3');
assert.dom('[data-test-site-links-fallback]').hasText('F');
});
test('it links to the notifications page when there are unread items', async function (assert) {
this.set('item', {
id: 'gitea-git.example.com',
type: 'gitea',
label: 'git.example.com',
href: 'https://git.example.com',
notificationsHref: 'https://git.example.com/notifications',
status: 'ready',
unreadCount: 2,
});
await render(<template><SiteLinksItem @item={{this.item}} /></template>);
assert
.dom('a')
.hasAttribute('href', 'https://git.example.com/notifications');
});
test('it links to the site root when there are no unread items', async function (assert) {
this.set('item', {
id: 'gitea-git.example.com',
type: 'gitea',
label: 'git.example.com',
href: 'https://git.example.com',
notificationsHref: 'https://git.example.com/notifications',
status: 'ready',
unreadCount: 0,
});
await render(<template><SiteLinksItem @item={{this.item}} /></template>);
assert.dom('a').hasAttribute('href', 'https://git.example.com');
});
test('it renders the fetched site icon when one is available', async function (assert) {
this.set('item', {
id: 'discourse-abc',
label: 'forum.example.com',
href: 'https://forum.example.com',
status: 'ready',
unreadCount: 1,
icon: {
dataUrl: PNG_ICON,
maskable: true,
themeColor: '#1d4ed8',
},
});
await render(<template><SiteLinksItem @item={{this.item}} /></template>);
assert.dom('[data-test-site-links-icon]').exists();
assert.dom('[data-test-site-links-fallback]').doesNotExist();
assert.dom('[data-test-site-links-icon]').hasAttribute('src', PNG_ICON);
assert.dom('[data-test-site-links-icon]').hasClass('object-cover');
assert
.dom('[data-test-site-links-tile]')
.hasAttribute('style', /background-color:\s*rgb\(29, 78, 216\)/);
});
test('it fits a non-maskable icon inside the tile', async function (assert) {
this.set('item', {
label: 'forum.example.com',
href: 'https://forum.example.com',
status: 'ready',
icon: { dataUrl: PNG_ICON, maskable: false },
});
await render(<template><SiteLinksItem @item={{this.item}} /></template>);
assert.dom('[data-test-site-links-icon]').hasClass('object-contain');
assert.dom('[data-test-site-links-icon]').hasClass('rounded-md');
assert.dom('[data-test-site-links-icon]').doesNotHaveClass('size-full');
});
test('it ignores a theme color that is not a plain CSS color', async function (assert) {
this.set('item', {
label: 'forum.example.com',
href: 'https://forum.example.com',
status: 'ready',
icon: {
dataUrl: PNG_ICON,
maskable: true,
themeColor: 'red; background-image: url(evil)',
},
});
await render(<template><SiteLinksItem @item={{this.item}} /></template>);
assert
.dom('[data-test-site-links-tile]')
.doesNotHaveAttribute('style', /background-color/);
});
test('it hides the count when there are no unread notifications', async function (assert) {
this.set('item', {
label: 'forum.example.com',
href: 'https://forum.example.com',
status: 'ready',
unreadCount: 0,
});
await render(<template><SiteLinksItem @item={{this.item}} /></template>);
assert.dom('[data-test-site-links-count]').doesNotExist();
assert.dom('[data-test-site-links-fallback]').exists();
});
test('it explains an unreachable site in the tooltip', async function (assert) {
this.set('item', {
label: 'forum.example.com',
href: 'https://forum.example.com',
status: 'error',
errorType: 'unreachable',
error: 'Notifications blocked — enable CORS on the forum or open it',
unreadCount: 0,
});
await render(<template><SiteLinksItem @item={{this.item}} /></template>);
assert.dom('a').hasAttribute('data-status', 'error');
assert.dom('a').hasAttribute('data-error-type', 'unreachable');
assert
.dom('a')
.hasAttribute(
'title',
'forum.example.com — Notifications blocked — enable CORS on the forum or open it'
);
assert.dom('a').hasAttribute('href');
});
});