From 15ca5160cab0b31ed5db072025030c04787a9d59 Mon Sep 17 00:00:00 2001 From: Krasilnikov Roman Date: Tue, 25 Jun 2024 23:31:45 +0300 Subject: [PATCH 1/9] WIP: Universal workers --- apps/ppp/package.json | 2 +- .../content/design-patterns/factory/code.js | 15 +++++++ .../design-patterns/factory/editor.astro | 33 ++++---------- .../design-patterns/factory/js-factory.ts | 27 +++++++++++ .../design-patterns/factory/reference.ts | 36 +++++++++++++++ .../design-patterns/factory/tests-data.ts | 31 +++++++++++++ apps/ppp/src/lib/workers/js.ts | 16 +++++++ packages/testing/src/actor.ts | 45 +++++++++++++++---- pnpm-lock.yaml | 6 +-- 9 files changed, 174 insertions(+), 37 deletions(-) create mode 100644 apps/ppp/src/content/design-patterns/factory/code.js create mode 100644 apps/ppp/src/content/design-patterns/factory/js-factory.ts create mode 100644 apps/ppp/src/content/design-patterns/factory/reference.ts create mode 100644 apps/ppp/src/content/design-patterns/factory/tests-data.ts create mode 100644 apps/ppp/src/lib/workers/js.ts diff --git a/apps/ppp/package.json b/apps/ppp/package.json index 949110f..f06a141 100644 --- a/apps/ppp/package.json +++ b/apps/ppp/package.json @@ -24,7 +24,7 @@ "tailwindcss": "^3.4.4", "libs": "workspace:*", "testing": "workspace:*", - "design-patterns-factory": "workspace:*" + "testing-javascript": "workspace:*" }, "devDependencies": { "@iconify-json/lucide": "^1.1.191", diff --git a/apps/ppp/src/content/design-patterns/factory/code.js b/apps/ppp/src/content/design-patterns/factory/code.js new file mode 100644 index 0000000..154770c --- /dev/null +++ b/apps/ppp/src/content/design-patterns/factory/code.js @@ -0,0 +1,15 @@ +const PAYMENT_SYSTEM_TYPE = { + PAYPAL: "paypal", + WEBMONEY: "webmoney", + CAT_BANK: "cat-bank", +}; + + + + + + + +export function payment(type, base, amount) { + throw new Error("Not implemented"); +} diff --git a/apps/ppp/src/content/design-patterns/factory/editor.astro b/apps/ppp/src/content/design-patterns/factory/editor.astro index b7e3742..e8f5232 100644 --- a/apps/ppp/src/content/design-patterns/factory/editor.astro +++ b/apps/ppp/src/content/design-patterns/factory/editor.astro @@ -13,34 +13,19 @@ const { contentId } = Astro.props; import { Language } from "@/lib/languages"; import { mountEditor } from "@/adapters/svelte-editor"; - - import { testsData } from "design-patterns-factory"; - import { code as jsCode, Worker as JsWorker } from "design-patterns-factory/js"; - import { code as tsCode, Worker as TsWorker } from "design-patterns-factory/ts"; - import { code as phpCode, Worker as PhpWorker } from "design-patterns-factory/php"; - import { code as pyCode, Worker as PyWorker } from "design-patterns-factory/python"; - import { code as goCode, Worker as GoWorker } from 'design-patterns-factory/go'; + + import { testsData } from "./tests-data"; + + import JsWorker from '@/lib/workers/js?worker'; + import jsCode from './code.js?raw'; + import { universalFactory } from './js-factory' mountEditor(testsData, { - [Language.PHP]: { - initialValue: phpCode, - testRunnerFactory: makeRemoteTestRunnerFactory(PhpWorker), - }, - [Language.TypeScript]: { - initialValue: tsCode, - testRunnerFactory: makeRemoteTestRunnerFactory(TsWorker), - }, + [Language.JavaScript]: { initialValue: jsCode, - testRunnerFactory: makeRemoteTestRunnerFactory(JsWorker), - }, - [Language.Python]: { - initialValue: pyCode, - testRunnerFactory: makeRemoteTestRunnerFactory(PyWorker), + testRunnerFactory: makeRemoteTestRunnerFactory(JsWorker, universalFactory), }, - [Language.Go]: { - initialValue: goCode, - testRunnerFactory: makeRemoteTestRunnerFactory(GoWorker), - } + }); diff --git a/apps/ppp/src/content/design-patterns/factory/js-factory.ts b/apps/ppp/src/content/design-patterns/factory/js-factory.ts new file mode 100644 index 0000000..7fa16b3 --- /dev/null +++ b/apps/ppp/src/content/design-patterns/factory/js-factory.ts @@ -0,0 +1,27 @@ +// Only type imports are allowed +import type { UniversalFactory } from "testing/actor"; + +import type { UniversalFactoryData } from "@/lib/workers/js"; + +import type { Input, Output } from "./tests-data"; +import type { PaymentSystemType } from "./reference"; + +interface TestingModule { + payment(type: PaymentSystemType, base: number, amount: number): number; +} + +export const universalFactory: UniversalFactory< + Input, + Output, + UniversalFactoryData +> = ({ createLogger, JsTestRunner }) => { + class TestRunner extends JsTestRunner { + override async executeTest( + m: TestingModule, + input: Input + ): Promise { + return m.payment(input.paymentSystem, input.base, input.amount); + } + } + return async (_, { code, out }) => new TestRunner(createLogger(out), code); +}; diff --git a/apps/ppp/src/content/design-patterns/factory/reference.ts b/apps/ppp/src/content/design-patterns/factory/reference.ts new file mode 100644 index 0000000..e90e519 --- /dev/null +++ b/apps/ppp/src/content/design-patterns/factory/reference.ts @@ -0,0 +1,36 @@ +export const enum PaymentSystemType { + PayPal = "paypal", + WebMoney = "webmoney", + CatBank = "cat-bank", +} + +interface PaymentSystem { + payment(amount: number): number; + payout(amount: number): number; +} + +type PaymentSystemFactory = (base: number) => PaymentSystem; + +const paymentSystemFactories: Record = + { + [PaymentSystemType.PayPal]: (base) => ({ + payment: (amount: number) => base + amount, + payout: (amount: number) => base - amount, + }), + [PaymentSystemType.WebMoney]: (base) => ({ + payment: (amount: number) => base + 2 * amount, + payout: (amount: number) => base - 2 * amount, + }), + [PaymentSystemType.CatBank]: (base) => ({ + payment: (amount: number) => base * amount, + payout: (amount: number) => base / amount, + }), + }; + +export function payment( + type: PaymentSystemType, + base: number, + amount: number +): number { + return paymentSystemFactories[type](base).payment(amount); +} diff --git a/apps/ppp/src/content/design-patterns/factory/tests-data.ts b/apps/ppp/src/content/design-patterns/factory/tests-data.ts new file mode 100644 index 0000000..6a504da --- /dev/null +++ b/apps/ppp/src/content/design-patterns/factory/tests-data.ts @@ -0,0 +1,31 @@ +import { testData } from "testing"; + +import { PaymentSystemType, payment } from "./reference"; + +export interface Input { + paymentSystem: PaymentSystemType; + base: number; + amount: number; +} + +export type Output = number; + +export const testsData = [ + { + paymentSystem: PaymentSystemType.PayPal, + base: 1, + amount: 1, + }, + { + paymentSystem: PaymentSystemType.WebMoney, + base: 1, + amount: 1, + }, + { + paymentSystem: PaymentSystemType.CatBank, + base: 1, + amount: 1, + }, +].map( + testData((input) => payment(input.paymentSystem, input.base, input.amount)) +); diff --git a/apps/ppp/src/lib/workers/js.ts b/apps/ppp/src/lib/workers/js.ts new file mode 100644 index 0000000..c2e7341 --- /dev/null +++ b/apps/ppp/src/lib/workers/js.ts @@ -0,0 +1,16 @@ +import { createLogger } from "libs/logger"; +import { startTestRunnerActor } from "testing/actor"; +import { JsTestRunner } from "testing-javascript"; + +export interface UniversalFactoryData { + JsTestRunner: typeof JsTestRunner; + createLogger: typeof createLogger; +} + +startTestRunnerActor( + (universalFactory) => + universalFactory({ + JsTestRunner, + createLogger, + }) +); diff --git a/packages/testing/src/actor.ts b/packages/testing/src/actor.ts index ccce7ce..51316df 100644 --- a/packages/testing/src/actor.ts +++ b/packages/testing/src/actor.ts @@ -14,9 +14,14 @@ import { stringifyError } from "libs/error"; import type { TestRunner, TestRunnerFactory } from "./model.js"; +export interface WorkerInitConfig { + code: string; + universalFactoryFunction: string; +} + interface Handlers { [key: string]: any; - init(code: string): Promise; + init(config: WorkerInitConfig): Promise; cancel(): void; run(data: I): Promise; } @@ -32,16 +37,29 @@ type Outgoing = | OutgoingMessage, string> | TestingActorEvent; -class TestRunnerActor extends Actor, string> { +async function evalEntity(functionStr: string) { + const moduleStr = `export default ${functionStr}`; + const moduleUrl = `data:text/javascript;base64,${btoa(moduleStr)}`; + const mod = await import(/* @vite-ignore */ moduleUrl); + return mod.default as T; +} + +export type UniversalFactory = (data: D) => TestRunnerFactory + +export type SuperFactory = (universalFactory: UniversalFactory) => TestRunnerFactory + +class TestRunnerActor extends Actor, string> { private ctx: Context = createContext(); private runner: TestRunner | null = null; constructor( connection: Connection, Outgoing>, - factory: TestRunnerFactory + superFactory: SuperFactory ) { const handlers: Handlers = { - init: async (code) => { + init: async ({ code, universalFactoryFunction }) => { + const universalFactory = await evalEntity>(universalFactoryFunction); + const factory = superFactory(universalFactory); this.runner = await factory(this.ctx, { code, out: { @@ -83,11 +101,13 @@ class TestRunnerActor extends Actor, string> { } } -export function startTestRunnerActor(factory: TestRunnerFactory) { +export function startTestRunnerActor( + superFactory: SuperFactory +) { const connection = new WorkerConnection, Outgoing>( self as unknown as Worker ); - const actor = new TestRunnerActor(connection, factory); + const actor = new TestRunnerActor(connection, superFactory); const stopConnection = connection.start(); const stopActor = actor.start(); return () => { @@ -100,8 +120,9 @@ interface WorkerConstructor { new (): Worker; } -export function makeRemoteTestRunnerFactory( - Worker: WorkerConstructor +export function makeRemoteTestRunnerFactory( + Worker: WorkerConstructor, + universalFactory: (data: D) => TestRunnerFactory ): TestRunnerFactory { return async (ctx, { code, out }) => { const worker = new Worker(); @@ -128,7 +149,13 @@ export function makeRemoteTestRunnerFactory( remote.cancel(); }); try { - await inContext(ctx, remote.init(code)); + await inContext( + ctx, + remote.init({ + code, + universalFactoryFunction: universalFactory.toString(), + }) + ); } catch (err) { dispose(); throw err; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f16d038..6d15913 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -47,9 +47,6 @@ importers: astro-icon: specifier: ^1.1.0 version: 1.1.0 - design-patterns-factory: - specifier: workspace:* - version: link:../../content/design-patterns/factory libs: specifier: workspace:* version: link:../../packages/libs @@ -65,6 +62,9 @@ importers: testing: specifier: workspace:* version: link:../../packages/testing + testing-javascript: + specifier: workspace:* + version: link:../../packages/testing-javascript devDependencies: '@iconify-json/lucide': specifier: ^1.1.191 From ab0b0e218ae7978a1263a0fdc0e6e7d64e9392d7 Mon Sep 17 00:00:00 2001 From: Krasilnikov Roman Date: Wed, 26 Jun 2024 00:57:54 +0300 Subject: [PATCH 2/9] Increased default timeout --- apps/ppp/src/components/editor/model.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ppp/src/components/editor/model.ts b/apps/ppp/src/components/editor/model.ts index 2ad1917..7174838 100644 --- a/apps/ppp/src/components/editor/model.ts +++ b/apps/ppp/src/components/editor/model.ts @@ -18,5 +18,5 @@ export const vimState = reactive( ); export const testRunnerTimeout = reactive( - createSyncStorage(localStorage, "editor-test-runner-timeout", 8000) + createSyncStorage(localStorage, "editor-test-runner-timeout", 60000) ); From 104b36deadfbffc944e7ffeb7728d7fb58d92442 Mon Sep 17 00:00:00 2001 From: Krasilnikov Roman Date: Wed, 26 Jun 2024 01:04:09 +0300 Subject: [PATCH 3/9] Universal test workers --- apps/ppp/package.json | 6 +- apps/ppp/src/adapters/testing-actor.ts | 151 ------------------ .../design-patterns/factory/editor.astro | 34 ++-- .../design-patterns/factory}/go/code.go | 0 .../design-patterns/factory/go/factory.ts | 25 +++ .../design-patterns/factory/go/index.ts | 2 + .../design-patterns/factory/{ => js}/code.js | 0 .../factory/{js-factory.ts => js/factory.ts} | 6 +- .../design-patterns/factory/js/index.ts | 2 + .../design-patterns/factory}/php/code.php | 0 .../design-patterns/factory/php/factory.ts | 39 +++++ .../design-patterns/factory/php/index.ts | 2 + .../design-patterns/factory}/python/code.py | 0 .../design-patterns/factory/python/factory.ts | 26 +++ .../design-patterns/factory/python/index.ts | 2 + .../design-patterns/factory}/ts/code.ts | 0 .../design-patterns/factory/ts/factory.ts | 27 ++++ .../design-patterns/factory/ts/index.ts | 2 + apps/ppp/src/lib/workers/go.ts | 18 +++ apps/ppp/src/lib/workers/index.ts | 5 + apps/ppp/src/lib/workers/php.ts | 17 ++ apps/ppp/src/lib/workers/python.ts | 18 +++ apps/ppp/src/lib/workers/ts.ts | 16 ++ content/design-patterns/factory/package.json | 31 ---- .../design-patterns/factory/src/go/index.ts | 4 - .../design-patterns/factory/src/go/runner.ts | 20 --- .../design-patterns/factory/src/go/worker.ts | 5 - content/design-patterns/factory/src/index.ts | 2 - .../design-patterns/factory/src/js/code.js | 15 -- .../design-patterns/factory/src/js/index.ts | 4 - .../design-patterns/factory/src/js/runner.ts | 19 --- .../design-patterns/factory/src/js/worker.ts | 5 - .../design-patterns/factory/src/php/index.ts | 4 - .../design-patterns/factory/src/php/runner.ts | 33 ---- .../design-patterns/factory/src/php/worker.ts | 5 - .../factory/src/python/index.ts | 4 - .../factory/src/python/runner.ts | 20 --- .../factory/src/python/worker.ts | 5 - .../design-patterns/factory/src/reference.ts | 36 ----- .../design-patterns/factory/src/tests-data.ts | 31 ---- .../design-patterns/factory/src/ts/index.ts | 4 - .../design-patterns/factory/src/ts/runner.ts | 21 --- .../design-patterns/factory/src/ts/worker.ts | 5 - content/design-patterns/factory/tsconfig.json | 14 -- .../design-patterns/factory/vite.config.js | 46 ------ pnpm-lock.yaml | 43 ++--- pnpm-workspace.yaml | 1 - 47 files changed, 246 insertions(+), 529 deletions(-) delete mode 100644 apps/ppp/src/adapters/testing-actor.ts rename {content/design-patterns/factory/src => apps/ppp/src/content/design-patterns/factory}/go/code.go (100%) create mode 100644 apps/ppp/src/content/design-patterns/factory/go/factory.ts create mode 100644 apps/ppp/src/content/design-patterns/factory/go/index.ts rename apps/ppp/src/content/design-patterns/factory/{ => js}/code.js (100%) rename apps/ppp/src/content/design-patterns/factory/{js-factory.ts => js/factory.ts} (81%) create mode 100644 apps/ppp/src/content/design-patterns/factory/js/index.ts rename {content/design-patterns/factory/src => apps/ppp/src/content/design-patterns/factory}/php/code.php (100%) create mode 100644 apps/ppp/src/content/design-patterns/factory/php/factory.ts create mode 100644 apps/ppp/src/content/design-patterns/factory/php/index.ts rename {content/design-patterns/factory/src => apps/ppp/src/content/design-patterns/factory}/python/code.py (100%) create mode 100644 apps/ppp/src/content/design-patterns/factory/python/factory.ts create mode 100644 apps/ppp/src/content/design-patterns/factory/python/index.ts rename {content/design-patterns/factory/src => apps/ppp/src/content/design-patterns/factory}/ts/code.ts (100%) create mode 100644 apps/ppp/src/content/design-patterns/factory/ts/factory.ts create mode 100644 apps/ppp/src/content/design-patterns/factory/ts/index.ts create mode 100644 apps/ppp/src/lib/workers/go.ts create mode 100644 apps/ppp/src/lib/workers/index.ts create mode 100644 apps/ppp/src/lib/workers/php.ts create mode 100644 apps/ppp/src/lib/workers/python.ts create mode 100644 apps/ppp/src/lib/workers/ts.ts delete mode 100644 content/design-patterns/factory/package.json delete mode 100644 content/design-patterns/factory/src/go/index.ts delete mode 100644 content/design-patterns/factory/src/go/runner.ts delete mode 100644 content/design-patterns/factory/src/go/worker.ts delete mode 100644 content/design-patterns/factory/src/index.ts delete mode 100644 content/design-patterns/factory/src/js/code.js delete mode 100644 content/design-patterns/factory/src/js/index.ts delete mode 100644 content/design-patterns/factory/src/js/runner.ts delete mode 100644 content/design-patterns/factory/src/js/worker.ts delete mode 100644 content/design-patterns/factory/src/php/index.ts delete mode 100644 content/design-patterns/factory/src/php/runner.ts delete mode 100644 content/design-patterns/factory/src/php/worker.ts delete mode 100644 content/design-patterns/factory/src/python/index.ts delete mode 100644 content/design-patterns/factory/src/python/runner.ts delete mode 100644 content/design-patterns/factory/src/python/worker.ts delete mode 100644 content/design-patterns/factory/src/reference.ts delete mode 100644 content/design-patterns/factory/src/tests-data.ts delete mode 100644 content/design-patterns/factory/src/ts/index.ts delete mode 100644 content/design-patterns/factory/src/ts/runner.ts delete mode 100644 content/design-patterns/factory/src/ts/worker.ts delete mode 100644 content/design-patterns/factory/tsconfig.json delete mode 100644 content/design-patterns/factory/vite.config.js diff --git a/apps/ppp/package.json b/apps/ppp/package.json index f06a141..a5823fd 100644 --- a/apps/ppp/package.json +++ b/apps/ppp/package.json @@ -24,7 +24,11 @@ "tailwindcss": "^3.4.4", "libs": "workspace:*", "testing": "workspace:*", - "testing-javascript": "workspace:*" + "testing-javascript": "workspace:*", + "testing-typescript": "workspace:*", + "testing-php": "workspace:*", + "testing-python": "workspace:*", + "testing-go": "workspace:*" }, "devDependencies": { "@iconify-json/lucide": "^1.1.191", diff --git a/apps/ppp/src/adapters/testing-actor.ts b/apps/ppp/src/adapters/testing-actor.ts deleted file mode 100644 index dec41bd..0000000 --- a/apps/ppp/src/adapters/testing-actor.ts +++ /dev/null @@ -1,151 +0,0 @@ -import { createContext, inContext, type Context } from "libs/context"; -import { createLogger } from "libs/logger"; -import { - Actor, - MessageType, - WorkerConnection, - startRemote, - type Connection, - type EventMessage, - type IncomingMessage, - type OutgoingMessage, -} from "libs/actor"; -import { stringifyError } from "libs/error"; -import type { TestRunner, TestRunnerFactory } from "testing"; - -interface Handlers { - [key: string]: any; - init(code: string): Promise; - cancel(): void; - run(data: I): Promise; -} - -type Incoming = IncomingMessage>; - -interface WriteEventMessage extends EventMessage<"write", string> {} -interface WritelnEventMessage extends EventMessage<"writeln", string> {} - -type TestingActorEvent = WriteEventMessage | WritelnEventMessage; - -type Outgoing = - | OutgoingMessage, string> - | TestingActorEvent; - -class TestRunnerActor extends Actor, string> { - private ctx: Context = createContext(); - private runner: TestRunner | null = null; - - constructor( - connection: Connection, Outgoing>, - factory: TestRunnerFactory - ) { - const handlers: Handlers = { - init: async (code) => { - this.runner = await factory(this.ctx, { - code, - out: { - write(text) { - connection.send({ - type: MessageType.Event, - event: "write", - payload: text, - }); - }, - writeln(text) { - connection.send({ - type: MessageType.Event, - event: "writeln", - payload: text, - }); - }, - }, - }); - }, - cancel: () => { - this.ctx.cancel(); - this.ctx = createContext(); - }, - run: (input) => { - if (!this.runner) { - const err = new Error("Test runner not initialized"); - connection.send({ - type: MessageType.Event, - event: "error", - payload: err.message, - }); - throw err; - } - return this.runner.run(this.ctx, input); - }, - }; - super(connection, handlers, stringifyError); - } -} - -export function startTestRunnerActor(factory: TestRunnerFactory) { - const connection = new WorkerConnection, Outgoing>( - self as unknown as Worker - ); - const actor = new TestRunnerActor(connection, factory); - const stopConnection = connection.start(); - const stopActor = actor.start(); - return () => { - stopActor(); - stopConnection(); - }; -} - -interface WorkerConstructor { - new (): Worker; -} - -export function makeRemoteTestRunnerFactory( - Worker: WorkerConstructor -): TestRunnerFactory { - return async (ctx, { code, out }) => { - const worker = new Worker(); - const connection = new WorkerConnection, Incoming>( - worker - ); - const stopConnection = connection.start(); - const log = createLogger(out); - const remote = startRemote, string, TestingActorEvent>( - log, - connection, - { - error: (err) => log.error(err), - write: (text) => out.write(text), - writeln: (text) => out.writeln(text), - } - ); - const dispose = () => { - remote[Symbol.dispose](); - stopConnection(); - worker.terminate(); - }; - const clear = ctx.onCancel(() => { - remote.cancel(); - }); - try { - await inContext(ctx, remote.init(code)); - } catch (err) { - dispose(); - throw err; - } finally { - clear(); - } - return { - async run(ctx, input) { - const clear = ctx.onCancel(() => { - remote.cancel(); - }); - try { - return inContext(ctx, remote.run(input)); - } finally { - clear(); - } - }, - [Symbol.dispose]: dispose, - }; - }; -} diff --git a/apps/ppp/src/content/design-patterns/factory/editor.astro b/apps/ppp/src/content/design-patterns/factory/editor.astro index e8f5232..19835e6 100644 --- a/apps/ppp/src/content/design-patterns/factory/editor.astro +++ b/apps/ppp/src/content/design-patterns/factory/editor.astro @@ -9,23 +9,39 @@ const { contentId } = Astro.props;
diff --git a/content/design-patterns/factory/src/go/code.go b/apps/ppp/src/content/design-patterns/factory/go/code.go similarity index 100% rename from content/design-patterns/factory/src/go/code.go rename to apps/ppp/src/content/design-patterns/factory/go/code.go diff --git a/apps/ppp/src/content/design-patterns/factory/go/factory.ts b/apps/ppp/src/content/design-patterns/factory/go/factory.ts new file mode 100644 index 0000000..fa33937 --- /dev/null +++ b/apps/ppp/src/content/design-patterns/factory/go/factory.ts @@ -0,0 +1,25 @@ +// Only type imports are allowed + +import type { UniversalFactory } from "testing/actor"; + +import type { UniversalFactoryData } from "@/lib/workers/go"; + +import type { Input, Output } from "../tests-data"; + +export const factory: UniversalFactory = ({ + GoTestRunner, + goRuntimeFactory, + createLogger, +}) => { + class TestRunner extends GoTestRunner { + protected override generateCaseExecutionCode({ + paymentSystem, + amount, + base, + }: Input): string { + return `factory.Payment(factory.PaymentSystemType("${paymentSystem}"), ${base}, ${amount})`; + } + } + return async (ctx, { code, out }) => + new TestRunner(await goRuntimeFactory(ctx, createLogger(out), code)); +}; diff --git a/apps/ppp/src/content/design-patterns/factory/go/index.ts b/apps/ppp/src/content/design-patterns/factory/go/index.ts new file mode 100644 index 0000000..c87bdee --- /dev/null +++ b/apps/ppp/src/content/design-patterns/factory/go/index.ts @@ -0,0 +1,2 @@ +export { default as goCode } from "./code.go?raw"; +export { factory as goFactory } from "./factory"; diff --git a/apps/ppp/src/content/design-patterns/factory/code.js b/apps/ppp/src/content/design-patterns/factory/js/code.js similarity index 100% rename from apps/ppp/src/content/design-patterns/factory/code.js rename to apps/ppp/src/content/design-patterns/factory/js/code.js diff --git a/apps/ppp/src/content/design-patterns/factory/js-factory.ts b/apps/ppp/src/content/design-patterns/factory/js/factory.ts similarity index 81% rename from apps/ppp/src/content/design-patterns/factory/js-factory.ts rename to apps/ppp/src/content/design-patterns/factory/js/factory.ts index 7fa16b3..7e55e8a 100644 --- a/apps/ppp/src/content/design-patterns/factory/js-factory.ts +++ b/apps/ppp/src/content/design-patterns/factory/js/factory.ts @@ -3,14 +3,14 @@ import type { UniversalFactory } from "testing/actor"; import type { UniversalFactoryData } from "@/lib/workers/js"; -import type { Input, Output } from "./tests-data"; -import type { PaymentSystemType } from "./reference"; +import type { Input, Output } from "../tests-data"; +import type { PaymentSystemType } from "../reference"; interface TestingModule { payment(type: PaymentSystemType, base: number, amount: number): number; } -export const universalFactory: UniversalFactory< +export const factory: UniversalFactory< Input, Output, UniversalFactoryData diff --git a/apps/ppp/src/content/design-patterns/factory/js/index.ts b/apps/ppp/src/content/design-patterns/factory/js/index.ts new file mode 100644 index 0000000..db12b49 --- /dev/null +++ b/apps/ppp/src/content/design-patterns/factory/js/index.ts @@ -0,0 +1,2 @@ +export { default as jsCode } from "./code.js?raw"; +export { factory as jsFactory } from "./factory"; diff --git a/content/design-patterns/factory/src/php/code.php b/apps/ppp/src/content/design-patterns/factory/php/code.php similarity index 100% rename from content/design-patterns/factory/src/php/code.php rename to apps/ppp/src/content/design-patterns/factory/php/code.php diff --git a/apps/ppp/src/content/design-patterns/factory/php/factory.ts b/apps/ppp/src/content/design-patterns/factory/php/factory.ts new file mode 100644 index 0000000..b51c23e --- /dev/null +++ b/apps/ppp/src/content/design-patterns/factory/php/factory.ts @@ -0,0 +1,39 @@ +// Only type imports are allowed +import type { UniversalFactory } from "testing/actor"; + +import type { UniversalFactoryData } from "@/lib/workers/php"; + +import type { Input, Output } from "../tests-data"; + +// Const enum import is allowed +import type { PaymentSystemType } from "../reference"; + +export const factory: UniversalFactory = ({ + FailSafePHP, + PHPTestRunner, + phpRuntimeFactory, +}) => { + const PHP_PAYMENT_SYSTEM_TYPES: Record = { + "paypal": "PaymentSystemType::PAYPAL", + "webmoney": "PaymentSystemType::WEBMONEY", + "cat-bank": "PaymentSystemType::CAT_BANK", + }; + class TestRunner extends PHPTestRunner { + protected override caseExecutionCode({ + paymentSystem, + base, + amount, + }: Input): string { + return `strval(payment(${PHP_PAYMENT_SYSTEM_TYPES[paymentSystem]}, ${base}, ${amount}))`; + } + protected transformResult(result: string): Output { + const r = parseInt(result, 10); + if (isNaN(r)) { + throw new Error(`Invalid result type: ${result}, expected number`); + } + return r; + } + } + return async (_, { code, out }) => + new TestRunner(out, new FailSafePHP(phpRuntimeFactory), code); +}; diff --git a/apps/ppp/src/content/design-patterns/factory/php/index.ts b/apps/ppp/src/content/design-patterns/factory/php/index.ts new file mode 100644 index 0000000..238d5ff --- /dev/null +++ b/apps/ppp/src/content/design-patterns/factory/php/index.ts @@ -0,0 +1,2 @@ +export { default as phpCode } from "./code.php?raw"; +export { factory as phpFactory } from "./factory"; diff --git a/content/design-patterns/factory/src/python/code.py b/apps/ppp/src/content/design-patterns/factory/python/code.py similarity index 100% rename from content/design-patterns/factory/src/python/code.py rename to apps/ppp/src/content/design-patterns/factory/python/code.py diff --git a/apps/ppp/src/content/design-patterns/factory/python/factory.ts b/apps/ppp/src/content/design-patterns/factory/python/factory.ts new file mode 100644 index 0000000..25a5578 --- /dev/null +++ b/apps/ppp/src/content/design-patterns/factory/python/factory.ts @@ -0,0 +1,26 @@ +// Only type imports are allowed + +import type { UniversalFactory } from "testing/actor"; + +import type { UniversalFactoryData } from "@/lib/workers/python"; + +import type { Input, Output } from "../tests-data"; + +export const factory: UniversalFactory = ({ + createLogger, + PyTestRunner, + pyRuntimeFactory, +}) => { + class TestRunner extends PyTestRunner { + protected override caseExecutionCode({ + paymentSystem, + base, + amount, + }: Input): string { + return `payment("${paymentSystem}", ${base}, ${amount})`; + } + } + + return async (ctx, { code, out }) => + new TestRunner(await pyRuntimeFactory(ctx, createLogger(out)), code); +}; diff --git a/apps/ppp/src/content/design-patterns/factory/python/index.ts b/apps/ppp/src/content/design-patterns/factory/python/index.ts new file mode 100644 index 0000000..7cfcd15 --- /dev/null +++ b/apps/ppp/src/content/design-patterns/factory/python/index.ts @@ -0,0 +1,2 @@ +export { default as pyCode } from "./code.py?raw"; +export { factory as pyFactory } from "./factory"; diff --git a/content/design-patterns/factory/src/ts/code.ts b/apps/ppp/src/content/design-patterns/factory/ts/code.ts similarity index 100% rename from content/design-patterns/factory/src/ts/code.ts rename to apps/ppp/src/content/design-patterns/factory/ts/code.ts diff --git a/apps/ppp/src/content/design-patterns/factory/ts/factory.ts b/apps/ppp/src/content/design-patterns/factory/ts/factory.ts new file mode 100644 index 0000000..9f77371 --- /dev/null +++ b/apps/ppp/src/content/design-patterns/factory/ts/factory.ts @@ -0,0 +1,27 @@ +// Only type imports are allowed + +import type { UniversalFactory } from "testing/actor"; + +import type { UniversalFactoryData } from "@/lib/workers/ts"; + +import type { PaymentSystemType } from "../reference"; +import type { Input, Output } from "../tests-data"; + +interface TestingModule { + payment(type: PaymentSystemType, base: number, amount: number): number; +} + +export const factory: UniversalFactory = ({ + createLogger, + TsTestRunner, +}) => { + class TestRunner extends TsTestRunner { + override async executeTest( + m: TestingModule, + input: Input + ): Promise { + return m.payment(input.paymentSystem, input.base, input.amount); + } + } + return async (_, { code, out }) => new TestRunner(createLogger(out), code); +}; diff --git a/apps/ppp/src/content/design-patterns/factory/ts/index.ts b/apps/ppp/src/content/design-patterns/factory/ts/index.ts new file mode 100644 index 0000000..3a4bfa1 --- /dev/null +++ b/apps/ppp/src/content/design-patterns/factory/ts/index.ts @@ -0,0 +1,2 @@ +export { default as tsCode } from "./code.ts?raw"; +export { factory as tsFactory } from "./factory"; diff --git a/apps/ppp/src/lib/workers/go.ts b/apps/ppp/src/lib/workers/go.ts new file mode 100644 index 0000000..cfa27a8 --- /dev/null +++ b/apps/ppp/src/lib/workers/go.ts @@ -0,0 +1,18 @@ +import { createLogger } from "libs/logger"; +import { GoTestRunner, goRuntimeFactory } from "testing-go"; +import { startTestRunnerActor } from "testing/actor"; + +export interface UniversalFactoryData { + createLogger: typeof createLogger; + GoTestRunner: typeof GoTestRunner; + goRuntimeFactory: typeof goRuntimeFactory; +} + +startTestRunnerActor( + (universalFactory) => + universalFactory({ + GoTestRunner, + createLogger, + goRuntimeFactory, + }) +); diff --git a/apps/ppp/src/lib/workers/index.ts b/apps/ppp/src/lib/workers/index.ts new file mode 100644 index 0000000..e1896ab --- /dev/null +++ b/apps/ppp/src/lib/workers/index.ts @@ -0,0 +1,5 @@ +export { default as JsWorker } from "./js?worker"; +export { default as TsWorker } from "./ts?worker"; +export { default as PhpWorker } from "./php?worker"; +export { default as PyWorker } from "./python?worker"; +export { default as GoWorker } from "./go?worker"; diff --git a/apps/ppp/src/lib/workers/php.ts b/apps/ppp/src/lib/workers/php.ts new file mode 100644 index 0000000..e6ec587 --- /dev/null +++ b/apps/ppp/src/lib/workers/php.ts @@ -0,0 +1,17 @@ +import { FailSafePHP, PHPTestRunner, phpRuntimeFactory } from "testing-php"; +import { startTestRunnerActor } from "testing/actor"; + +export interface UniversalFactoryData { + PHPTestRunner: typeof PHPTestRunner; + FailSafePHP: typeof FailSafePHP; + phpRuntimeFactory: typeof phpRuntimeFactory; +} + +startTestRunnerActor( + (universalFactory) => + universalFactory({ + PHPTestRunner, + FailSafePHP, + phpRuntimeFactory, + }) +); diff --git a/apps/ppp/src/lib/workers/python.ts b/apps/ppp/src/lib/workers/python.ts new file mode 100644 index 0000000..91629f2 --- /dev/null +++ b/apps/ppp/src/lib/workers/python.ts @@ -0,0 +1,18 @@ +import { createLogger } from "libs/logger"; +import { PyTestRunner, pyRuntimeFactory } from "testing-python"; +import { startTestRunnerActor } from "testing/actor"; + +export interface UniversalFactoryData { + createLogger: typeof createLogger; + PyTestRunner: typeof PyTestRunner; + pyRuntimeFactory: typeof pyRuntimeFactory; +} + +startTestRunnerActor( + (universalFactory) => + universalFactory({ + PyTestRunner, + createLogger, + pyRuntimeFactory, + }) +); diff --git a/apps/ppp/src/lib/workers/ts.ts b/apps/ppp/src/lib/workers/ts.ts new file mode 100644 index 0000000..1e2a763 --- /dev/null +++ b/apps/ppp/src/lib/workers/ts.ts @@ -0,0 +1,16 @@ +import { createLogger } from "libs/logger"; +import { startTestRunnerActor } from "testing/actor"; +import { TsTestRunner } from "testing-typescript"; + +export interface UniversalFactoryData { + TsTestRunner: typeof TsTestRunner; + createLogger: typeof createLogger; +} + +startTestRunnerActor( + (universalFactory) => + universalFactory({ + TsTestRunner, + createLogger, + }) +); diff --git a/content/design-patterns/factory/package.json b/content/design-patterns/factory/package.json deleted file mode 100644 index 22654a5..0000000 --- a/content/design-patterns/factory/package.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "name": "design-patterns-factory", - "private": true, - "version": "0.0.0", - "type": "module", - "scripts": { - "check": "tsc", - "build": "vite build" - }, - "dependencies": { - "libs": "workspace:*", - "testing": "workspace:*", - "testing-javascript": "workspace:*", - "testing-typescript": "workspace:*", - "testing-php": "workspace:*", - "testing-python": "workspace:*", - "testing-go": "workspace:*" - }, - "devDependencies": { - "vite": "^5.2.0", - "vite-plugin-dts": "^3.9.1" - }, - "exports": { - ".": "./dist/index.js", - "./js": "./dist/js.js", - "./ts": "./dist/ts.js", - "./php": "./dist/php.js", - "./python": "./dist/python.js", - "./go": "./dist/go.js" - } -} \ No newline at end of file diff --git a/content/design-patterns/factory/src/go/index.ts b/content/design-patterns/factory/src/go/index.ts deleted file mode 100644 index a7f9679..0000000 --- a/content/design-patterns/factory/src/go/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from './runner' - -export { default as code } from './code.go?raw' -export { default as Worker } from './worker?worker' diff --git a/content/design-patterns/factory/src/go/runner.ts b/content/design-patterns/factory/src/go/runner.ts deleted file mode 100644 index 1f15424..0000000 --- a/content/design-patterns/factory/src/go/runner.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { createLogger } from "libs/logger"; -import { GoTestRunner, goRuntimeFactory } from "testing-go"; -import type { TestRunnerFactory } from "testing"; - -import type { Input, Output } from "../tests-data"; - -export class TestRunner extends GoTestRunner { - protected override generateCaseExecutionCode({ - paymentSystem, - amount, - base, - }: Input): string { - return `factory.Payment(factory.PaymentSystemType("${paymentSystem}"), ${base}, ${amount})`; - } -} - -export const factory: TestRunnerFactory = async ( - ctx, - { code, out } -) => new TestRunner(await goRuntimeFactory(ctx, createLogger(out), code)); diff --git a/content/design-patterns/factory/src/go/worker.ts b/content/design-patterns/factory/src/go/worker.ts deleted file mode 100644 index f78bfce..0000000 --- a/content/design-patterns/factory/src/go/worker.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { startTestRunnerActor } from "testing/actor" - -import { factory } from "./runner" - -startTestRunnerActor(factory) diff --git a/content/design-patterns/factory/src/index.ts b/content/design-patterns/factory/src/index.ts deleted file mode 100644 index 7433a8e..0000000 --- a/content/design-patterns/factory/src/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './tests-data.js' -export * from './reference.js' diff --git a/content/design-patterns/factory/src/js/code.js b/content/design-patterns/factory/src/js/code.js deleted file mode 100644 index 154770c..0000000 --- a/content/design-patterns/factory/src/js/code.js +++ /dev/null @@ -1,15 +0,0 @@ -const PAYMENT_SYSTEM_TYPE = { - PAYPAL: "paypal", - WEBMONEY: "webmoney", - CAT_BANK: "cat-bank", -}; - - - - - - - -export function payment(type, base, amount) { - throw new Error("Not implemented"); -} diff --git a/content/design-patterns/factory/src/js/index.ts b/content/design-patterns/factory/src/js/index.ts deleted file mode 100644 index 91e100b..0000000 --- a/content/design-patterns/factory/src/js/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from './runner' - -export { default as code } from "./code.js?raw"; -export { default as Worker } from './worker?worker' diff --git a/content/design-patterns/factory/src/js/runner.ts b/content/design-patterns/factory/src/js/runner.ts deleted file mode 100644 index ceeaad6..0000000 --- a/content/design-patterns/factory/src/js/runner.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { createLogger } from "libs/logger"; -import type { TestRunnerFactory } from "testing"; -import { JsTestRunner } from 'testing-javascript' - -import type { Input, Output } from "../tests-data"; -import type { PaymentSystemType } from "../reference"; - -interface TestingModule { - payment(type: PaymentSystemType, base: number, amount: number): number; -} - -export class TestRunner extends JsTestRunner { - async executeTest(m: TestingModule, input: Input): Promise { - return m.payment(input.paymentSystem, input.base, input.amount); - } -} - -export const factory: TestRunnerFactory = async (_, { code, out }) => - new TestRunner(createLogger(out), code); diff --git a/content/design-patterns/factory/src/js/worker.ts b/content/design-patterns/factory/src/js/worker.ts deleted file mode 100644 index 8e7422a..0000000 --- a/content/design-patterns/factory/src/js/worker.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { startTestRunnerActor } from "testing/actor"; - -import { factory } from './runner'; - -startTestRunnerActor(factory); diff --git a/content/design-patterns/factory/src/php/index.ts b/content/design-patterns/factory/src/php/index.ts deleted file mode 100644 index cf0b13a..0000000 --- a/content/design-patterns/factory/src/php/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from './runner' - -export { default as code } from './code.php?raw' -export { default as Worker } from './worker?worker' diff --git a/content/design-patterns/factory/src/php/runner.ts b/content/design-patterns/factory/src/php/runner.ts deleted file mode 100644 index 1a6555d..0000000 --- a/content/design-patterns/factory/src/php/runner.ts +++ /dev/null @@ -1,33 +0,0 @@ -import type { TestRunnerFactory } from "testing"; -import { FailSafePHP, PHPTestRunner, phpRuntimeFactory } from "testing-php"; - -import type { Input, Output } from "../tests-data"; -import { PaymentSystemType } from "../reference"; - -const PHP_PAYMENT_SYSTEM_TYPES: Record = { - [PaymentSystemType.PayPal]: "PaymentSystemType::PAYPAL", - [PaymentSystemType.WebMoney]: "PaymentSystemType::WEBMONEY", - [PaymentSystemType.CatBank]: "PaymentSystemType::CAT_BANK", -}; - -export class TestRunner extends PHPTestRunner { - protected override caseExecutionCode({ - paymentSystem, - base, - amount, - }: Input): string { - return `strval(payment(${PHP_PAYMENT_SYSTEM_TYPES[paymentSystem]}, ${base}, ${amount}))`; - } - protected transformResult(result: string): Output { - const r = parseInt(result, 10); - if (isNaN(r)) { - throw new Error(`Invalid result type: ${result}, expected number`); - } - return r; - } -} - -export const factory: TestRunnerFactory = async ( - _, - { code, out } -) => new TestRunner(out, new FailSafePHP(phpRuntimeFactory), code); diff --git a/content/design-patterns/factory/src/php/worker.ts b/content/design-patterns/factory/src/php/worker.ts deleted file mode 100644 index 8e7422a..0000000 --- a/content/design-patterns/factory/src/php/worker.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { startTestRunnerActor } from "testing/actor"; - -import { factory } from './runner'; - -startTestRunnerActor(factory); diff --git a/content/design-patterns/factory/src/python/index.ts b/content/design-patterns/factory/src/python/index.ts deleted file mode 100644 index db1aeb4..0000000 --- a/content/design-patterns/factory/src/python/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from './runner' - -export { default as code } from "./code.py?raw" -export { default as Worker } from "./worker?worker" diff --git a/content/design-patterns/factory/src/python/runner.ts b/content/design-patterns/factory/src/python/runner.ts deleted file mode 100644 index 969b25b..0000000 --- a/content/design-patterns/factory/src/python/runner.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { createLogger } from "libs/logger"; -import type { TestRunnerFactory } from "testing"; -import { PyTestRunner, pyRuntimeFactory } from "testing-python"; - -import type { Input, Output } from "../tests-data"; - -export class TestRunner extends PyTestRunner { - protected override caseExecutionCode({ - paymentSystem, - base, - amount, - }: Input): string { - return `payment("${paymentSystem}", ${base}, ${amount})`; - } -} - -export const factory: TestRunnerFactory = async ( - ctx, - { code, out } -) => new TestRunner(await pyRuntimeFactory(ctx, createLogger(out)), code); diff --git a/content/design-patterns/factory/src/python/worker.ts b/content/design-patterns/factory/src/python/worker.ts deleted file mode 100644 index c35a06d..0000000 --- a/content/design-patterns/factory/src/python/worker.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { startTestRunnerActor } from "testing/actor"; - -import { factory } from "./runner"; - -startTestRunnerActor(factory); diff --git a/content/design-patterns/factory/src/reference.ts b/content/design-patterns/factory/src/reference.ts deleted file mode 100644 index e90e519..0000000 --- a/content/design-patterns/factory/src/reference.ts +++ /dev/null @@ -1,36 +0,0 @@ -export const enum PaymentSystemType { - PayPal = "paypal", - WebMoney = "webmoney", - CatBank = "cat-bank", -} - -interface PaymentSystem { - payment(amount: number): number; - payout(amount: number): number; -} - -type PaymentSystemFactory = (base: number) => PaymentSystem; - -const paymentSystemFactories: Record = - { - [PaymentSystemType.PayPal]: (base) => ({ - payment: (amount: number) => base + amount, - payout: (amount: number) => base - amount, - }), - [PaymentSystemType.WebMoney]: (base) => ({ - payment: (amount: number) => base + 2 * amount, - payout: (amount: number) => base - 2 * amount, - }), - [PaymentSystemType.CatBank]: (base) => ({ - payment: (amount: number) => base * amount, - payout: (amount: number) => base / amount, - }), - }; - -export function payment( - type: PaymentSystemType, - base: number, - amount: number -): number { - return paymentSystemFactories[type](base).payment(amount); -} diff --git a/content/design-patterns/factory/src/tests-data.ts b/content/design-patterns/factory/src/tests-data.ts deleted file mode 100644 index 6a504da..0000000 --- a/content/design-patterns/factory/src/tests-data.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { testData } from "testing"; - -import { PaymentSystemType, payment } from "./reference"; - -export interface Input { - paymentSystem: PaymentSystemType; - base: number; - amount: number; -} - -export type Output = number; - -export const testsData = [ - { - paymentSystem: PaymentSystemType.PayPal, - base: 1, - amount: 1, - }, - { - paymentSystem: PaymentSystemType.WebMoney, - base: 1, - amount: 1, - }, - { - paymentSystem: PaymentSystemType.CatBank, - base: 1, - amount: 1, - }, -].map( - testData((input) => payment(input.paymentSystem, input.base, input.amount)) -); diff --git a/content/design-patterns/factory/src/ts/index.ts b/content/design-patterns/factory/src/ts/index.ts deleted file mode 100644 index 2513fed..0000000 --- a/content/design-patterns/factory/src/ts/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from './runner.js' - -export { default as code } from './code.ts?raw' -export { default as Worker } from './worker?worker' diff --git a/content/design-patterns/factory/src/ts/runner.ts b/content/design-patterns/factory/src/ts/runner.ts deleted file mode 100644 index 3e8c35d..0000000 --- a/content/design-patterns/factory/src/ts/runner.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { createLogger } from "libs/logger"; -import type { TestRunnerConfig, TestRunnerFactory } from "testing"; -import { TsTestRunner } from "testing-typescript"; - -import type { Input, Output } from "../tests-data"; -import type { PaymentSystemType } from "../reference"; - -interface TestingModule { - payment(type: PaymentSystemType, base: number, amount: number): number; -} - -export class TestRunner extends TsTestRunner { - async executeTest(m: TestingModule, input: Input): Promise { - return m.payment(input.paymentSystem, input.base, input.amount); - } -} - -export const factory: TestRunnerFactory = async ( - _, - { code, out }: TestRunnerConfig -) => new TestRunner(createLogger(out), code); diff --git a/content/design-patterns/factory/src/ts/worker.ts b/content/design-patterns/factory/src/ts/worker.ts deleted file mode 100644 index f7d1be6..0000000 --- a/content/design-patterns/factory/src/ts/worker.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { startTestRunnerActor } from "testing/actor"; - -import { factory } from './runner.js' - -startTestRunnerActor(factory); diff --git a/content/design-patterns/factory/tsconfig.json b/content/design-patterns/factory/tsconfig.json deleted file mode 100644 index 79e443e..0000000 --- a/content/design-patterns/factory/tsconfig.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "extends": "@total-typescript/tsconfig/bundler/dom/library-monorepo", - "compilerOptions": { - "lib": [ - "ES2022", - "ESNext.Disposable", - "DOM" - ], - "noUncheckedIndexedAccess": false, - "types": [ - "vite/client" - ] - }, -} \ No newline at end of file diff --git a/content/design-patterns/factory/vite.config.js b/content/design-patterns/factory/vite.config.js deleted file mode 100644 index 318ed2c..0000000 --- a/content/design-patterns/factory/vite.config.js +++ /dev/null @@ -1,46 +0,0 @@ -import { resolve } from "path"; -import { defineConfig } from "vite"; -import dts from "vite-plugin-dts"; - -export default defineConfig({ - worker: { - format: "es", - }, - // Workers support in lib mode - // https://github.com/vitejs/vite/discussions/15547#discussioncomment-8950765 - base: "./", - build: { - lib: { - // Could also be a dictionary or array of multiple entry points - entry: { - index: resolve(__dirname, "src/index.ts"), - js: resolve(__dirname, "src/js/index.ts"), - ts: resolve(__dirname, "src/ts/index.ts"), - php: resolve(__dirname, "src/php/index.ts"), - python: resolve(__dirname, "src/python/index.ts"), - go: resolve(__dirname, "src/go/index.ts"), - }, - formats: ["es"], - // name: "MyLib", - // the proper extensions will be added - // fileName: "index", - }, - rollupOptions: { - // make sure to externalize deps that shouldn't be bundled - // into your library - external: [/^libs\//], - output: { - // Provide global variables to use in the UMD build - // for externalized deps - globals: { - // vue: "Vue", - }, - }, - }, - }, - plugins: [ - dts({ - rollupTypes: true, - }), - ], -}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6d15913..0071865 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -62,9 +62,21 @@ importers: testing: specifier: workspace:* version: link:../../packages/testing + testing-go: + specifier: workspace:* + version: link:../../packages/testing-go testing-javascript: specifier: workspace:* version: link:../../packages/testing-javascript + testing-php: + specifier: workspace:* + version: link:../../packages/testing-php + testing-python: + specifier: workspace:* + version: link:../../packages/testing-python + testing-typescript: + specifier: workspace:* + version: link:../../packages/testing-typescript devDependencies: '@iconify-json/lucide': specifier: ^1.1.191 @@ -88,37 +100,6 @@ importers: specifier: 5.0.0-next.133 version: 5.0.0-next.133 - content/design-patterns/factory: - dependencies: - libs: - specifier: workspace:* - version: link:../../../packages/libs - testing: - specifier: workspace:* - version: link:../../../packages/testing - testing-go: - specifier: workspace:* - version: link:../../../packages/testing-go - testing-javascript: - specifier: workspace:* - version: link:../../../packages/testing-javascript - testing-php: - specifier: workspace:* - version: link:../../../packages/testing-php - testing-python: - specifier: workspace:* - version: link:../../../packages/testing-python - testing-typescript: - specifier: workspace:* - version: link:../../../packages/testing-typescript - devDependencies: - vite: - specifier: ^5.2.0 - version: 5.2.13(@types/node@20.14.2) - vite-plugin-dts: - specifier: ^3.9.1 - version: 3.9.1(@types/node@20.14.2)(rollup@4.18.0)(typescript@5.4.5)(vite@5.2.13(@types/node@20.14.2)) - packages/libs: {} packages/testing: diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index c5c0d6f..0e5a073 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,4 +1,3 @@ packages: - "packages/*" - "apps/*" - - "content/**" From 52ed261d95a51f1c6698e6ad7535f97fbf1477f5 Mon Sep 17 00:00:00 2001 From: Krasilnikov Roman Date: Wed, 26 Jun 2024 09:27:21 +0300 Subject: [PATCH 4/9] Unbundle go wasm --- .../design-patterns/factory/go/factory.ts | 27 +++---- apps/ppp/src/lib/workers/go.ts | 53 +++++++++++--- packages/testing-go/mkfilex | 5 +- packages/testing-go/package.json | 5 +- .../testing-go/src/go-executor-factory.ts | 16 ----- packages/testing-go/src/go-runtime-factory.ts | 72 ++++++++----------- packages/testing-go/src/index.ts | 1 - packages/testing-go/src/model.ts | 8 +++ packages/testing-go/vite.config.js | 6 +- 9 files changed, 100 insertions(+), 93 deletions(-) delete mode 100644 packages/testing-go/src/go-executor-factory.ts diff --git a/apps/ppp/src/content/design-patterns/factory/go/factory.ts b/apps/ppp/src/content/design-patterns/factory/go/factory.ts index fa33937..6a27760 100644 --- a/apps/ppp/src/content/design-patterns/factory/go/factory.ts +++ b/apps/ppp/src/content/design-patterns/factory/go/factory.ts @@ -2,24 +2,17 @@ import type { UniversalFactory } from "testing/actor"; -import type { UniversalFactoryData } from "@/lib/workers/go"; +import type { GoUniversalFactoryData } from "@/lib/workers/go"; import type { Input, Output } from "../tests-data"; -export const factory: UniversalFactory = ({ - GoTestRunner, - goRuntimeFactory, - createLogger, -}) => { - class TestRunner extends GoTestRunner { - protected override generateCaseExecutionCode({ - paymentSystem, - amount, - base, - }: Input): string { - return `factory.Payment(factory.PaymentSystemType("${paymentSystem}"), ${base}, ${amount})`; - } - } - return async (ctx, { code, out }) => - new TestRunner(await goRuntimeFactory(ctx, createLogger(out), code)); +export const factory: UniversalFactory< + Input, + Output, + GoUniversalFactoryData +> = ({ makeTestRunnerFactory }) => { + return makeTestRunnerFactory( + ({ paymentSystem, amount, base }) => + `factory.Payment(factory.PaymentSystemType("${paymentSystem}"), ${base}, ${amount})` + ); }; diff --git a/apps/ppp/src/lib/workers/go.ts b/apps/ppp/src/lib/workers/go.ts index cfa27a8..50efa55 100644 --- a/apps/ppp/src/lib/workers/go.ts +++ b/apps/ppp/src/lib/workers/go.ts @@ -1,18 +1,51 @@ import { createLogger } from "libs/logger"; -import { GoTestRunner, goRuntimeFactory } from "testing-go"; +import type { TestRunnerFactory } from "testing"; +import { + GoTestRunner, + createCompilerFactory, + makeGoRuntimeFactory, + type GoRuntimeFactory, +} from "testing-go"; import { startTestRunnerActor } from "testing/actor"; +import wasmInit from "testing-go/compiler.wasm?init"; -export interface UniversalFactoryData { +export interface GoUniversalFactoryData { createLogger: typeof createLogger; GoTestRunner: typeof GoTestRunner; - goRuntimeFactory: typeof goRuntimeFactory; + goRuntimeFactory: GoRuntimeFactory; + compilerFactory: ReturnType; + makeTestRunnerFactory: ( + generateCaseExecutionCode: (input: I) => string + ) => TestRunnerFactory; } -startTestRunnerActor( - (universalFactory) => - universalFactory({ - GoTestRunner, - createLogger, - goRuntimeFactory, - }) +const compilerFactory = createCompilerFactory(wasmInit); + +startTestRunnerActor< + unknown, + unknown, + GoUniversalFactoryData +>((universalFactory) => + universalFactory({ + GoTestRunner, + createLogger, + compilerFactory, + goRuntimeFactory: async (ctx, log, code) => + makeGoRuntimeFactory(await compilerFactory)(ctx, log, code), + makeTestRunnerFactory: (generateCaseExecutionCode) => { + class TestRunner extends GoTestRunner { + protected override generateCaseExecutionCode(input: unknown): string { + return generateCaseExecutionCode(input); + } + } + return async (ctx, { code, out }) => + new TestRunner( + await makeGoRuntimeFactory(await compilerFactory)( + ctx, + createLogger(out), + code + ) + ); + }, + }) ); diff --git a/packages/testing-go/mkfilex b/packages/testing-go/mkfilex index 8a983a1..ab4261b 100644 --- a/packages/testing-go/mkfilex +++ b/packages/testing-go/mkfilex @@ -3,7 +3,7 @@ go/: pushd go build: - GOOS=js GOARCH=wasm go build -o ../assets/compiler.wasm cmd/compiler/main.go + GOOS=js GOARCH=wasm go build -o ../public/compiler.wasm cmd/compiler/main.go p: go run cmd/probe/main.go tidy: @@ -12,3 +12,6 @@ go/: p: go/build bun run probe/index.ts + +b: go/build + pnpm run build diff --git a/packages/testing-go/package.json b/packages/testing-go/package.json index 7f8aa9c..cbe50ef 100644 --- a/packages/testing-go/package.json +++ b/packages/testing-go/package.json @@ -5,7 +5,7 @@ "type": "module", "scripts": { "check": "tsc", - "build:wasm": "cd go && GOOS=js GOARCH=wasm go build -o ../assets/compiler.wasm cmd/compiler/main.go && cd -", + "build:wasm": "cd go && GOOS=js GOARCH=wasm go build -o ../public/compiler.wasm cmd/compiler/main.go && cd -", "build:js": "vite build", "build": "pnpm run build:wasm && pnpm run build:js" }, @@ -19,6 +19,7 @@ "vite-plugin-dts": "^3.9.1" }, "exports": { - ".": "./dist/index.js" + ".": "./dist/index.js", + "./compiler.wasm": "./dist/compiler.wasm" } } \ No newline at end of file diff --git a/packages/testing-go/src/go-executor-factory.ts b/packages/testing-go/src/go-executor-factory.ts deleted file mode 100644 index 169db6f..0000000 --- a/packages/testing-go/src/go-executor-factory.ts +++ /dev/null @@ -1,16 +0,0 @@ -import type { Context } from "libs/context"; -import { isErr } from "libs/result"; - -import { type Compiler, type Executor } from "./model"; - -export async function goExecutorFactory( - ctx: Context, - compiler: Compiler, - code: string -): Promise> { - const executor = await compiler.compile(ctx.signal, code); - if (isErr(executor)) { - throw new Error(executor.error); - } - return executor.value; -} diff --git a/packages/testing-go/src/go-runtime-factory.ts b/packages/testing-go/src/go-runtime-factory.ts index 2a8b30f..3818b76 100644 --- a/packages/testing-go/src/go-runtime-factory.ts +++ b/packages/testing-go/src/go-runtime-factory.ts @@ -1,47 +1,37 @@ -import type { Context } from "libs/context"; -import { redirect, type Logger } from "libs/logger"; +import { redirect } from "libs/logger"; import { isErr } from "libs/result"; -import wasmUrl from "../assets/compiler.wasm?url"; +import { LogLevel, type CompilerFactory, type GoRuntimeFactory } from "./model"; -import { createCompilerFactory } from "./go-compiler-factory"; -import { LogLevel } from "./model"; -import { goExecutorFactory } from "./go-executor-factory"; - -export async function goRuntimeFactory( - ctx: Context, - log: Logger, - code: string -) { - const makeCompiler = await createCompilerFactory( - async (imports) => - ( - await WebAssembly.instantiateStreaming( - fetch(wasmUrl, { signal: ctx.signal }), - imports - ) - ).instance - ); - const compiler = makeCompiler({ - logger: { - level: LogLevel.Info, - console: redirect(globalThis.console, log), - }, - stdout: { - write(text) { - log.debug(text); - return null; +export function makeGoRuntimeFactory( + makeCompiler: CompilerFactory +): GoRuntimeFactory { + return async (ctx, log, code) => { + const compiler = makeCompiler({ + logger: { + level: LogLevel.Info, + console: redirect(globalThis.console, log), + }, + stdout: { + write(text) { + log.debug(text); + return null; + }, }, - }, - stderr: { - write(text) { - log.error(text); - return null; + stderr: { + write(text) { + log.error(text); + return null; + }, }, - }, - }); - if (isErr(compiler)) { - throw new Error(compiler.error); - } - return goExecutorFactory(ctx, compiler.value, code); + }); + if (isErr(compiler)) { + throw new Error(compiler.error); + } + const executor = await compiler.value.compile(ctx.signal, code); + if (isErr(executor)) { + throw new Error(executor.error); + } + return executor.value; + }; } diff --git a/packages/testing-go/src/index.ts b/packages/testing-go/src/index.ts index 5fc7406..afcbb37 100644 --- a/packages/testing-go/src/index.ts +++ b/packages/testing-go/src/index.ts @@ -1,6 +1,5 @@ export * from './model.js' export * from './go-compiler-factory.js' -export * from './go-executor-factory.js' export * from './go-runtime-factory.js' export * from './go-test-runner.js' export * from './version.js' diff --git a/packages/testing-go/src/model.ts b/packages/testing-go/src/model.ts index 896bb5b..078aa02 100644 --- a/packages/testing-go/src/model.ts +++ b/packages/testing-go/src/model.ts @@ -1,3 +1,5 @@ +import type { Context } from "libs/context"; +import type { Logger } from "libs/logger"; import type { Result } from "libs/result"; export enum LogLevel { @@ -39,5 +41,11 @@ export type CompilerFactory = ( config: CompilerConfig ) => Result; +export type GoRuntimeFactory = ( + ctx: Context, + log: Logger, + code: string +) => Promise>; + export const DEFAULT_GLOBAL_COMPILER_INIT_FUNCTION_NAME = "__compiler_init_function"; diff --git a/packages/testing-go/vite.config.js b/packages/testing-go/vite.config.js index 55f7799..85e57cf 100644 --- a/packages/testing-go/vite.config.js +++ b/packages/testing-go/vite.config.js @@ -25,9 +25,5 @@ export default defineConfig({ }, }, }, - plugins: [ - dts({ - rollupTypes: true, - }), - ], + plugins: [dts()], }); From 9fefd10368b87e5f9185747fd734539eeab554e3 Mon Sep 17 00:00:00 2001 From: Krasilnikov Roman Date: Wed, 26 Jun 2024 09:31:35 +0300 Subject: [PATCH 5/9] Disable dts merge --- packages/testing-php/vite.config.js | 4 +--- packages/testing-python/vite.config.js | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/testing-php/vite.config.js b/packages/testing-php/vite.config.js index ad97fe4..536cb5c 100644 --- a/packages/testing-php/vite.config.js +++ b/packages/testing-php/vite.config.js @@ -49,8 +49,6 @@ export default defineConfig({ } }, }, - dts({ - rollupTypes: true, - }), + dts(), ], }); diff --git a/packages/testing-python/vite.config.js b/packages/testing-python/vite.config.js index 6e20614..5a24ea4 100644 --- a/packages/testing-python/vite.config.js +++ b/packages/testing-python/vite.config.js @@ -36,8 +36,6 @@ export default defineConfig({ }, }, plugins: [ - dts({ - rollupTypes: true, - }), + dts(), ], }); From 325f2e197596abd46cd402a2ff7a8ebfb771538c Mon Sep 17 00:00:00 2001 From: Krasilnikov Roman Date: Wed, 26 Jun 2024 13:51:14 +0300 Subject: [PATCH 6/9] Unbundle python wasm --- apps/ppp/astro.config.mjs | 1 + apps/ppp/package.json | 6 +-- apps/ppp/src/lib/workers/python.ts | 22 ++++++++-- packages/testing-python/package.json | 11 +++-- .../testing-python/src/py-runtime-factory.ts | 12 +++--- packages/testing-python/vite.config.js | 13 ++++++ pnpm-lock.yaml | 42 +++++++++++++++++++ 7 files changed, 92 insertions(+), 15 deletions(-) diff --git a/apps/ppp/astro.config.mjs b/apps/ppp/astro.config.mjs index 814ef6b..3cffef5 100644 --- a/apps/ppp/astro.config.mjs +++ b/apps/ppp/astro.config.mjs @@ -25,6 +25,7 @@ export default defineConfig({ external: ["sharp"], }, }, + assetsInclude: ["**/*.wasm", "**/*.zip"], }, markdown: { shikiConfig: { diff --git a/apps/ppp/package.json b/apps/ppp/package.json index a5823fd..6b36e92 100644 --- a/apps/ppp/package.json +++ b/apps/ppp/package.json @@ -19,16 +19,16 @@ "@xterm/xterm": "^5.5.0", "astro": "^4.10.2", "astro-icon": "^1.1.0", + "libs": "workspace:*", "monaco-editor": "^0.49.0", "monaco-vim": "^0.4.1", "tailwindcss": "^3.4.4", - "libs": "workspace:*", "testing": "workspace:*", + "testing-go": "workspace:*", "testing-javascript": "workspace:*", - "testing-typescript": "workspace:*", "testing-php": "workspace:*", "testing-python": "workspace:*", - "testing-go": "workspace:*" + "testing-typescript": "workspace:*" }, "devDependencies": { "@iconify-json/lucide": "^1.1.191", diff --git a/apps/ppp/src/lib/workers/python.ts b/apps/ppp/src/lib/workers/python.ts index 91629f2..00247d0 100644 --- a/apps/ppp/src/lib/workers/python.ts +++ b/apps/ppp/src/lib/workers/python.ts @@ -1,11 +1,20 @@ -import { createLogger } from "libs/logger"; +import type { Context } from "libs/context"; +import { createLogger, type Logger } from "libs/logger"; import { PyTestRunner, pyRuntimeFactory } from "testing-python"; import { startTestRunnerActor } from "testing/actor"; +// @ts-ignore +import wasmUrl from "testing-python/pyodide.wasm"; +// @ts-ignore +import stdlibUrl from "testing-python/python-stdlib.zip"; + export interface UniversalFactoryData { createLogger: typeof createLogger; PyTestRunner: typeof PyTestRunner; - pyRuntimeFactory: typeof pyRuntimeFactory; + pyRuntimeFactory: ( + ctx: Context, + log: Logger + ) => ReturnType; } startTestRunnerActor( @@ -13,6 +22,13 @@ startTestRunnerActor( universalFactory({ PyTestRunner, createLogger, - pyRuntimeFactory, + pyRuntimeFactory: (ctx, log) => + pyRuntimeFactory( + ctx, + log, + (imports) => + WebAssembly.instantiateStreaming(fetch(wasmUrl), imports), + stdlibUrl + ), }) ); diff --git a/packages/testing-python/package.json b/packages/testing-python/package.json index fc7f0b9..cbf86ee 100644 --- a/packages/testing-python/package.json +++ b/packages/testing-python/package.json @@ -9,14 +9,17 @@ }, "dependencies": { "libs": "workspace:*", - "testing": "workspace:*", - "pyodide": "^0.26.1" + "pyodide": "^0.26.1", + "testing": "workspace:*" }, "devDependencies": { "vite": "^5.2.0", - "vite-plugin-dts": "^3.9.1" + "vite-plugin-dts": "^3.9.1", + "vite-plugin-static-copy": "^1.0.5" }, "exports": { - ".": "./dist/index.js" + ".": "./dist/index.js", + "./pyodide.wasm": "./dist/pyodide/pyodide.asm.wasm", + "./python-stdlib.zip": "./dist/pyodide/python_stdlib.zip" } } \ No newline at end of file diff --git a/packages/testing-python/src/py-runtime-factory.ts b/packages/testing-python/src/py-runtime-factory.ts index 639e7f7..2e37bd7 100644 --- a/packages/testing-python/src/py-runtime-factory.ts +++ b/packages/testing-python/src/py-runtime-factory.ts @@ -1,6 +1,4 @@ import { loadPyodide, type PyodideInterface } from "pyodide"; -import wasmUrl from "pyodide/pyodide.asm.wasm?url"; -import stdLibUrl from "pyodide/python_stdlib.zip?url"; import lockFilerUrl from "pyodide/pyodide-lock.json?url"; import "pyodide/pyodide.asm.js"; @@ -27,7 +25,11 @@ const originalCreatePyodideModule = globalThis._createPyodideModule; export const pyRuntimeFactory = async ( ctx: Context, - log: Logger + log: Logger, + wasmInstance: ( + imports: WebAssembly.Imports + ) => Promise, + stdLibUrl: string ): Promise => { const recover = patch( globalThis, @@ -36,7 +38,7 @@ export const pyRuntimeFactory = async ( return originalCreatePyodideModule({ ...settings, instantiateWasm(imports, callback) { - WebAssembly.instantiateStreaming(fetch(wasmUrl), imports).then( + wasmInstance(imports).then( ({ instance, module }) => { callback(instance, module); }, @@ -50,7 +52,7 @@ export const pyRuntimeFactory = async ( return await inContext( ctx, loadPyodide({ - indexURL: "intentionally-missing-url", + indexURL: "intentionally-missing-index-url", stdLibURL: stdLibUrl, lockFileURL: lockFilerUrl, stdout: log.debug.bind(log), diff --git a/packages/testing-python/vite.config.js b/packages/testing-python/vite.config.js index 5a24ea4..51bf813 100644 --- a/packages/testing-python/vite.config.js +++ b/packages/testing-python/vite.config.js @@ -1,6 +1,7 @@ import { resolve } from "path"; import { defineConfig, createLogger } from "vite"; import dts from "vite-plugin-dts"; +import { viteStaticCopy } from "vite-plugin-static-copy"; const logger = createLogger(); const loggerWarn = logger.warn; @@ -37,5 +38,17 @@ export default defineConfig({ }, plugins: [ dts(), + viteStaticCopy({ + targets: [ + { + src: "node_modules/pyodide/pyodide.asm.wasm", + dest: "pyodide", + }, + { + src: "node_modules/pyodide/python_stdlib.zip", + dest: "pyodide", + }, + ], + }), ], }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0071865..efdee61 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -176,6 +176,9 @@ importers: vite-plugin-dts: specifier: ^3.9.1 version: 3.9.1(@types/node@20.14.2)(rollup@4.18.0)(typescript@5.4.5)(vite@5.2.13(@types/node@20.14.2)) + vite-plugin-static-copy: + specifier: ^1.0.5 + version: 1.0.5(vite@5.2.13(@types/node@20.14.2)) packages/testing-typescript: dependencies: @@ -1688,6 +1691,10 @@ packages: fraction.js@4.3.7: resolution: {integrity: sha512-ZsDfxO51wGAXREY55a7la9LScWpwv9RxIrYABrlvOFBlH/ShPnrtsXeuUIfXKKOVicNxQ+o8JTbJvjS4M89yew==} + fs-extra@11.2.0: + resolution: {integrity: sha512-PmDi3uwK5nFuXh7XDTlVnS17xJS7vW36is2+w3xcv8SVxiB4NyATf4ctkVY5bkSjX0Y4nbvZCq1/EjtEyr9ktw==} + engines: {node: '>=14.14'} + fs-extra@7.0.1: resolution: {integrity: sha512-YJDaCJZEnBmcbw13fvdAM9AwNOJwOzrE4pqMqBq5nFiEqXUqHwlK4B+3pUw6JNvfSPtX05xFHtYy/1ni01eGCw==} engines: {node: '>=6 <7 || >=8'} @@ -1975,6 +1982,9 @@ packages: jsonfile@4.0.0: resolution: {integrity: sha512-m6F1R3z8jjlf2imQHS2Qez5sjKWQzbuuhuJ/FKYFRZvPE3PuHcSMVZzfsLhGVOkfd20obL5SWEBew5ShlquNxg==} + jsonfile@6.1.0: + resolution: {integrity: sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==} + kind-of@6.0.3: resolution: {integrity: sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==} engines: {node: '>=0.10.0'} @@ -2982,6 +2992,10 @@ packages: resolution: {integrity: sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==} engines: {node: '>= 4.0.0'} + universalify@2.0.1: + resolution: {integrity: sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==} + engines: {node: '>= 10.0.0'} + update-browserslist-db@1.0.16: resolution: {integrity: sha512-KVbTxlBYlckhF5wgfyZXTWnMn7MMZjMu9XG8bPlliUOP9ThaF4QnhP8qrjrH7DRzHfSk0oQv1wToW+iA5GajEQ==} hasBin: true @@ -3023,6 +3037,12 @@ packages: vite: optional: true + vite-plugin-static-copy@1.0.5: + resolution: {integrity: sha512-02k0Rox+buYdEOfeilKZSgs1gXfPf9RjVztZEIYZgVIxjsVZi6AXssjzdi+qW6zYt00d3bq+tpP2voVXN2fKLw==} + engines: {node: ^18.0.0 || >=20.0.0} + peerDependencies: + vite: ^5.0.0 + vite@5.2.13: resolution: {integrity: sha512-SSq1noJfY9pR3I1TUENL3rQYDQCFqgD+lM6fTRAM8Nv6Lsg5hDLaXkjETVeBt+7vZBCMoibD+6IWnT2mJ+Zb/A==} engines: {node: ^18.0.0 || >=20.0.0} @@ -4924,6 +4944,12 @@ snapshots: fraction.js@4.3.7: {} + fs-extra@11.2.0: + dependencies: + graceful-fs: 4.2.11 + jsonfile: 6.1.0 + universalify: 2.0.1 + fs-extra@7.0.1: dependencies: graceful-fs: 4.2.11 @@ -5244,6 +5270,12 @@ snapshots: optionalDependencies: graceful-fs: 4.2.11 + jsonfile@6.1.0: + dependencies: + universalify: 2.0.1 + optionalDependencies: + graceful-fs: 4.2.11 + kind-of@6.0.3: {} kleur@3.0.3: {} @@ -6649,6 +6681,8 @@ snapshots: universalify@0.1.2: {} + universalify@2.0.1: {} + update-browserslist-db@1.0.16(browserslist@4.23.1): dependencies: browserslist: 4.23.1 @@ -6708,6 +6742,14 @@ snapshots: - rollup - supports-color + vite-plugin-static-copy@1.0.5(vite@5.2.13(@types/node@20.14.2)): + dependencies: + chokidar: 3.6.0 + fast-glob: 3.3.2 + fs-extra: 11.2.0 + picocolors: 1.0.1 + vite: 5.2.13(@types/node@20.14.2) + vite@5.2.13(@types/node@20.14.2): dependencies: esbuild: 0.20.2 From 216a14c73a59c39b74832356ebabbd156da6af7f Mon Sep 17 00:00:00 2001 From: Krasilnikov Roman Date: Wed, 26 Jun 2024 15:04:02 +0300 Subject: [PATCH 7/9] Simplify test factories --- apps/ppp/src/adapters/monaco.ts | 2 +- apps/ppp/src/adapters/svelte-editor.ts | 42 +++++++++++++++++-- .../design-patterns/factory/editor.astro | 21 ++++------ .../design-patterns/factory/js/factory.ts | 16 +++---- .../design-patterns/factory/php/factory.ts | 31 ++++++-------- .../design-patterns/factory/python/factory.ts | 26 ++++-------- apps/ppp/src/lib/workers/js.ts | 30 +++++++++---- apps/ppp/src/lib/workers/php.ts | 21 +++++++++- apps/ppp/src/lib/workers/python.ts | 26 +++++++++++- apps/ppp/src/shared/index.ts | 1 + apps/ppp/src/{lib => shared}/languages.ts | 0 apps/ppp/src/{ => shared}/shared.ts | 0 12 files changed, 142 insertions(+), 74 deletions(-) create mode 100644 apps/ppp/src/shared/index.ts rename apps/ppp/src/{lib => shared}/languages.ts (100%) rename apps/ppp/src/{ => shared}/shared.ts (100%) diff --git a/apps/ppp/src/adapters/monaco.ts b/apps/ppp/src/adapters/monaco.ts index 7bf8f66..6569a65 100644 --- a/apps/ppp/src/adapters/monaco.ts +++ b/apps/ppp/src/adapters/monaco.ts @@ -1,4 +1,4 @@ -import { Language } from "@/lib/languages"; +import { Language } from "@/shared/languages"; export const MONACO_LANGUAGE_ID: Record = { [Language.PHP]: "php", diff --git a/apps/ppp/src/adapters/svelte-editor.ts b/apps/ppp/src/adapters/svelte-editor.ts index 0808dd8..8cbe9e1 100644 --- a/apps/ppp/src/adapters/svelte-editor.ts +++ b/apps/ppp/src/adapters/svelte-editor.ts @@ -2,21 +2,57 @@ import { mount } from "svelte"; import type { TestData, TestRunnerFactory } from "testing"; +import { + GoWorker, + JsWorker, + PhpWorker, + PyWorker, + TsWorker, +} from "@/lib/workers"; +import { Language } from "@/shared/languages"; import Editor, { type Props } from "@/components/editor/editor.svelte"; -import type { Language } from "@/lib/languages"; +import { + makeRemoteTestRunnerFactory, + type UniversalFactory, +} from "testing/actor"; + +export const LANG_WORKERS: Record Worker> = { + [Language.Go]: GoWorker, + [Language.PHP]: PhpWorker, + [Language.Python]: PyWorker, + [Language.TypeScript]: TsWorker, + [Language.JavaScript]: JsWorker, +}; export function mountEditor( testsData: TestData[], runtimes: Record< L, - { initialValue: string; testRunnerFactory: TestRunnerFactory } + { initialValue: string; factory: UniversalFactory } > ) { const element = document.getElementById("editor-placeholder")!; const props = { contentId: element.dataset.contentId!, testsData, - runtimes, + runtimes: Object.fromEntries( + Object.keys(runtimes).map( + (lang) => + [ + lang, + { + initialValue: runtimes[lang as L].initialValue, + testRunnerFactory: makeRemoteTestRunnerFactory( + LANG_WORKERS[lang as L], + runtimes[lang as L].factory + ), + }, + ] as const + ) + ) as Record< + L, + { initialValue: string; testRunnerFactory: TestRunnerFactory } + >, } satisfies Props; mount(Editor, { target: element.parentElement!, diff --git a/apps/ppp/src/content/design-patterns/factory/editor.astro b/apps/ppp/src/content/design-patterns/factory/editor.astro index 19835e6..2f1a6b5 100644 --- a/apps/ppp/src/content/design-patterns/factory/editor.astro +++ b/apps/ppp/src/content/design-patterns/factory/editor.astro @@ -9,39 +9,36 @@ const { contentId } = Astro.props;
diff --git a/apps/ppp/src/content/design-patterns/factory/js/factory.ts b/apps/ppp/src/content/design-patterns/factory/js/factory.ts index 7e55e8a..3a3c6f5 100644 --- a/apps/ppp/src/content/design-patterns/factory/js/factory.ts +++ b/apps/ppp/src/content/design-patterns/factory/js/factory.ts @@ -13,15 +13,9 @@ interface TestingModule { export const factory: UniversalFactory< Input, Output, - UniversalFactoryData -> = ({ createLogger, JsTestRunner }) => { - class TestRunner extends JsTestRunner { - override async executeTest( - m: TestingModule, - input: Input - ): Promise { - return m.payment(input.paymentSystem, input.base, input.amount); - } - } - return async (_, { code, out }) => new TestRunner(createLogger(out), code); + UniversalFactoryData +> = ({ makeTestRunnerFactory }) => { + return makeTestRunnerFactory(async (m, input) => + m.payment(input.paymentSystem, input.base, input.amount) + ); }; diff --git a/apps/ppp/src/content/design-patterns/factory/php/factory.ts b/apps/ppp/src/content/design-patterns/factory/php/factory.ts index b51c23e..841e5a8 100644 --- a/apps/ppp/src/content/design-patterns/factory/php/factory.ts +++ b/apps/ppp/src/content/design-patterns/factory/php/factory.ts @@ -8,32 +8,25 @@ import type { Input, Output } from "../tests-data"; // Const enum import is allowed import type { PaymentSystemType } from "../reference"; -export const factory: UniversalFactory = ({ - FailSafePHP, - PHPTestRunner, - phpRuntimeFactory, -}) => { +export const factory: UniversalFactory< + Input, + Output, + UniversalFactoryData +> = ({ makeTestRunnerFactory }) => { const PHP_PAYMENT_SYSTEM_TYPES: Record = { - "paypal": "PaymentSystemType::PAYPAL", - "webmoney": "PaymentSystemType::WEBMONEY", + paypal: "PaymentSystemType::PAYPAL", + webmoney: "PaymentSystemType::WEBMONEY", "cat-bank": "PaymentSystemType::CAT_BANK", }; - class TestRunner extends PHPTestRunner { - protected override caseExecutionCode({ - paymentSystem, - base, - amount, - }: Input): string { - return `strval(payment(${PHP_PAYMENT_SYSTEM_TYPES[paymentSystem]}, ${base}, ${amount}))`; - } - protected transformResult(result: string): Output { + return makeTestRunnerFactory( + ({ paymentSystem, base, amount }: Input) => + `strval(payment(${PHP_PAYMENT_SYSTEM_TYPES[paymentSystem]}, ${base}, ${amount}))`, + (result: string) => { const r = parseInt(result, 10); if (isNaN(r)) { throw new Error(`Invalid result type: ${result}, expected number`); } return r; } - } - return async (_, { code, out }) => - new TestRunner(out, new FailSafePHP(phpRuntimeFactory), code); + ); }; diff --git a/apps/ppp/src/content/design-patterns/factory/python/factory.ts b/apps/ppp/src/content/design-patterns/factory/python/factory.ts index 25a5578..7f19f78 100644 --- a/apps/ppp/src/content/design-patterns/factory/python/factory.ts +++ b/apps/ppp/src/content/design-patterns/factory/python/factory.ts @@ -6,21 +6,13 @@ import type { UniversalFactoryData } from "@/lib/workers/python"; import type { Input, Output } from "../tests-data"; -export const factory: UniversalFactory = ({ - createLogger, - PyTestRunner, - pyRuntimeFactory, -}) => { - class TestRunner extends PyTestRunner { - protected override caseExecutionCode({ - paymentSystem, - base, - amount, - }: Input): string { - return `payment("${paymentSystem}", ${base}, ${amount})`; - } - } - - return async (ctx, { code, out }) => - new TestRunner(await pyRuntimeFactory(ctx, createLogger(out)), code); +export const factory: UniversalFactory< + Input, + Output, + UniversalFactoryData +> = ({ makeTestRunnerFactory }) => { + return makeTestRunnerFactory( + ({ paymentSystem, amount, base }) => + `payment("${paymentSystem}", ${base}, ${amount})` + ); }; diff --git a/apps/ppp/src/lib/workers/js.ts b/apps/ppp/src/lib/workers/js.ts index c2e7341..508c6e4 100644 --- a/apps/ppp/src/lib/workers/js.ts +++ b/apps/ppp/src/lib/workers/js.ts @@ -1,16 +1,32 @@ import { createLogger } from "libs/logger"; +import type { TestRunnerFactory } from "testing"; import { startTestRunnerActor } from "testing/actor"; import { JsTestRunner } from "testing-javascript"; -export interface UniversalFactoryData { +export interface UniversalFactoryData { JsTestRunner: typeof JsTestRunner; createLogger: typeof createLogger; + makeTestRunnerFactory: ( + invokeTestMethod: (m: M, input: I) => Promise + ) => TestRunnerFactory; } -startTestRunnerActor( - (universalFactory) => - universalFactory({ - JsTestRunner, - createLogger, - }) +startTestRunnerActor< + unknown, + unknown, + UniversalFactoryData +>((universalFactory) => + universalFactory({ + JsTestRunner, + createLogger, + makeTestRunnerFactory: (invokeTestMethod) => { + class TestRunner extends JsTestRunner { + override executeTest(m: unknown, input: unknown): Promise { + return invokeTestMethod(m, input); + } + } + return async (_, { code, out }) => + new TestRunner(createLogger(out), code); + }, + }) ); diff --git a/apps/ppp/src/lib/workers/php.ts b/apps/ppp/src/lib/workers/php.ts index e6ec587..d90fd82 100644 --- a/apps/ppp/src/lib/workers/php.ts +++ b/apps/ppp/src/lib/workers/php.ts @@ -1,17 +1,34 @@ +import type { TestRunnerFactory } from "testing"; import { FailSafePHP, PHPTestRunner, phpRuntimeFactory } from "testing-php"; import { startTestRunnerActor } from "testing/actor"; -export interface UniversalFactoryData { +export interface UniversalFactoryData { PHPTestRunner: typeof PHPTestRunner; FailSafePHP: typeof FailSafePHP; phpRuntimeFactory: typeof phpRuntimeFactory; + makeTestRunnerFactory: ( + generateCaseExecutionCode: (input: I) => string, + transformResult: (result: string) => O + ) => TestRunnerFactory; } -startTestRunnerActor( +startTestRunnerActor>( (universalFactory) => universalFactory({ PHPTestRunner, FailSafePHP, phpRuntimeFactory, + makeTestRunnerFactory: (generateCaseExecutionCode, transformResult) => { + class TestRunner extends PHPTestRunner { + protected override caseExecutionCode(data: unknown): string { + return generateCaseExecutionCode(data); + } + protected transformResult(result: string) { + return transformResult(result); + } + } + return async (_, { code, out }) => + new TestRunner(out, new FailSafePHP(phpRuntimeFactory), code); + }, }) ); diff --git a/apps/ppp/src/lib/workers/python.ts b/apps/ppp/src/lib/workers/python.ts index 00247d0..483783b 100644 --- a/apps/ppp/src/lib/workers/python.ts +++ b/apps/ppp/src/lib/workers/python.ts @@ -1,5 +1,6 @@ import type { Context } from "libs/context"; import { createLogger, type Logger } from "libs/logger"; +import type { TestRunnerFactory } from "testing"; import { PyTestRunner, pyRuntimeFactory } from "testing-python"; import { startTestRunnerActor } from "testing/actor"; @@ -8,16 +9,19 @@ import wasmUrl from "testing-python/pyodide.wasm"; // @ts-ignore import stdlibUrl from "testing-python/python-stdlib.zip"; -export interface UniversalFactoryData { +export interface UniversalFactoryData { createLogger: typeof createLogger; PyTestRunner: typeof PyTestRunner; pyRuntimeFactory: ( ctx: Context, log: Logger ) => ReturnType; + makeTestRunnerFactory: ( + generateCaseExecutionCode: (input: I) => string + ) => TestRunnerFactory; } -startTestRunnerActor( +startTestRunnerActor>( (universalFactory) => universalFactory({ PyTestRunner, @@ -30,5 +34,23 @@ startTestRunnerActor( WebAssembly.instantiateStreaming(fetch(wasmUrl), imports), stdlibUrl ), + makeTestRunnerFactory: (generateCaseExecutionCode) => { + class TestRunner extends PyTestRunner { + protected override caseExecutionCode(data: unknown): string { + return generateCaseExecutionCode(data); + } + } + return async (ctx, { code, out }) => + new TestRunner( + await pyRuntimeFactory( + ctx, + createLogger(out), + (imports) => + WebAssembly.instantiateStreaming(fetch(wasmUrl), imports), + stdlibUrl + ), + code + ); + }, }) ); diff --git a/apps/ppp/src/shared/index.ts b/apps/ppp/src/shared/index.ts new file mode 100644 index 0000000..fe7dca3 --- /dev/null +++ b/apps/ppp/src/shared/index.ts @@ -0,0 +1 @@ +export * from './shared' diff --git a/apps/ppp/src/lib/languages.ts b/apps/ppp/src/shared/languages.ts similarity index 100% rename from apps/ppp/src/lib/languages.ts rename to apps/ppp/src/shared/languages.ts diff --git a/apps/ppp/src/shared.ts b/apps/ppp/src/shared/shared.ts similarity index 100% rename from apps/ppp/src/shared.ts rename to apps/ppp/src/shared/shared.ts From 871aad8f35f3197c9bbcc32f8607aa12bed325ba Mon Sep 17 00:00:00 2001 From: Krasilnikov Roman Date: Wed, 26 Jun 2024 15:15:39 +0300 Subject: [PATCH 8/9] Export runtime versions --- apps/ppp/src/components/editor/editor.svelte | 2 +- apps/ppp/src/shared/languages.ts | 13 +++++++++---- packages/testing-go/package.json | 1 + packages/testing-go/vite.config.js | 7 +++++-- packages/testing-php/package.json | 3 ++- packages/testing-php/vite.config.js | 7 +++++-- packages/testing-python/package.json | 1 + packages/testing-python/vite.config.js | 7 +++++-- packages/testing-typescript/package.json | 3 ++- 9 files changed, 31 insertions(+), 13 deletions(-) diff --git a/apps/ppp/src/components/editor/editor.svelte b/apps/ppp/src/components/editor/editor.svelte index c95ea62..c40e871 100644 --- a/apps/ppp/src/components/editor/editor.svelte +++ b/apps/ppp/src/components/editor/editor.svelte @@ -18,7 +18,7 @@ import { LANGUAGE_TITLE, Language, - } from '@/lib/languages' + } from '@/shared/languages' import Select from '@/components/select.svelte'; import { MONACO_LANGUAGE_ID } from "@/adapters/monaco"; import { createSyncStorage } from "@/adapters/storage"; diff --git a/apps/ppp/src/shared/languages.ts b/apps/ppp/src/shared/languages.ts index db32a7e..a35d4f5 100644 --- a/apps/ppp/src/shared/languages.ts +++ b/apps/ppp/src/shared/languages.ts @@ -1,3 +1,8 @@ +import { version as goVersion } from "testing-go/version"; +import { version as phpVersion } from "testing-php/version"; +import { version as pythonVersion } from "testing-python/version"; +import { version as typescriptVersion } from "testing-typescript/version"; + export enum Language { PHP = "php", Python = "python", @@ -7,9 +12,9 @@ export enum Language { } export const LANGUAGE_TITLE: Record = { - [Language.PHP]: `PHP 8.3`, - [Language.TypeScript]: `TypeScript 5.4.5`, - [Language.Python]: `Python 3.12.1`, + [Language.PHP]: `PHP ${phpVersion}`, + [Language.TypeScript]: `TypeScript ${typescriptVersion}`, + [Language.Python]: `Python ${pythonVersion}`, [Language.JavaScript]: "JavaScript", - [Language.Go]: "Go 1.22", + [Language.Go]: `Go ${goVersion}`, }; diff --git a/packages/testing-go/package.json b/packages/testing-go/package.json index cbe50ef..470926f 100644 --- a/packages/testing-go/package.json +++ b/packages/testing-go/package.json @@ -20,6 +20,7 @@ }, "exports": { ".": "./dist/index.js", + "./version": "./dist/version.js", "./compiler.wasm": "./dist/compiler.wasm" } } \ No newline at end of file diff --git a/packages/testing-go/vite.config.js b/packages/testing-go/vite.config.js index 85e57cf..8ac4ed8 100644 --- a/packages/testing-go/vite.config.js +++ b/packages/testing-go/vite.config.js @@ -6,11 +6,14 @@ export default defineConfig({ build: { lib: { // Could also be a dictionary or array of multiple entry points - entry: resolve(__dirname, "src/index.ts"), + entry: { + index: resolve(__dirname, "src/index.ts"), + version: resolve(__dirname, "src/version.ts"), + }, formats: ["es"], // name: "MyLib", // the proper extensions will be added - fileName: "index", + // fileName: "index", }, rollupOptions: { // make sure to externalize deps that shouldn't be bundled diff --git a/packages/testing-php/package.json b/packages/testing-php/package.json index 73dbafc..e19cf4d 100644 --- a/packages/testing-php/package.json +++ b/packages/testing-php/package.json @@ -17,6 +17,7 @@ "vite-plugin-dts": "^3.9.1" }, "exports": { - ".": "./dist/index.js" + ".": "./dist/index.js", + "./version": "./dist/version.js" } } \ No newline at end of file diff --git a/packages/testing-php/vite.config.js b/packages/testing-php/vite.config.js index 536cb5c..1c6e193 100644 --- a/packages/testing-php/vite.config.js +++ b/packages/testing-php/vite.config.js @@ -6,11 +6,14 @@ export default defineConfig({ build: { lib: { // Could also be a dictionary or array of multiple entry points - entry: resolve(__dirname, "src/index.ts"), + entry:{ + index: resolve(__dirname, "src/index.ts"), + version: resolve(__dirname, "src/version.ts"), + }, formats: ["es"], // name: "MyLib", // the proper extensions will be added - fileName: "index", + // fileName: "index", }, rollupOptions: { // make sure to externalize deps that shouldn't be bundled diff --git a/packages/testing-python/package.json b/packages/testing-python/package.json index cbf86ee..e66f15e 100644 --- a/packages/testing-python/package.json +++ b/packages/testing-python/package.json @@ -19,6 +19,7 @@ }, "exports": { ".": "./dist/index.js", + "./version": "./dist/version.js", "./pyodide.wasm": "./dist/pyodide/pyodide.asm.wasm", "./python-stdlib.zip": "./dist/pyodide/python_stdlib.zip" } diff --git a/packages/testing-python/vite.config.js b/packages/testing-python/vite.config.js index 51bf813..5bcc111 100644 --- a/packages/testing-python/vite.config.js +++ b/packages/testing-python/vite.config.js @@ -17,11 +17,14 @@ export default defineConfig({ build: { lib: { // Could also be a dictionary or array of multiple entry points - entry: resolve(__dirname, "src/index.ts"), + entry: { + index: resolve(__dirname, "src/index.ts"), + version: resolve(__dirname, "src/version.ts"), + }, formats: ["es"], // name: "MyLib", // the proper extensions will be added - fileName: "index", + // fileName: "index", }, rollupOptions: { // make sure to externalize deps that shouldn't be bundled diff --git a/packages/testing-typescript/package.json b/packages/testing-typescript/package.json index 3f6d8f9..b2ac38d 100644 --- a/packages/testing-typescript/package.json +++ b/packages/testing-typescript/package.json @@ -10,7 +10,8 @@ "author": "Krasilnikov Roman", "license": "ISC", "exports": { - ".": "./dist/index.js" + ".": "./dist/index.js", + "./version": "./dist/version.js" }, "dependencies": { "libs": "workspace:*", From 2a41ccb772816ac1b18ec61268d7a9f6490879ec Mon Sep 17 00:00:00 2001 From: Krasilnikov Roman Date: Wed, 26 Jun 2024 15:20:25 +0300 Subject: [PATCH 9/9] Fix go output --- apps/ppp/src/lib/workers/go.ts | 6 +----- packages/libs/src/logger.ts | 20 +++++++++---------- packages/testing-go/src/go-runtime-factory.ts | 10 +++++----- packages/testing-go/src/model.ts | 4 ++-- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/apps/ppp/src/lib/workers/go.ts b/apps/ppp/src/lib/workers/go.ts index 50efa55..0476e71 100644 --- a/apps/ppp/src/lib/workers/go.ts +++ b/apps/ppp/src/lib/workers/go.ts @@ -40,11 +40,7 @@ startTestRunnerActor< } return async (ctx, { code, out }) => new TestRunner( - await makeGoRuntimeFactory(await compilerFactory)( - ctx, - createLogger(out), - code - ) + await makeGoRuntimeFactory(await compilerFactory)(ctx, out, code) ); }, }) diff --git a/packages/libs/src/logger.ts b/packages/libs/src/logger.ts index 9cc87a5..f55e160 100644 --- a/packages/libs/src/logger.ts +++ b/packages/libs/src/logger.ts @@ -13,27 +13,27 @@ export interface Logger { } // ANSI color codes for log levels -const colors = { - debug: "\x1b[32m", // Green - info: "\x1b[34m", // Blue - warn: "\x1b[33m", // Yellow - error: "\x1b[31m", // Red - reset: "\x1b[0m", // Reset to default color +export const COLOR = { + DEBUG: "\x1b[32m", // Green + INFO: "\x1b[34m", // Blue + WARN: "\x1b[33m", // Yellow + ERROR: "\x1b[31m", // Red + RESET: "\x1b[0m", // Reset to default color }; export function createLogger(writer: Writer): Logger { return { debug(text) { - writer.writeln(`${colors.debug}[DEBUG]${colors.reset} ${text}`); + writer.writeln(`${COLOR.DEBUG}[DEBUG]${COLOR.RESET} ${text}`); }, info(text) { - writer.writeln(`${colors.info}[INFO]${colors.reset} ${text}`); + writer.writeln(`${COLOR.INFO}[INFO]${COLOR.RESET} ${text}`); }, warn(text) { - writer.writeln(`${colors.warn}[WARN]${colors.reset} ${text}`); + writer.writeln(`${COLOR.WARN}[WARN]${COLOR.RESET} ${text}`); }, error(text) { - writer.writeln(`${colors.error}[ERROR]${colors.reset} ${text}`); + writer.writeln(`${COLOR.ERROR}[ERROR]${COLOR.RESET} ${text}`); }, }; } diff --git a/packages/testing-go/src/go-runtime-factory.ts b/packages/testing-go/src/go-runtime-factory.ts index 3818b76..82d87e1 100644 --- a/packages/testing-go/src/go-runtime-factory.ts +++ b/packages/testing-go/src/go-runtime-factory.ts @@ -1,4 +1,4 @@ -import { redirect } from "libs/logger"; +import { createLogger, redirect, COLOR } from "libs/logger"; import { isErr } from "libs/result"; import { LogLevel, type CompilerFactory, type GoRuntimeFactory } from "./model"; @@ -6,21 +6,21 @@ import { LogLevel, type CompilerFactory, type GoRuntimeFactory } from "./model"; export function makeGoRuntimeFactory( makeCompiler: CompilerFactory ): GoRuntimeFactory { - return async (ctx, log, code) => { + return async (ctx, out, code) => { const compiler = makeCompiler({ logger: { level: LogLevel.Info, - console: redirect(globalThis.console, log), + console: redirect(globalThis.console, createLogger(out)), }, stdout: { write(text) { - log.debug(text); + out.write(text); return null; }, }, stderr: { write(text) { - log.error(text); + out.write(`${COLOR.ERROR}${text}${COLOR.RESET}`); return null; }, }, diff --git a/packages/testing-go/src/model.ts b/packages/testing-go/src/model.ts index 078aa02..9451b8c 100644 --- a/packages/testing-go/src/model.ts +++ b/packages/testing-go/src/model.ts @@ -1,5 +1,5 @@ import type { Context } from "libs/context"; -import type { Logger } from "libs/logger"; +import type { Logger, Writer } from "libs/logger"; import type { Result } from "libs/result"; export enum LogLevel { @@ -43,7 +43,7 @@ export type CompilerFactory = ( export type GoRuntimeFactory = ( ctx: Context, - log: Logger, + out: Writer, code: string ) => Promise>;