Apply new code format standard (#217)

This commit is contained in:
Chenlei Hu
2024-07-25 10:10:18 -04:00
committed by GitHub
parent 19c70d95d3
commit e179f75387
121 changed files with 11898 additions and 11983 deletions

View File

@@ -1,28 +1,28 @@
import "../../src/scripts/api";
import '../../src/scripts/api'
const fs = require("fs");
const path = require("path");
const fs = require('fs')
const path = require('path')
function* walkSync(dir: string): Generator<string> {
const files = fs.readdirSync(dir, { withFileTypes: true });
const files = fs.readdirSync(dir, { withFileTypes: true })
for (const file of files) {
if (file.isDirectory()) {
yield* walkSync(path.join(dir, file.name));
yield* walkSync(path.join(dir, file.name))
} else {
yield path.join(dir, file.name);
yield path.join(dir, file.name)
}
}
}
export interface APIConfig {
mockExtensions?: string[];
mockNodeDefs?: Record<string, any>;
settings?: Record<string, string>;
mockExtensions?: string[]
mockNodeDefs?: Record<string, any>
settings?: Record<string, string>
userConfig?: {
storage: "server" | "browser";
users?: Record<string, any>;
migrated?: boolean;
};
userData?: Record<string, any>;
storage: 'server' | 'browser'
users?: Record<string, any>
migrated?: boolean
}
userData?: Record<string, any>
}
/**
@@ -42,20 +42,20 @@ export function mockApi(config: APIConfig = {}) {
let { mockExtensions, mockNodeDefs, userConfig, settings, userData } = {
settings: {},
userData: {},
...config,
};
...config
}
if (!mockExtensions) {
mockExtensions = Array.from(walkSync(path.resolve("./src/extensions/core")))
.filter((x) => x.endsWith(".js"))
.map((x) => path.relative(path.resolve("./src/"), x).replace(/\\/g, "/"));
mockExtensions = Array.from(walkSync(path.resolve('./src/extensions/core')))
.filter((x) => x.endsWith('.js'))
.map((x) => path.relative(path.resolve('./src/'), x).replace(/\\/g, '/'))
}
if (!mockNodeDefs) {
mockNodeDefs = JSON.parse(
fs.readFileSync(path.resolve("./tests-ui/data/object_info.json"))
);
fs.readFileSync(path.resolve('./tests-ui/data/object_info.json'))
)
}
const events = new EventTarget();
const events = new EventTarget()
const mockApi = {
addEventListener: events.addEventListener.bind(events),
removeEventListener: events.removeEventListener.bind(events),
@@ -64,37 +64,37 @@ export function mockApi(config: APIConfig = {}) {
getExtensions: jest.fn(() => mockExtensions),
getNodeDefs: jest.fn(() => mockNodeDefs),
init: jest.fn(),
apiURL: jest.fn((x) => "src/" + x),
fileURL: jest.fn((x) => "src/" + x),
apiURL: jest.fn((x) => 'src/' + x),
fileURL: jest.fn((x) => 'src/' + x),
createUser: jest.fn((username) => {
// @ts-ignore
if (username in userConfig.users) {
return { status: 400, json: () => "Duplicate" };
return { status: 400, json: () => 'Duplicate' }
}
// @ts-ignore
userConfig.users[username + "!"] = username;
return { status: 200, json: () => username + "!" };
userConfig.users[username + '!'] = username
return { status: 200, json: () => username + '!' }
}),
getUserConfig: jest.fn(
() => userConfig ?? { storage: "browser", migrated: false }
() => userConfig ?? { storage: 'browser', migrated: false }
),
getSettings: jest.fn(() => settings),
storeSettings: jest.fn((v) => Object.assign(settings, v)),
getUserData: jest.fn((f) => {
if (f in userData) {
return { status: 200, json: () => userData[f] };
return { status: 200, json: () => userData[f] }
} else {
return { status: 404 };
return { status: 404 }
}
}),
storeUserData: jest.fn((file, data) => {
userData[file] = data;
userData[file] = data
}),
listUserData: jest.fn(() => []),
};
jest.mock("../../src/scripts/api", () => ({
listUserData: jest.fn(() => [])
}
jest.mock('../../src/scripts/api', () => ({
get api() {
return mockApi;
},
}));
return mockApi
}
}))
}