@blankgrimoire/testing 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/nativeBridge.d.ts +2 -0
- package/dist/nativeBridge.js +6 -0
- package/package.json +5 -4
- package/dist/node.d 2.d.ts +0 -39
- package/dist/node.d 2.js +0 -2
- package/dist/vmHarness 2.d.ts +0 -34
- package/dist/vmHarness 2.js +0 -128
package/dist/nativeBridge.d.ts
CHANGED
|
@@ -21,6 +21,8 @@ export declare class NativeBridge {
|
|
|
21
21
|
request(options: NativeRequestOptions): Promise<NativeResponse>;
|
|
22
22
|
parseHTML(html: string): NativeParseResult;
|
|
23
23
|
log(...args: unknown[]): void;
|
|
24
|
+
warn(...args: unknown[]): void;
|
|
25
|
+
error(...args: unknown[]): void;
|
|
24
26
|
setTimeout(cb: () => void, ms: number): number;
|
|
25
27
|
clearTimeout(id: number): void;
|
|
26
28
|
}
|
package/dist/nativeBridge.js
CHANGED
|
@@ -69,6 +69,12 @@ export class NativeBridge {
|
|
|
69
69
|
log(...args) {
|
|
70
70
|
console.log(...args);
|
|
71
71
|
}
|
|
72
|
+
warn(...args) {
|
|
73
|
+
console.warn(...args);
|
|
74
|
+
}
|
|
75
|
+
error(...args) {
|
|
76
|
+
console.error(...args);
|
|
77
|
+
}
|
|
72
78
|
setTimeout(cb, ms) {
|
|
73
79
|
const id = ++this.timerIdCounter;
|
|
74
80
|
const nativeId = setTimeout(() => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blankgrimoire/testing",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Node testing harness and mock server for BlankGrimoire theme adapters.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
"dist"
|
|
17
17
|
],
|
|
18
18
|
"publishConfig": {
|
|
19
|
-
"access": "public"
|
|
19
|
+
"access": "public",
|
|
20
|
+
"provenance": true
|
|
20
21
|
},
|
|
21
22
|
"repository": {
|
|
22
23
|
"type": "git",
|
|
@@ -25,8 +26,8 @@
|
|
|
25
26
|
},
|
|
26
27
|
"dependencies": {
|
|
27
28
|
"jsdom": "^24.0.0",
|
|
28
|
-
"@blankgrimoire/runtime": "1.
|
|
29
|
-
"@blankgrimoire/types": "1.
|
|
29
|
+
"@blankgrimoire/runtime": "1.1.0",
|
|
30
|
+
"@blankgrimoire/types": "1.1.0"
|
|
30
31
|
},
|
|
31
32
|
"scripts": {
|
|
32
33
|
"build": "tsc -p tsconfig.json",
|
package/dist/node.d 2.d.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
declare module 'http' {
|
|
2
|
-
interface IncomingMessage {
|
|
3
|
-
url?: string;
|
|
4
|
-
method?: string;
|
|
5
|
-
headers: Record<string, string>;
|
|
6
|
-
on(event: string, listener: (...args: unknown[]) => void): this;
|
|
7
|
-
}
|
|
8
|
-
interface ServerResponse {
|
|
9
|
-
writeHead(statusCode: number, headers?: Record<string, string>): void;
|
|
10
|
-
end(data?: string): void;
|
|
11
|
-
}
|
|
12
|
-
class Server {
|
|
13
|
-
listen(port: number, host: string, callback?: () => void): void;
|
|
14
|
-
close(callback?: (err: Error | null) => void): void;
|
|
15
|
-
address(): {
|
|
16
|
-
port: number;
|
|
17
|
-
};
|
|
18
|
-
on(event: string, listener: (...args: unknown[]) => void): this;
|
|
19
|
-
}
|
|
20
|
-
function createServer(requestListener?: (req: IncomingMessage, res: ServerResponse) => void): Server;
|
|
21
|
-
}
|
|
22
|
-
declare module 'node:fs' {
|
|
23
|
-
function readFileSync(path: string | URL, encoding: string): string;
|
|
24
|
-
}
|
|
25
|
-
declare module 'node:vm' {
|
|
26
|
-
interface Context {
|
|
27
|
-
[name: string]: unknown;
|
|
28
|
-
}
|
|
29
|
-
function createContext(contextObject: object): Context;
|
|
30
|
-
function runInContext(code: string, contextObject: object, options?: {
|
|
31
|
-
filename?: string;
|
|
32
|
-
timeout?: number;
|
|
33
|
-
}): unknown;
|
|
34
|
-
}
|
|
35
|
-
declare const Buffer: {
|
|
36
|
-
from(value: string, encoding?: string): {
|
|
37
|
-
toString(encoding: string): string;
|
|
38
|
-
};
|
|
39
|
-
};
|
package/dist/node.d 2.js
DELETED
package/dist/vmHarness 2.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { type Chapter, type MangaDetails, type MangaSummary, type Page, type PagedResult, type SearchQuery, type ThemeAdapter, type ThemeSelectors, type ThemeSignature } from '@blankgrimoire/types';
|
|
2
|
-
import { NativeBridge } from './nativeBridge.js';
|
|
3
|
-
export interface ThemeTestOptions {
|
|
4
|
-
mangaKey?: string;
|
|
5
|
-
chapterKey?: string;
|
|
6
|
-
searchQuery?: SearchQuery;
|
|
7
|
-
}
|
|
8
|
-
export interface ThemeTestResult {
|
|
9
|
-
detected: boolean;
|
|
10
|
-
score: number;
|
|
11
|
-
detectionSignature: ThemeSignature;
|
|
12
|
-
selectors: ThemeSelectors;
|
|
13
|
-
search?: PagedResult<MangaSummary>;
|
|
14
|
-
details?: MangaDetails;
|
|
15
|
-
chapters?: Chapter[];
|
|
16
|
-
pages?: Page[];
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Loads the real `dist/runtime.js` IIFE into an isolated `node:vm` context,
|
|
20
|
-
* together with an adapter and a native bridge, and exercises the same
|
|
21
|
-
* declarative engine used on iOS.
|
|
22
|
-
*/
|
|
23
|
-
export declare class VMHarness {
|
|
24
|
-
private readonly bridge;
|
|
25
|
-
private readonly runtime;
|
|
26
|
-
private readonly adapter;
|
|
27
|
-
constructor(allowedHosts: string[], runtimeIIFE: string, adapterModule: {
|
|
28
|
-
default: ThemeAdapter;
|
|
29
|
-
} | ThemeAdapter);
|
|
30
|
-
private score;
|
|
31
|
-
testTheme(html: string, url?: string, options?: ThemeTestOptions): Promise<ThemeTestResult>;
|
|
32
|
-
/** Exposes the bridge for tests that simulate network responses. */
|
|
33
|
-
getNativeBridge(): NativeBridge;
|
|
34
|
-
}
|
package/dist/vmHarness 2.js
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
// @ts-ignore - jsdom has no bundled type declarations
|
|
3
|
-
import { JSDOM } from 'jsdom';
|
|
4
|
-
import * as nodeVm from 'node:vm';
|
|
5
|
-
import { API_VERSION, } from '@blankgrimoire/types';
|
|
6
|
-
import { NativeBridge } from './nativeBridge.js';
|
|
7
|
-
/**
|
|
8
|
-
* Loads the real `dist/runtime.js` IIFE into an isolated `node:vm` context,
|
|
9
|
-
* together with an adapter and a native bridge, and exercises the same
|
|
10
|
-
* declarative engine used on iOS.
|
|
11
|
-
*/
|
|
12
|
-
export class VMHarness {
|
|
13
|
-
constructor(allowedHosts, runtimeIIFE, adapterModule) {
|
|
14
|
-
this.bridge = new NativeBridge(allowedHosts);
|
|
15
|
-
this.adapter = 'default' in adapterModule ? adapterModule.default : adapterModule;
|
|
16
|
-
const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>');
|
|
17
|
-
const window = dom.window;
|
|
18
|
-
const document = dom.window.document;
|
|
19
|
-
const native = {
|
|
20
|
-
request: (request) => this.bridge.request(request),
|
|
21
|
-
parseHTML: (html) => this.bridge.parseHTML(html),
|
|
22
|
-
log: (...args) => this.bridge.log(...args),
|
|
23
|
-
error: (...args) => this.bridge.log(...args),
|
|
24
|
-
warn: (...args) => this.bridge.log(...args),
|
|
25
|
-
setTimeout: (callback, ms) => this.bridge.setTimeout(callback, ms),
|
|
26
|
-
clearTimeout: (id) => this.bridge.clearTimeout(id),
|
|
27
|
-
};
|
|
28
|
-
const sandbox = {
|
|
29
|
-
window,
|
|
30
|
-
document,
|
|
31
|
-
__native: native,
|
|
32
|
-
console,
|
|
33
|
-
URL,
|
|
34
|
-
atob: (value) => Buffer.from(value, 'base64').toString('binary'),
|
|
35
|
-
btoa: (value) => Buffer.from(value, 'binary').toString('base64'),
|
|
36
|
-
setTimeout: (callback, ms) => this.bridge.setTimeout(callback, ms),
|
|
37
|
-
clearTimeout: (id) => this.bridge.clearTimeout(id),
|
|
38
|
-
};
|
|
39
|
-
const context = nodeVm.createContext(sandbox);
|
|
40
|
-
nodeVm.runInContext(runtimeIIFE, context, { filename: 'runtime.js' });
|
|
41
|
-
const runtime = sandbox.BlankGrimoireRuntime;
|
|
42
|
-
if (!runtime || runtime.__apiVersion !== API_VERSION) {
|
|
43
|
-
throw new Error(`runtimeIIFE did not expose a BlankGrimoireRuntime global for API version ${API_VERSION}`);
|
|
44
|
-
}
|
|
45
|
-
if (typeof runtime.dispatch !== 'function') {
|
|
46
|
-
throw new Error('runtimeIIFE did not expose a BlankGrimoireRuntime global with dispatch');
|
|
47
|
-
}
|
|
48
|
-
this.runtime = runtime;
|
|
49
|
-
}
|
|
50
|
-
score(signature, document, url) {
|
|
51
|
-
let total = 0;
|
|
52
|
-
if (signature.metaGenerator) {
|
|
53
|
-
const meta = document.querySelector('meta[name="generator"]');
|
|
54
|
-
const content = meta?.getAttribute('content') ?? '';
|
|
55
|
-
if (new RegExp(signature.metaGenerator).test(content)) {
|
|
56
|
-
total += 3;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
for (const selector of signature.selectors ?? []) {
|
|
60
|
-
if (document.querySelector(selector)) {
|
|
61
|
-
total += 2;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
for (const pattern of signature.scriptSrc ?? []) {
|
|
65
|
-
const regex = new RegExp(pattern);
|
|
66
|
-
const scripts = Array.from(document.querySelectorAll('script'));
|
|
67
|
-
if (scripts.some((script) => {
|
|
68
|
-
const src = script.getAttribute('src') ?? '';
|
|
69
|
-
return regex.test(src);
|
|
70
|
-
})) {
|
|
71
|
-
total += 1;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return (async () => {
|
|
75
|
-
for (const probePath of signature.probePaths ?? []) {
|
|
76
|
-
try {
|
|
77
|
-
const response = await this.bridge.request({
|
|
78
|
-
url: new URL(probePath, url).toString(),
|
|
79
|
-
method: 'GET',
|
|
80
|
-
});
|
|
81
|
-
if (response.status >= 200 && response.status < 300) {
|
|
82
|
-
total += 3;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
catch {
|
|
86
|
-
// A failed probe simply contributes no score.
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return total;
|
|
90
|
-
})();
|
|
91
|
-
}
|
|
92
|
-
async testTheme(html, url = 'https://example.org/', options = {}) {
|
|
93
|
-
const dom = new JSDOM(html, { url });
|
|
94
|
-
const document = dom.window.document;
|
|
95
|
-
const window = dom.window;
|
|
96
|
-
const detectionSignature = this.adapter.detectionSignature;
|
|
97
|
-
const score = await this.score(detectionSignature, document, url);
|
|
98
|
-
const base = {
|
|
99
|
-
detected: score >= detectionSignature.minScore,
|
|
100
|
-
score,
|
|
101
|
-
detectionSignature,
|
|
102
|
-
selectors: this.adapter.selectors,
|
|
103
|
-
};
|
|
104
|
-
if (!base.detected) {
|
|
105
|
-
return base;
|
|
106
|
-
}
|
|
107
|
-
const runtime = {
|
|
108
|
-
adapter: this.adapter,
|
|
109
|
-
context: { baseURL: url, siteName: 'Example Site', overrides: {} },
|
|
110
|
-
document,
|
|
111
|
-
window,
|
|
112
|
-
parseHTML: (value) => this.bridge.parseHTML(value),
|
|
113
|
-
};
|
|
114
|
-
const mangaKey = options.mangaKey ?? 'lorem-ipsum';
|
|
115
|
-
const searchQuery = options.searchQuery ?? { page: 1 };
|
|
116
|
-
const call = async (method, args) => JSON.parse(await this.runtime.dispatch(runtime, method, JSON.stringify(args)));
|
|
117
|
-
base.search = await call('search', searchQuery);
|
|
118
|
-
base.details = await call('details', { key: mangaKey });
|
|
119
|
-
base.chapters = await call('chapters', { key: mangaKey });
|
|
120
|
-
const chapterKey = options.chapterKey ?? base.chapters[0]?.key ?? 'chapter-1';
|
|
121
|
-
base.pages = await call('pages', { mangaKey, chapterKey });
|
|
122
|
-
return base;
|
|
123
|
-
}
|
|
124
|
-
/** Exposes the bridge for tests that simulate network responses. */
|
|
125
|
-
getNativeBridge() {
|
|
126
|
-
return this.bridge;
|
|
127
|
-
}
|
|
128
|
-
}
|