mirror of
https://github.com/HeyPuter/puter.git
synced 2026-11-25 07:09:11 +00:00
Apps get `utm_content` set to how the user opened them: `app_icon`, `file_open`, `open_with` or `file_drop`. Apps opened from the app browser embed get the framing page's host as `utm_source`, which the server reads from the same referrer check that pins `frame-ancestors`.
273 lines
9.6 KiB
JavaScript
273 lines
9.6 KiB
JavaScript
// @vitest-environment jsdom
|
|
/*
|
|
* Copyright (C) 2024-present Puter Technologies Inc.
|
|
*
|
|
* This file is part of Puter.
|
|
*
|
|
* Puter is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import jQuery from '../lib/jquery-3.6.1/jquery-3.6.1.min.js';
|
|
|
|
vi.mock('./UIWindowItemProperties.js', () => ({ default: vi.fn() }));
|
|
vi.mock('./UIWindowSaveAccount.js', () => ({ default: vi.fn() }));
|
|
vi.mock('./UIPopover.js', () => ({ default: vi.fn() }));
|
|
vi.mock('./UIWindowEmailConfirmationRequired.js', () => ({ default: vi.fn() }));
|
|
vi.mock('./UIContextMenu.js', () => ({ default: vi.fn() }));
|
|
vi.mock('./UIAlert.js', () => ({ default: vi.fn() }));
|
|
vi.mock('../helpers/launchApp.js', () => ({ default: vi.fn() }));
|
|
vi.mock('../helpers/openItem.js', () => ({ default: vi.fn() }));
|
|
vi.mock('../helpers/openWithAI.js', () => ({ default: vi.fn() }));
|
|
vi.mock('../helpers/refreshItemContainer.js', () => ({ default: vi.fn() }));
|
|
|
|
globalThis.$ = jQuery;
|
|
globalThis.jQuery = jQuery;
|
|
globalThis.def = globalThis.def ?? ((value) => value);
|
|
globalThis.use = globalThis.use ?? (() => class {});
|
|
globalThis.html_encode = (value) =>
|
|
jQuery('<div>')
|
|
.text(value ?? '')
|
|
.html();
|
|
globalThis.i18n = (key) => key;
|
|
globalThis.isMobile = { phone: false, tablet: false };
|
|
globalThis.timeago = { format: () => '' };
|
|
for (const plugin of ['draggable', 'droppable', 'dragster']) {
|
|
jQuery.fn[plugin] = function () {
|
|
return this;
|
|
};
|
|
}
|
|
jQuery.fn.removeItems = function () {
|
|
return this.remove();
|
|
};
|
|
window.icons = {};
|
|
Object.assign(window, {
|
|
determine_website_url: () => null,
|
|
check_fsentry_against_allowed_file_types_string: () => true,
|
|
toggle_empty_folder_message: () => {},
|
|
update_explorer_footer_item_count: () => {},
|
|
byte_format: (bytes) => `${bytes} B`,
|
|
suggest_apps_for_fsentry: async () => [],
|
|
feature_flags: {},
|
|
clipboard: [],
|
|
});
|
|
|
|
const { default: UIItem } = await import('./UIItem.js');
|
|
const { default: UIContextMenu } = await import('./UIContextMenu.js');
|
|
const { default: UIAlert } = await import('./UIAlert.js');
|
|
const { unshareAnswering, allRefused, goneRefused } =
|
|
await import('../../tests/fixtures/sdkUnshare.js');
|
|
const { default: refresh_item_container } =
|
|
await import('../helpers/refreshItemContainer.js');
|
|
const { default: launch_app } = await import('../helpers/launchApp.js');
|
|
|
|
const menuItems = async (options) => {
|
|
document.body.innerHTML =
|
|
'<div class="window"><div class="item-container"></div></div>';
|
|
await UIItem({
|
|
appendTo: $('.item-container'),
|
|
uid: 'u-1',
|
|
path: '/bob/u-1/Photos',
|
|
name: 'Photos',
|
|
is_dir: true,
|
|
icon: { image: '', type: 'icon' },
|
|
shared_with_me: true,
|
|
share_mode: 'read',
|
|
shared_by: 'bob',
|
|
owner: 'bob',
|
|
...options,
|
|
});
|
|
UIContextMenu.mockClear();
|
|
$('.item').trigger('contextmenu');
|
|
await vi.waitFor(() => expect(UIContextMenu).toHaveBeenCalled());
|
|
return UIContextMenu.mock.calls[0][0].items;
|
|
};
|
|
|
|
const menuLabels = async (options) =>
|
|
(await menuItems(options)).map((item) => item?.html);
|
|
|
|
const removeFromShared = async (options) => {
|
|
await menuLabels(options);
|
|
await UIContextMenu.mock.calls[0][0].items
|
|
.find((item) => item?.html === 'share_remove_from_shared')
|
|
.onClick();
|
|
};
|
|
|
|
describe('UIItem Remove from Shared', () => {
|
|
beforeEach(() => {
|
|
window.user = { username: 'alice' };
|
|
window.trash_path = '/alice/Trash';
|
|
window.puter = {
|
|
fs: {
|
|
listShared: vi.fn(async () => ({ items: [] })),
|
|
unshare: vi.fn(async () => ({ revoked: 1 })),
|
|
},
|
|
};
|
|
globalThis.puter = window.puter;
|
|
refresh_item_container.mockClear();
|
|
UIAlert.mockClear();
|
|
});
|
|
|
|
it('is offered on an item shared with the user directly', async () => {
|
|
expect(await menuLabels({})).toContain('share_remove_from_shared');
|
|
});
|
|
|
|
it('is not offered on an item shared through a team', async () => {
|
|
const labels = await menuLabels({ is_team_share: true });
|
|
expect($('.item').attr('data-is_team_share')).toBe('1');
|
|
expect(labels).not.toContain('share_remove_from_shared');
|
|
});
|
|
|
|
it('keeps the item and shows the error when leaving is refused', async () => {
|
|
puter.fs.unshare = unshareAnswering(allRefused);
|
|
const items = await menuItems({});
|
|
await items
|
|
.find((item) => item?.html === 'share_remove_from_shared')
|
|
.onClick();
|
|
|
|
expect(UIAlert).toHaveBeenCalledWith({
|
|
message: 'Held through a team',
|
|
});
|
|
expect($('.item').length).toBe(1);
|
|
});
|
|
|
|
it('keeps the item and shows why when the unshare resolves with the pair refused', async () => {
|
|
puter.fs.unshare = unshareAnswering(goneRefused);
|
|
await removeFromShared({});
|
|
|
|
expect(UIAlert).toHaveBeenCalledWith({
|
|
message: 'Subject does not exist',
|
|
});
|
|
expect($('.item').length).toBe(1);
|
|
});
|
|
|
|
it('takes the item away once the user has left it', async () => {
|
|
await removeFromShared({});
|
|
expect(puter.fs.unshare).toHaveBeenCalledWith('/bob/u-1/Photos', 'alice');
|
|
expect($('.item')).toHaveLength(0);
|
|
expect(refresh_item_container).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('lists the item again when a team still shares it', async () => {
|
|
await removeFromShared({ is_also_team_share: true });
|
|
expect(puter.fs.unshare).toHaveBeenCalledWith('/bob/u-1/Photos', 'alice');
|
|
expect($('.item')).toHaveLength(1);
|
|
expect(refresh_item_container).toHaveBeenCalledTimes(1);
|
|
expect(refresh_item_container.mock.calls[0][0][0]).toBe($('.item-container')[0]);
|
|
});
|
|
});
|
|
|
|
describe('UIItem Share', () => {
|
|
beforeEach(() => {
|
|
window.user = { username: 'alice' };
|
|
window.trash_path = '/alice/Trash';
|
|
});
|
|
|
|
it('is not offered, even on an item the user owns', async () => {
|
|
const labels = await menuLabels({
|
|
path: '/alice/Photos',
|
|
owner: 'alice',
|
|
shared_with_me: false,
|
|
share_mode: undefined,
|
|
shared_by: undefined,
|
|
});
|
|
expect(labels).not.toContain('share_ellipsis');
|
|
});
|
|
});
|
|
|
|
describe('UIItem Publish as Worker', () => {
|
|
beforeEach(() => {
|
|
window.user = { username: 'alice' };
|
|
window.trash_path = '/alice/Trash';
|
|
});
|
|
|
|
it('is not offered, even on a .js file the user owns', async () => {
|
|
const labels = await menuLabels({
|
|
path: '/alice/worker.js',
|
|
name: 'worker.js',
|
|
is_dir: false,
|
|
owner: 'alice',
|
|
shared_with_me: false,
|
|
share_mode: undefined,
|
|
shared_by: undefined,
|
|
});
|
|
expect(labels).not.toContain('publish_as_serverless_worker');
|
|
});
|
|
});
|
|
|
|
describe('UIItem Publish as Website', () => {
|
|
beforeEach(() => {
|
|
window.user = { username: 'alice' };
|
|
window.trash_path = '/alice/Trash';
|
|
});
|
|
|
|
it('is not offered, even on a folder the user owns', async () => {
|
|
const labels = await menuLabels({
|
|
path: '/alice/Site',
|
|
name: 'Site',
|
|
is_dir: true,
|
|
owner: 'alice',
|
|
shared_with_me: false,
|
|
share_mode: undefined,
|
|
shared_by: undefined,
|
|
});
|
|
expect(labels).not.toContain('publish_as_website');
|
|
expect(labels).toContain('deploy_as_app');
|
|
});
|
|
});
|
|
|
|
describe('UIItem Open With', () => {
|
|
const editor = { name: 'editor', title: 'Editor', uuid: 'app-editor' };
|
|
const code = { name: 'code', title: 'Code', uuid: 'app-code' };
|
|
|
|
const openWithItems = async () => {
|
|
const items = await menuItems({
|
|
uid: 'f-1',
|
|
path: '/alice/notes.txt',
|
|
name: 'notes.txt',
|
|
is_dir: false,
|
|
shared_with_me: false,
|
|
suggested_apps: [editor, code],
|
|
});
|
|
return items.find((item) => item?.html === 'open_with').items;
|
|
};
|
|
|
|
beforeEach(() => {
|
|
window.user = { username: 'alice' };
|
|
window.trash_path = '/alice/Trash';
|
|
window.user_preferences = { 'default_apps.txt': code.uuid };
|
|
window.mutate_user_preferences = vi.fn();
|
|
launch_app.mockClear();
|
|
UIAlert.mockClear();
|
|
});
|
|
|
|
it('lists the default app first', async () => {
|
|
const items = await openWithItems();
|
|
expect(items.slice(0, 2).map((item) => item.html)).toEqual(['Code', 'Editor']);
|
|
});
|
|
|
|
it('opens another app without asking to make it the default', async () => {
|
|
await (await openWithItems()).find((item) => item.html === 'Editor').onClick();
|
|
expect(UIAlert).not.toHaveBeenCalled();
|
|
expect(window.mutate_user_preferences).not.toHaveBeenCalled();
|
|
expect(launch_app).toHaveBeenCalledWith({
|
|
name: 'editor',
|
|
file_path: '/alice/notes.txt',
|
|
window_title: 'notes.txt',
|
|
file_uid: 'f-1',
|
|
trigger: 'open_with',
|
|
});
|
|
});
|
|
});
|