chore: remove legacy tests (#9183)

This commit is contained in:
liuyi
2024-12-17 17:06:28 +08:00
committed by GitHub
parent 27d4aa7ca7
commit b0e64fe516
79 changed files with 45 additions and 12448 deletions
-18
View File
@@ -1,18 +0,0 @@
import { dirname, join } from 'node:path';
import type { Page } from '@playwright/test';
declare global {
function readAffineDatabase(): Promise<any>;
function writeAffineDatabase(data: any, binaries: any): Promise<void>;
function readAffineLocalStorage(): Promise<any>;
function writeAffineLocalStorage(data: any): Promise<void>;
}
export async function patchDataEnhancement(page: Page) {
const idbPath = join(dirname(require.resolve('idb')), 'umd.js');
await page.addInitScript({ path: idbPath });
const patchPath = join(__dirname, './storage-patch.js');
await page.addInitScript({ path: patchPath });
}
-109
View File
@@ -1,109 +0,0 @@
import { readFile, writeFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { mkdirp, readJSON } from 'fs-extra';
interface SnapshotData {
idbData: Record<string, any>;
localStorageData: Record<string, string>;
binaries: Record<string, number[]>;
}
interface BinaryIndexData {
name: string;
start: number;
end: number;
}
export class SnapshotStorage {
idbFilePath: string;
localStorageFilePath: string;
binaryIndexPath: string;
binaryFilePath: string;
constructor(version: string) {
// The snapshots data is stored in "@affine-test/fixtures/legacy", just keep all fixtures in one place
const legacyReadMeFilePath = require.resolve(
'@affine-test/fixtures/legacy/README.md'
);
const dir = dirname(legacyReadMeFilePath);
this.idbFilePath = join(dir, version, 'idb.json');
this.binaryFilePath = join(dir, version, 'idb.bin');
this.binaryIndexPath = join(dir, version, 'idb_index.json');
this.localStorageFilePath = join(dir, version, 'local-storage.json');
}
async read(): Promise<SnapshotData> {
const {
idbFilePath,
localStorageFilePath,
binaryIndexPath,
binaryFilePath,
} = this;
const [idbData, localStorageData, binaryIndexArr, binaryContent] =
await Promise.all([
readJSON(idbFilePath),
readJSON(localStorageFilePath),
readJSON(binaryIndexPath) as Promise<BinaryIndexData[]>,
readFile(binaryFilePath),
]);
const binaries: Record<string, number[]> = {};
for (const index of binaryIndexArr) {
const chunk = binaryContent.subarray(index.start, index.end);
binaries[index.name] = Array.from(chunk);
}
return {
binaries,
idbData,
localStorageData,
};
}
async write(data: SnapshotData) {
const {
idbFilePath,
localStorageFilePath,
binaryIndexPath,
binaryFilePath,
} = this;
const { idbData, localStorageData, binaries } = data;
await mkdirp(dirname(idbFilePath));
const binaryIndexData: BinaryIndexData[] = [];
const binaryBuffers: Buffer[] = [];
let currentIndex = 0;
for (const [name, value] of Object.entries(binaries)) {
const buffer = Buffer.from(value);
const endIndex = currentIndex + buffer.length;
binaryIndexData.push({
name,
start: currentIndex,
end: endIndex,
});
binaryBuffers.push(buffer);
currentIndex += buffer.length;
}
await Promise.all([
writeFile(
localStorageFilePath,
JSON.stringify(localStorageData, null, 2),
'utf-8'
),
writeFile(idbFilePath, JSON.stringify(idbData, null, 2), 'utf-8'),
writeFile(
binaryIndexPath,
JSON.stringify(binaryIndexData, null, 2),
'utf-8'
),
writeFile(binaryFilePath, Buffer.concat(binaryBuffers), 'utf-8'),
]);
}
}
-141
View File
@@ -1,141 +0,0 @@
/**
* @type {import('idb')}
*/
const idb = window.idb;
const createUniqueIndex = (() => {
let index = 0;
return () => ++index;
})();
function replaceBinary(value, binaries) {
if (value instanceof Uint8Array) {
const name = `__BINARY__${createUniqueIndex()}`;
binaries[name] = Array.from(value);
return name;
}
if (Array.isArray(value)) {
return value.map(item => replaceBinary(item, binaries));
}
if (typeof value === 'object' && value !== null) {
const replaced = {};
for (const key of Object.keys(value)) {
replaced[key] = replaceBinary(value[key], binaries);
}
return replaced;
}
return value;
}
function recoveryBinary(value, binaries) {
if (typeof value === 'string') {
const arr = binaries[value];
if (arr) {
return new Uint8Array(arr);
}
}
if (Array.isArray(value)) {
return value.map(item => recoveryBinary(item, binaries));
}
if (typeof value === 'object' && value !== null) {
const result = {};
for (const key of Object.keys(value)) {
result[key] = recoveryBinary(value[key], binaries);
}
return result;
}
return value;
}
async function readAffineDatabase() {
const idbData = [];
const binaries = {};
const databases = await indexedDB.databases();
for (const databaseInfo of databases) {
const idbDatabase = await idb.openDB(
databaseInfo.name,
databaseInfo.version
);
if (!idbDatabase) {
throw new Error('idbDatabase is null');
}
const stores = [];
const objectStoreNames = Array.from(idbDatabase.objectStoreNames);
const transaction = idbDatabase.transaction(objectStoreNames, 'readonly');
for (const storeName of objectStoreNames) {
const objectStore = transaction.objectStore(storeName);
const objectValues = await objectStore.getAll();
stores.push({
name: storeName,
keyPath: objectStore.keyPath,
values: replaceBinary(objectValues, binaries),
});
}
idbData.push({ ...databaseInfo, stores });
}
return { idbData, binaries };
}
async function writeAffineDatabase(allDatabases, binaries) {
for (const database of allDatabases) {
const idbDatabase = await idb.openDB(database.name, database.version, {
upgrade(db) {
for (const objectStore of database.stores) {
db.createObjectStore(objectStore.name, {
keyPath: objectStore.keyPath,
});
}
},
});
for (const store of database.stores) {
const transaction = idbDatabase.transaction(store.name, 'readwrite');
const objectStore = transaction.objectStore(store.name);
for (const value of store.values) {
await objectStore.add(recoveryBinary(value, binaries));
}
}
}
}
async function readAffineLocalStorage() {
const data = {};
const keys = [
'jotai-workspaces',
'last_page_id',
'last_workspace_id',
'affine-local-workspace',
'is-first-open',
];
for (const key of keys) {
const value = window.localStorage.getItem(key);
data[key] = value;
}
return data;
}
async function writeAffineLocalStorage(data) {
for (const [key, value] of Object.entries(data)) {
window.localStorage.setItem(key, value);
}
}
window.readAffineDatabase = readAffineDatabase;
window.writeAffineDatabase = writeAffineDatabase;
window.readAffineLocalStorage = readAffineLocalStorage;
window.writeAffineLocalStorage = writeAffineLocalStorage;
+17 -22
View File
@@ -1,28 +1,23 @@
import { join } from 'node:path';
import type { Page } from '@playwright/test';
import { focusInlineEditor } from './page-logic';
export async function importImage(page: Page, pathInFixtures: string) {
await page.evaluate(() => {
// Force fallback to input[type=file] in tests
// See https://github.com/microsoft/playwright/issues/8850
// @ts-expect-error allow
window.showOpenFilePicker = undefined;
});
export async function importImage(page: Page, url: string) {
await focusInlineEditor(page);
await page.evaluate(
([url]) => {
const clipData = {
'text/html': `<img alt={'Sample image'} src=${url} />`,
};
const e = new ClipboardEvent('paste', {
clipboardData: new DataTransfer(),
});
Object.defineProperty(e, 'target', {
writable: false,
value: document,
});
Object.entries(clipData).forEach(([key, value]) => {
e.clipboardData?.setData(key, value);
});
document.dispatchEvent(e);
},
[url]
);
const fileChooser = page.waitForEvent('filechooser');
// open slash menu
await page.keyboard.type('/image', { delay: 50 });
await page.keyboard.press('Enter');
await (
await fileChooser
).setFiles(join(__dirname, '../../fixtures', pathInFixtures));
// TODO(@catsjuice): wait for image to be loaded more reliably
await page.waitForTimeout(1000);
}
-62
View File
@@ -1,62 +0,0 @@
import type Test from '@playwright/test';
import type { BrowserContext } from '@playwright/test';
import express from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';
import { waitForEditorLoad } from './page-logic';
export async function check8080Available(context: BrowserContext) {
// make sure 8080 is ready
const page = await context.newPage();
await page.goto('http://localhost:8080/');
await waitForEditorLoad(page);
await page.close();
}
export function setupProxyServer(test: typeof Test, dir: string) {
let app: express.Express;
let server: ReturnType<express.Express['listen']>;
test.beforeEach(() => {
app = express();
app.use(express.static(dir));
server = app.listen(8081);
});
test.afterEach(() => {
server.close();
});
return {
get app() {
return app;
},
get server() {
return server;
},
switchToNext: async function () {
// close previous express server
await new Promise<void>((resolve, reject) => {
server.close(err => {
if (err) {
reject(err);
}
resolve();
});
});
app = express();
app.use(
createProxyMiddleware({
target: 'http://localhost:8080',
pathFilter: ['**'],
changeOrigin: true,
})
);
return new Promise<void>(resolve => {
server = app.listen(8081, () => {
console.log('proxy to next.js server');
resolve();
});
});
},
};
}