Skip to content

Commit a9d82d7

Browse files
committed
refactor: some renames and tweaks
1 parent 28036ff commit a9d82d7

9 files changed

Lines changed: 91 additions & 94 deletions

File tree

packages/scaffold/src/core/tester/reporter.ts renamed to packages/scaffold/src/core/tester/http-reporter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ interface ResultTestFail {
4343

4444
type Result = ResultS | ResultSuite | ResultTestPass | ResultTestFail;
4545

46-
export class HttpReporter {
46+
export class TestHttpReporter {
4747
private _server?: http.Server;
4848
private _port?: number;
4949

packages/scaffold/src/core/tester/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import { ZoteroRunner } from "../../utils/zotero-runner.js";
1111
import { Base } from "../base.js";
1212
import Build from "../builder.js";
1313
import { prepareHeadless } from "./headless.js";
14-
import { HttpReporter } from "./reporter.js";
15-
import { TestRunnerPlugin } from "./test-runner-plugin.js";
14+
import { TestHttpReporter } from "./http-reporter.js";
15+
import { TestBundler } from "./test-bundler.js";
1616

1717
export default class Test extends Base {
1818
private builder: Build;
1919
private zotero?: ZoteroRunner;
20-
private reporter: HttpReporter = new HttpReporter();
21-
private testPluginBuilder?: TestRunnerPlugin;
20+
private reporter: TestHttpReporter = new TestHttpReporter();
21+
private testBundler?: TestBundler;
2222

2323
constructor(ctx: Context) {
2424
super(ctx);
@@ -54,11 +54,11 @@ export default class Test extends Base {
5454
await this.ctx.hooks.callHook("test:listen", this.ctx);
5555

5656
// Create proxy plugin to run tests
57-
this.testPluginBuilder = new TestRunnerPlugin(
57+
this.testBundler = new TestBundler(
5858
this.ctx,
5959
this.reporter.port,
6060
);
61-
await this.testPluginBuilder.generate();
61+
await this.testBundler.generate();
6262
await this.ctx.hooks.callHook("test:copyAssets", this.ctx);
6363

6464
// Start Zotero
@@ -87,11 +87,11 @@ export default class Test extends Base {
8787
onChange: async (path) => {
8888
if (isSource(path)) {
8989
await this.builder.run();
90-
await this.testPluginBuilder?.regenerate(path);
90+
await this.testBundler?.regenerate(path);
9191
await this.zotero?.reloadAllPlugins();
9292
}
9393
else {
94-
await this.testPluginBuilder?.regenerate(path);
94+
await this.testBundler?.regenerate(path);
9595
await this.zotero?.reloadTemporaryPluginBySourceDir(TESTER_PLUGIN_DIR);
9696
}
9797
},

packages/scaffold/src/core/tester/create-proxy-plugin/bootsrtap.ts renamed to packages/scaffold/src/core/tester/test-bundler-template/bootsrtap.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TESTER_PLUGIN_ID, TESTER_PLUGIN_REF } from "../../../constant.js";
1+
import { TESTER_PLUGIN_REF } from "../../../constant.js";
22

33
export function generateBootstrap(options: {
44
port: number;
@@ -100,20 +100,3 @@ function waitUtilAsync(condition, interval = 100, timeout = 1e4) {
100100
}
101101
`;
102102
}
103-
104-
export function generateManifest() {
105-
return {
106-
manifest_version: 2,
107-
name: "Zotero Plugin Scaffold Test Runner",
108-
version: "0.0.1",
109-
description: "Test suite for the Zotero plugin. This is a runtime-generated plugin only for testing purposes.",
110-
applications: {
111-
zotero: {
112-
id: TESTER_PLUGIN_ID,
113-
update_url: "https://example.com",
114-
// strict_min_version: "*.*.*",
115-
strict_max_version: "999.*.*",
116-
},
117-
},
118-
};
119-
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
export function generateHtml(
2+
setupCode: string,
3+
testFiles: string[],
4+
) {
5+
const tests = testFiles.map(f => `<script src="${f}"></script>`).join("\n ");
6+
7+
return `<!-- Generated by zotero-plugin-scaffold -->
8+
<!DOCTYPE html>
9+
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
10+
<head>
11+
<meta charset="UTF-8"></meta>
12+
<meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
13+
<title>Zotero Plugin Test</title>
14+
<style>
15+
html {
16+
min-width: 400px;
17+
min-height: 600px;
18+
}
19+
body {
20+
font-family: Arial, sans-serif;
21+
}
22+
</style>
23+
</head>
24+
<body>
25+
<div id="mocha"></div>
26+
27+
<!-- Include Zotero Vars -->
28+
<script src="chrome://zotero/content/include.js"></script>
29+
30+
<!-- Mocha and Chai Libraries -->
31+
<script src="mocha.js"></script>
32+
<script src="chai.js"></script>
33+
34+
<!-- Setup Mocha -->
35+
<script>
36+
${setupCode}
37+
</script>
38+
39+
<!-- Unit tests -->
40+
${tests}
41+
42+
<!-- Run Mocha -->
43+
<script class="mocha-exec">
44+
mocha.run();
45+
</script>
46+
</body>
47+
</html>
48+
`;
49+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from "./bootsrtap.js";
2+
export * from "./html.js";
3+
export * from "./manifest.js";
4+
export * from "./mocha-setup.js";
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { TESTER_PLUGIN_ID } from "../../../constant.js";
2+
3+
export function generateManifest() {
4+
return {
5+
manifest_version: 2,
6+
name: "Zotero Plugin Scaffold Test Runner",
7+
version: "0.0.1",
8+
description: "Test suite for the Zotero plugin. This is a runtime-generated plugin only for testing purposes.",
9+
applications: {
10+
zotero: {
11+
id: TESTER_PLUGIN_ID,
12+
update_url: "https://example.com",
13+
// strict_min_version: "*.*.*",
14+
strict_max_version: "999.*.*",
15+
},
16+
},
17+
};
18+
}

packages/scaffold/src/core/tester/create-proxy-plugin/mocha-setup.ts renamed to packages/scaffold/src/core/tester/test-bundler-template/mocha-setup.ts

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -150,53 +150,3 @@ function Reporter(runner) {
150150
}
151151
`;
152152
}
153-
154-
export function generateHtml(
155-
setupCode: string,
156-
testFiles: string[],
157-
) {
158-
const tests = testFiles.map(f => `<script src="${f}"></script>`).join("\n ");
159-
160-
return `<!-- Generated by zotero-plugin-scaffold -->
161-
<!DOCTYPE html>
162-
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
163-
<head>
164-
<meta charset="UTF-8"></meta>
165-
<meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
166-
<title>Zotero Plugin Test</title>
167-
<style>
168-
html {
169-
min-width: 400px;
170-
min-height: 600px;
171-
}
172-
body {
173-
font-family: Arial, sans-serif;
174-
}
175-
</style>
176-
</head>
177-
<body>
178-
<div id="mocha"></div>
179-
180-
<!-- Include Zotero Vars -->
181-
<script src="chrome://zotero/content/include.js"></script>
182-
183-
<!-- Mocha and Chai Libraries -->
184-
<script src="mocha.js"></script>
185-
<script src="chai.js"></script>
186-
187-
<!-- Setup Mocha -->
188-
<script>
189-
${setupCode}
190-
</script>
191-
192-
<!-- Unit tests -->
193-
${tests}
194-
195-
<!-- Run Mocha -->
196-
<script class="mocha-exec">
197-
mocha.run();
198-
</script>
199-
</body>
200-
</html>
201-
`;
202-
}

packages/scaffold/src/core/tester/test-runner-plugin.ts renamed to packages/scaffold/src/core/tester/test-bundler.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { BuildContext, BuildResult } from "esbuild";
2-
import type { Context } from "../..//types/index.js";
2+
import type { Context } from "../../types/index.js";
33
import { context } from "esbuild";
44
import { copy, outputFile, outputJSON, pathExists } from "fs-extra/esm";
55
import { resolve } from "pathe";
@@ -8,10 +8,9 @@ import { CACHE_DIR, TESTER_PLUGIN_DIR } from "../../constant.js";
88
import { saveResource } from "../../utils/file.js";
99
import { logger } from "../../utils/logger.js";
1010
import { toArray } from "../../utils/string.js";
11-
import { generateBootstrap, generateManifest } from "./create-proxy-plugin/bootsrtap.js";
12-
import { generateHtml, generateMochaSetup } from "./create-proxy-plugin/mocha-setup.js";
11+
import { generateBootstrap, generateHtml, generateManifest, generateMochaSetup } from "./test-bundler-template/index.js";
1312

14-
export class TestRunnerPlugin {
13+
export class TestBundler {
1514
private esbuildContext?: BuildContext;
1615
constructor(
1716
private ctx: Context,
@@ -26,12 +25,12 @@ export class TestRunnerPlugin {
2625
// manifest
2726
// copy lib
2827
// bundle tests
29-
await this.generatePluginRes();
28+
await this.generateTestResources();
3029

3130
// this.generateTestPage
3231
// mocha setup
3332
// html
34-
await this.generateTestPage();
33+
await this.createTestHtml();
3534
}
3635

3736
async regenerate(changedFile: string) {
@@ -44,10 +43,10 @@ export class TestRunnerPlugin {
4443
// this.generateTestPage
4544
// mocha setup
4645
// html
47-
await this.generateTestPage(tests);
46+
await this.createTestHtml(tests);
4847
}
4948

50-
private async generatePluginRes() {
49+
private async generateTestResources() {
5150
// bootstrape
5251
const manifest = generateManifest();
5352
await outputJSON(`${TESTER_PLUGIN_DIR}/manifest.json`, manifest, { spaces: 2 });
@@ -128,7 +127,7 @@ export class TestRunnerPlugin {
128127
await this.esbuildContext.rebuild();
129128
}
130129

131-
private async generateTestPage(tests: string[] = []) {
130+
private async createTestHtml(tests: string[] = []) {
132131
// mocha setup
133132
const setupCode = generateMochaSetup({
134133
timeout: this.ctx.test.mocha.timeout,

packages/scaffold/test/unit/tester.test.ts renamed to packages/scaffold/test/unit/test-bundler.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import type { Metafile } from "esbuild";
2-
import { describe, expect, it, vi } from "vitest";
3-
import { findImpactedTests } from "../../src/core/tester/test-runner-plugin.js";
4-
5-
// Mock resolve function
6-
vi.mock("path", async () => {
7-
const actual = await vi.importActual<typeof import("path")>("path");
8-
return { ...actual, resolve: (p: string) => p };
9-
});
2+
import { describe, expect, it } from "vitest";
3+
import { findImpactedTests } from "../../src/core/tester/test-bundler.js";
104

115
describe("findImpactedTests", () => {
126
const mockMetafileOutputs = {

0 commit comments

Comments
 (0)