A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.
TypeScript animationcartoonclip-studio-paintcollaborativedesktop-appdigital-paintingdrawing-appillustrationmangamultiplayeropen-sourcerusttaurithreejswebglwebtoon
import { act } from 'react';
import { createRoot, Root } from 'react-dom/client';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { CrashRecoveryModal } from '../../components/CrashRecoveryModal';
import { CrashedSessionInfo } from '../../services/recoveryService';
interface MiniNode {
nodeType: number;
nodeName: string;
parentNode: MiniElement | null;
ownerDocument: MiniDocument;
}
class MiniTextNode implements MiniNode {
public nodeType = 3;
public nodeName = '#text';
public nodeValue: string;
public parentNode: MiniElement | null = null;
public ownerDocument: MiniDocument;
constructor(text: string, ownerDocument: MiniDocument) {
this.nodeValue = text;
this.ownerDocument = ownerDocument;
}
}
class MiniCommentNode implements MiniNode {
public nodeType = 8;
public nodeName = '#comment';
public nodeValue: string;
public parentNode: MiniElement | null = null;
public ownerDocument: MiniDocument;
constructor(text: string, ownerDocument: MiniDocument) {
this.nodeValue = text;
this.ownerDocument = ownerDocument;
}
}
class MiniElement extends EventTarget implements MiniNode {
public nodeType = 1;
public nodeName: string;
public tagName: string;
public namespaceURI = 'http://www.w3.org/1999/xhtml';
public ownerDocument: MiniDocument;
public parentNode: MiniElement | null = null;
public childNodes: MiniNode[] = [];
public style: Record<string, string> = {};
public disabled = false;
public className = '';
public isContentEditable = false;
public value = '';
public checked = false;
private attrs = new Map<string, string>();
constructor(tag: string, ownerDocument: MiniDocument) {
super();
this.nodeName = tag.toUpperCase();
this.tagName = tag.toUpperCase();
this.ownerDocument = ownerDocument;
}
get firstChild(): MiniNode | null {
return this.childNodes[0] ?? null;
}
get textContent(): string {
return this.childNodes
.map((n) => (n instanceof MiniTextNode ? n.nodeValue : n instanceof MiniElement ? n.textContent : ''))
.join('');
}
set textContent(val: string) {
this.childNodes = val ? [new MiniTextNode(val, this.ownerDocument)] : [];
}
appendChild<T extends MiniNode>(child: T): T {
if (child.parentNode) {
child.parentNode.removeChild(child);
}
child.parentNode = this;
this.childNodes.push(child);
return child;
}
insertBefore<T extends MiniNode>(child: T, ref: MiniNode | null): T {
if (!ref) return this.appendChild(child);
if (child.parentNode) {
child.parentNode.removeChild(child);
}
const idx = this.childNodes.indexOf(ref);
child.parentNode = this;
if (idx === -1) {
this.childNodes.push(child);
} else {
this.childNodes.splice(idx, 0, child);
}
return child;
}
removeChild<T extends MiniNode>(child: T): T {
const idx = this.childNodes.indexOf(child);
if (idx !== -1) {
this.childNodes.splice(idx, 1);
child.parentNode = null;
}
return child;
}
setAttribute(name: string, value: string): void {
this.attrs.set(name, String(value));
if (name === 'class') this.className = String(value);
if (name === 'disabled') this.disabled = true;
if (name === 'value') this.value = String(value);
}
getAttribute(name: string): string | null {
return this.attrs.get(name) ?? null;
}
removeAttribute(name: string): void {
this.attrs.delete(name);
if (name === 'disabled') this.disabled = false;
}
hasAttribute(name: string): boolean {
return this.attrs.has(name);
}
focus(): void {
this.ownerDocument.activeElement = this;
}
blur(): void {
if (this.ownerDocument.activeElement === this) {
this.ownerDocument.activeElement = null;
}
}
override dispatchEvent(event: Event): boolean {
const path: EventTarget[] = [];
let cur: MiniElement | null = this;
while (cur) {
path.push(cur);
cur = cur.parentNode;
}
path.push(this.ownerDocument);
if (this.ownerDocument.defaultView) {
path.push(this.ownerDocument.defaultView);
}
Object.defineProperty(event, 'target', { value: this, configurable: true });
for (const node of path) {
Object.defineProperty(event, 'currentTarget', { value: node, configurable: true });
EventTarget.prototype.dispatchEvent.call(node, event);
if (event.cancelBubble) break;
}
return !event.defaultPrevented;
}
}
class MiniDocument extends EventTarget {
public nodeType = 9;
public nodeName = '#document';
public defaultView: EventTarget | null = null;
public documentElement: MiniElement;
public body: MiniElement;
public activeElement: MiniElement | null = null;
constructor() {
super();
this.documentElement = new MiniElement('html', this);
this.body = new MiniElement('body', this);
this.documentElement.appendChild(this.body);
}
createElement(tag: string): MiniElement {
return new MiniElement(tag, this);
}
createElementNS(_ns: string, tag: string): MiniElement {
return new MiniElement(tag, this);
}
createTextNode(text: string): MiniTextNode {
return new MiniTextNode(text, this);
}
createComment(text: string): MiniCommentNode {
return new MiniCommentNode(text, this);
}
}
function findElements(root: MiniElement, pred: (el: MiniElement) => boolean): MiniElement[] {
const result: MiniElement[] = [];
const walk = (el: MiniElement) => {
if (pred(el)) result.push(el);
for (const child of el.childNodes) {
if (child instanceof MiniElement) walk(child);
}
};
walk(root);
return result;
}
function findByTestId(root: MiniElement, testId: string): MiniElement | undefined {
return findElements(root, (el) => el.getAttribute('data-testid') === testId)[0];
}
function clickElement(el: MiniElement): void {
const evt = new Event('click', { bubbles: true, cancelable: true });
el.dispatchEvent(evt);
}
const makeSession = (id: string, name: string): CrashedSessionInfo => ({
session_id: id,
timestamp: 1796081844201,
pid: 2412,
manifest_json: JSON.stringify({
documentName: name,
width: 1920,
height: 1080,
dpi: 300,
colorMode: 'color',
layers: [{ id: 'l1' }, { id: 'l2' }],
}),
has_thumbnail: false,
thumbnail_data_url: null,
});
describe('CrashRecoveryModal - Discard All Sessions', () => {
let doc: MiniDocument;
let win: EventTarget;
let container: MiniElement;
let root: Root;
const prevWindow = (globalThis as Record<string, unknown>).window;
const prevDocument = (globalThis as Record<string, unknown>).document;
const prevHTMLElement = (globalThis as Record<string, unknown>).HTMLElement;
const prevHTMLIFrameElement = (globalThis as Record<string, unknown>).HTMLIFrameElement;
const prevElement = (globalThis as Record<string, unknown>).Element;
const prevNode = (globalThis as Record<string, unknown>).Node;
const prevActEnv = (globalThis as Record<string, unknown>).IS_REACT_ACT_ENVIRONMENT;
beforeEach(() => {
(globalThis as Record<string, unknown>).IS_REACT_ACT_ENVIRONMENT = true;
const MockHTMLIFrameElement = class HTMLIFrameElement {};
doc = new MiniDocument();
win = Object.assign(new EventTarget(), {
document: doc,
HTMLElement: MiniElement,
HTMLIFrameElement: MockHTMLIFrameElement,
Element: MiniElement,
Node: MiniElement,
});
doc.defaultView = win;
container = doc.createElement('div');
doc.body.appendChild(container);
(globalThis as Record<string, unknown>).window = win;
(globalThis as Record<string, unknown>).document = doc;
(globalThis as Record<string, unknown>).HTMLElement = MiniElement;
(globalThis as Record<string, unknown>).HTMLIFrameElement = MockHTMLIFrameElement;
(globalThis as Record<string, unknown>).Element = MiniElement;
(globalThis as Record<string, unknown>).Node = MiniElement;
root = createRoot(container as unknown as Element);
});
afterEach(() => {
act(() => {
root.unmount();
});
(globalThis as Record<string, unknown>).window = prevWindow;
(globalThis as Record<string, unknown>).document = prevDocument;
(globalThis as Record<string, unknown>).HTMLElement = prevHTMLElement;
(globalThis as Record<string, unknown>).HTMLIFrameElement = prevHTMLIFrameElement;
(globalThis as Record<string, unknown>).Element = prevElement;
(globalThis as Record<string, unknown>).Node = prevNode;
(globalThis as Record<string, unknown>).IS_REACT_ACT_ENVIRONMENT = prevActEnv;
});
it('renders "Discard All Sessions" for a single session and invokes onDiscardAll with all sessions', async () => {
const sessions = [makeSession('doc-1', 'Untitled*')];
const onRestore = vi.fn(async () => {});
const onDiscard = vi.fn(async () => {});
const onDiscardAll = vi.fn(async () => {});
act(() => {
root.render(
<CrashRecoveryModal
isOpen
sessions={sessions}
onRestore={onRestore}
onDiscard={onDiscard}
onDiscardAll={onDiscardAll}
/>
);
});
const btn = findByTestId(container, 'discard-all-sessions-btn');
expect(btn).toBeDefined();
expect(btn?.textContent.trim()).toBe('Discard All Sessions');
await act(async () => {
clickElement(btn!);
});
expect(onDiscardAll).toHaveBeenCalledTimes(1);
expect(onDiscardAll).toHaveBeenCalledWith(sessions);
expect(onDiscard).not.toHaveBeenCalled();
});
it('shows count for multiple sessions, disables buttons while discarding all, and passes all sessions', async () => {
const sessions = [
makeSession('doc-1', 'Page 1'),
makeSession('doc-2', 'Page 2'),
makeSession('doc-3', 'Page 3'),
];
let resolveDiscardAll!: () => void;
const onDiscardAll = vi.fn(
() =>
new Promise<void>((resolve) => {
resolveDiscardAll = resolve;
})
);
act(() => {
root.render(
<CrashRecoveryModal
isOpen
sessions={sessions}
onRestore={vi.fn(async () => {})}
onDiscard={vi.fn(async () => {})}
onDiscardAll={onDiscardAll}
/>
);
});
const discardAllBtn = findByTestId(container, 'discard-all-sessions-btn');
expect(discardAllBtn?.textContent.trim()).toBe('Discard All Sessions (3)');
act(() => {
clickElement(discardAllBtn!);
});
expect(onDiscardAll).toHaveBeenCalledTimes(1);
expect(onDiscardAll).toHaveBeenCalledWith(sessions);
expect(discardAllBtn?.textContent.trim()).toBe('Discarding All...');
expect(discardAllBtn?.disabled).toBe(true);
// Extra clicks while busy are ignored
act(() => {
clickElement(discardAllBtn!);
});
expect(onDiscardAll).toHaveBeenCalledTimes(1);
await act(async () => {
resolveDiscardAll();
});
expect(discardAllBtn?.disabled).toBe(false);
expect(discardAllBtn?.textContent.trim()).toBe('Discard All Sessions (3)');
});
});