mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-14 02:24:58 +00:00
This breaks down reading and writing into low-level and high level sagas. Opening now takes a lock on the file so that it can only have one writer or have multiple readers. The low-level read and write sagas use the file descriptor to determine if the file is still open before actually performing the action. Since we are using the web locks api, these locks should work across browser tabs/windows. The high-level read/write sagas perform the low-level open, read/write, close operations in a single call. The high-level delete and rename sagas are also updated to take locks on the files while performing the respective operations and will fail if the file is already being used somewhere else.
93 lines
2.6 KiB
TypeScript
93 lines
2.6 KiB
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (c) 2020-2022 The Pybricks Authors
|
|
|
|
// jest-dom adds custom jest matchers for asserting on DOM nodes.
|
|
// allows you to do things like:
|
|
// expect(element).toHaveTextContent(/react/i)
|
|
// learn more: https://github.com/testing-library/jest-dom
|
|
import '@testing-library/jest-dom/extend-expect';
|
|
import 'navigator.locks';
|
|
import crypto from 'crypto';
|
|
import { inspect } from 'util';
|
|
import {
|
|
KeyCodes,
|
|
Modifiers,
|
|
} from '@blueprintjs/core/lib/cjs/components/hotkeys/hotkeyParser';
|
|
// @ts-expect-error no typings
|
|
import matchMediaPolyfill from 'mq-polyfill';
|
|
|
|
//https://stackoverflow.com/a/66515427/1976323
|
|
matchMediaPolyfill(window);
|
|
|
|
// implementation of window.resizeTo for dispatching event
|
|
window.resizeTo = function resizeTo(width, height) {
|
|
Object.assign(this, {
|
|
innerWidth: width,
|
|
innerHeight: height,
|
|
outerWidth: width,
|
|
outerHeight: height,
|
|
}).dispatchEvent(new this.Event('resize'));
|
|
};
|
|
|
|
// scroll functions are not implemented in jsdom
|
|
// https://github.com/jsdom/jsdom/issues/1695
|
|
|
|
if (!Element.prototype.scrollTo) {
|
|
Element.prototype.scrollTo = jest.fn();
|
|
}
|
|
|
|
// HACK: work around https://github.com/palantir/blueprint/issues/4165
|
|
// userEvent.keyboard does not set which, so we have to do a reverse lookup
|
|
// using the blueprintjs keymap.
|
|
|
|
// handle cases that are not simple lower case conversion
|
|
const specialCases: Record<string, string> = {
|
|
Control: 'ctrl',
|
|
' ': 'space',
|
|
ArrowDown: 'down',
|
|
ArrowLeft: 'left',
|
|
ArrowRight: 'right',
|
|
ArrowUp: 'up',
|
|
Delete: 'del',
|
|
Insert: 'ins',
|
|
Escape: 'esc',
|
|
};
|
|
|
|
function addWhichToKeyboardEvent(e: KeyboardEvent) {
|
|
const blueprintsKeyName = specialCases[e.key] ?? e.key.toLowerCase();
|
|
let which = 0;
|
|
|
|
for (const [k, v] of Object.entries(KeyCodes).concat(Object.entries(Modifiers))) {
|
|
if (v === blueprintsKeyName) {
|
|
which = Number(k);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (which === 0) {
|
|
console.warn('unsupported key:', e.key);
|
|
}
|
|
|
|
Object.defineProperty(e, 'which', { value: which });
|
|
}
|
|
|
|
document.addEventListener('keydown', addWhichToKeyboardEvent);
|
|
document.addEventListener('keypress', addWhichToKeyboardEvent);
|
|
document.addEventListener('keyup', addWhichToKeyboardEvent);
|
|
|
|
Object.defineProperty(global.self, 'crypto', {
|
|
value: crypto.webcrypto,
|
|
});
|
|
|
|
// https://github.com/facebook/jest/issues/11698
|
|
function fail(reason: unknown): never {
|
|
if (typeof reason === 'string') {
|
|
throw new Error(reason);
|
|
}
|
|
throw new Error(inspect(reason));
|
|
}
|
|
|
|
if (global.fail === undefined) {
|
|
global.fail = fail;
|
|
}
|