diff --git a/__output__/schema-data/public-methods/bundle.js b/__output__/schema-data/public-methods.bundle.js similarity index 92% rename from __output__/schema-data/public-methods/bundle.js rename to __output__/schema-data/public-methods.bundle.js index 40b5d5e..cb323fe 100644 --- a/__output__/schema-data/public-methods/bundle.js +++ b/__output__/schema-data/public-methods.bundle.js @@ -30,4 +30,4 @@ var public_methods_default = MyContract; export { public_methods_default as default }; -//# sourceMappingURL=bundle.js.map +//# sourceMappingURL=public-methods.bundle.js.map diff --git a/__output__/schema-data/public-methods/bundle.js.map b/__output__/schema-data/public-methods.bundle.js.map similarity index 89% rename from __output__/schema-data/public-methods/bundle.js.map rename to __output__/schema-data/public-methods.bundle.js.map index d4e318a..6f48365 100644 --- a/__output__/schema-data/public-methods/bundle.js.map +++ b/__output__/schema-data/public-methods.bundle.js.map @@ -1,6 +1,6 @@ { "version": 3, - "sources": ["../../../__fixtures__/schema-data/public-methods/contract.ts", "../../../__fixtures__/schema-data/public-methods/index.ts"], + "sources": ["../../__fixtures__/schema-data/public-methods/contract.ts", "../../__fixtures__/schema-data/public-methods/index.ts"], "sourcesContent": ["import { State } from './state';\n\nexport class MyContract {\n private state: State;\n\n constructor() {\n this.state = {\n count: 0,\n startCoin: {\n denom: 'uatom',\n amount: '1000'\n },\n tokens: []\n };\n }\n\n public increment() {\n this.state.count++;\n }\n\n private reset() {\n this.state.count = 0;\n }\n\n public addToken(denom: string, amount: string) {\n this.state.tokens.push({ denom, amount });\n }\n\n public removeToken(index: number) {\n this.state.tokens.splice(index, 1);\n }\n}", "import { MyContract } from \"./contract\";\nexport type { State } from \"./state\";\n\nexport default MyContract;\n"], "mappings": ";AAEO,IAAM,aAAN,MAAiB;AAAA,EACd;AAAA,EAER,cAAc;AACZ,SAAK,QAAQ;AAAA,MACX,OAAO;AAAA,MACP,WAAW;AAAA,QACT,OAAO;AAAA,QACP,QAAQ;AAAA,MACV;AAAA,MACA,QAAQ,CAAC;AAAA,IACX;AAAA,EACF;AAAA,EAEO,YAAY;AACjB,SAAK,MAAM;AAAA,EACb;AAAA,EAEQ,QAAQ;AACd,SAAK,MAAM,QAAQ;AAAA,EACrB;AAAA,EAEO,SAAS,OAAe,QAAgB;AAC7C,SAAK,MAAM,OAAO,KAAK,EAAE,OAAO,OAAO,CAAC;AAAA,EAC1C;AAAA,EAEO,YAAY,OAAe;AAChC,SAAK,MAAM,OAAO,OAAO,OAAO,CAAC;AAAA,EACnC;AACF;;;AC5BA,IAAO,yBAAQ;", "names": [] diff --git a/__output__/schema-data/public-methods/bundle.js.schema.json b/__output__/schema-data/public-methods.schema.json similarity index 100% rename from __output__/schema-data/public-methods/bundle.js.schema.json rename to __output__/schema-data/public-methods.schema.json diff --git a/__output__/schema-data/state-export.bundle.js b/__output__/schema-data/state-export.bundle.js new file mode 100644 index 0000000..828ebcc --- /dev/null +++ b/__output__/schema-data/state-export.bundle.js @@ -0,0 +1 @@ +//# sourceMappingURL=state-export.bundle.js.map diff --git a/__output__/schema-data/state-export/bundle.js.map b/__output__/schema-data/state-export.bundle.js.map similarity index 100% rename from __output__/schema-data/state-export/bundle.js.map rename to __output__/schema-data/state-export.bundle.js.map diff --git a/__output__/schema-data/state-export/bundle.js.schema.json b/__output__/schema-data/state-export.schema.json similarity index 100% rename from __output__/schema-data/state-export/bundle.js.schema.json rename to __output__/schema-data/state-export.schema.json diff --git a/__output__/schema-data/state-export/bundle.js b/__output__/schema-data/state-export/bundle.js deleted file mode 100644 index 9f95631..0000000 --- a/__output__/schema-data/state-export/bundle.js +++ /dev/null @@ -1 +0,0 @@ -//# sourceMappingURL=bundle.js.map diff --git a/packages/build/__tests__/__snapshots__/schemaExtractor.test.ts.snap b/packages/build/__tests__/__snapshots__/schemaExtractor.test.ts.snap new file mode 100644 index 0000000..dcc7291 --- /dev/null +++ b/packages/build/__tests__/__snapshots__/schemaExtractor.test.ts.snap @@ -0,0 +1,121 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`schemaExtractorPlugin should extract a basic contract with public and private methods 1`] = ` +{ + "methods": [], + "state": { + "properties": { + "count": { + "type": "number", + }, + "startCoin": { + "properties": { + "amount": { + "type": "string", + }, + "denom": { + "type": "string", + }, + }, + "type": "object", + }, + "tokens": { + "items": { + "properties": { + "amount": { + "type": "string", + }, + "denom": { + "type": "string", + }, + }, + "type": "object", + }, + "type": "array", + }, + }, + "type": "object", + }, +} +`; + +exports[`schemaExtractorPlugin should extract methods from classes public methods 1`] = ` +{ + "methods": [ + { + "functionName": "increment", + "parameters": [], + "returnType": { + "type": "any", + }, + }, + { + "functionName": "addToken", + "parameters": [ + { + "name": "denom", + "type": { + "type": "string", + }, + }, + { + "name": "amount", + "type": { + "type": "string", + }, + }, + ], + "returnType": { + "type": "any", + }, + }, + { + "functionName": "removeToken", + "parameters": [ + { + "name": "index", + "type": { + "type": "number", + }, + }, + ], + "returnType": { + "type": "any", + }, + }, + ], + "state": { + "properties": { + "count": { + "type": "number", + }, + "startCoin": { + "properties": { + "amount": { + "type": "string", + }, + "denom": { + "type": "string", + }, + }, + "type": "object", + }, + "tokens": { + "items": { + "properties": { + "amount": { + "type": "string", + }, + "denom": { + "type": "string", + }, + }, + "type": "object", + }, + "type": "array", + }, + }, + "type": "object", + }, +} +`; diff --git a/packages/build/__tests__/schemaExtractor.test.ts b/packages/build/__tests__/schemaExtractor.test.ts new file mode 100644 index 0000000..e05ac73 --- /dev/null +++ b/packages/build/__tests__/schemaExtractor.test.ts @@ -0,0 +1,58 @@ +import fs from 'fs/promises'; +import { join, resolve } from 'path'; +import { HyperwebBuild, HyperwebBuildOptions, schemaExtractorPlugin } from '../src'; + +const outputDir = resolve(join(__dirname, '/../../../__output__/schema-data')); + +const runTest = async (fixtureName: string) => { + const fixtureDir = resolve(join(__dirname, `/../../../__fixtures__/schema-data/${fixtureName}`)); + const schemaOutputPath = join(outputDir, `${fixtureName}.schema.json`); + + const buildOptions: Partial = { + entryPoints: [join(fixtureDir, 'index.ts')], + outfile: join(outputDir, `${fixtureName}.bundle.js`), + customPlugins: [ + schemaExtractorPlugin({ + outputPath: schemaOutputPath, + baseDir: fixtureDir, + include: [/\.ts$/], + exclude: [/node_modules/, /\.test\.ts$/], + }), + ], + }; + + await HyperwebBuild.build(buildOptions); + const schemaContent = await fs.readFile(schemaOutputPath, 'utf-8'); + return JSON.parse(schemaContent); +}; + +describe('schemaExtractorPlugin', () => { + it('should extract a basic contract with public and private methods', async () => { + const schemaData = await runTest('state-export'); + + expect(schemaData).toHaveProperty('state'); + expect(schemaData.state).toHaveProperty('type', 'object'); + expect(schemaData.state).toHaveProperty('properties'); + + expect(schemaData).toHaveProperty('methods'); + expect(schemaData.methods).toEqual([]); + + expect(schemaData).toMatchSnapshot(); + }); + + it('should extract methods from classes public methods', async () => { + const schemaData = await runTest('public-methods'); + + expect(schemaData).toHaveProperty('state'); + expect(schemaData.state).toHaveProperty('type', 'object'); + expect(schemaData.state).toHaveProperty('properties'); + + const methodNames = schemaData.methods.map((method: any) => method.functionName); + expect(methodNames).toContain('addToken'); + expect(methodNames).toContain('increment'); + expect(methodNames).toContain('removeToken'); + expect(methodNames).not.toContain('reset'); + + expect(schemaData).toMatchSnapshot(); + }); +}); diff --git a/packages/build/__tests__/schemaMethods.test.ts b/packages/build/__tests__/schemaMethods.test.ts deleted file mode 100644 index ab3e4f5..0000000 --- a/packages/build/__tests__/schemaMethods.test.ts +++ /dev/null @@ -1,57 +0,0 @@ -import fs from 'fs/promises'; -import { join, resolve } from 'path'; - -import { HyperwebBuild, HyperwebBuildOptions, schemaExtractorPlugin } from '../src'; - -const fixtureDir = resolve(join(__dirname, '/../../../__fixtures__/', 'schema-data', 'public-methods')); -const outputDir = resolve(join(__dirname, '/../../../__output__/', 'schema-data', 'public-methods')); - -describe('HyperwebBuild Schema Methods', () => { - it('builds the fixture project successfully for public-methods', async () => { - const outfile = join(outputDir, 'bundle.js'); - const schemaOutputPath = `${outfile}.schema.json`; - - const options: Partial = { - entryPoints: [join(fixtureDir, 'index.ts')], - outfile, - customPlugins: [ - schemaExtractorPlugin({ - outputPath: schemaOutputPath, - baseDir: fixtureDir, - include: [/\.ts$/], // Only process TypeScript files - exclude: [/node_modules/, /\.test\.ts$/], // Skip node_modules and test files - }), - ], - }; - - await HyperwebBuild.build(options); - - // Check if the output file exists - const outfileExists = await fs.access(outfile) - .then(() => true) - .catch(() => false); - - expect(outfileExists).toBe(true); - - // Check if schema file exists - const schemafileExists = await fs.access(schemaOutputPath) - .then(() => true) - .catch(() => false); - - expect(schemafileExists).toBe(true); - - // Optionally, read the schema file and verify the contents - const schemaContent = await fs.readFile(schemaOutputPath, 'utf-8'); - const schemaData = JSON.parse(schemaContent); - - // Perform checks on schema structure (example: check if 'state' exists and has properties) - expect(schemaData).toHaveProperty('state'); - expect(schemaData.state).toHaveProperty('type', 'object'); - expect(schemaData.state).toHaveProperty('properties'); - - // Perform checks on schema structure (example: check if 'methods' exists and has properties) - expect(schemaData).toHaveProperty('methods'); - - expect(schemaData).toMatchSnapshot(); - }); -}); diff --git a/packages/build/__tests__/schemaState.test.ts b/packages/build/__tests__/schemaState.test.ts deleted file mode 100644 index f96f1ba..0000000 --- a/packages/build/__tests__/schemaState.test.ts +++ /dev/null @@ -1,54 +0,0 @@ -import fs from 'fs/promises'; -import { join, resolve } from 'path'; - -import { HyperwebBuild, HyperwebBuildOptions, schemaExtractorPlugin } from '../src'; - -const fixtureDir = resolve(join(__dirname, '/../../../__fixtures__/', 'schema-data', 'state-export')); -const outputDir = resolve(join(__dirname, '/../../../__output__/', 'schema-data', 'state-export')); - -describe('HyperwebBuild Schema State', () => { - it('builds the fixture project successfully', async () => { - const outfile = join(outputDir, 'bundle.js'); - const schemaOutputPath = `${outfile}.schema.json`; - - const options: Partial = { - entryPoints: [join(fixtureDir, 'index.ts')], - outfile, - customPlugins: [ - schemaExtractorPlugin({ - outputPath: schemaOutputPath, - baseDir: fixtureDir, - include: [/\.ts$/], // Only process TypeScript files - exclude: [/node_modules/, /\.test\.ts$/], // Skip node_modules and test files - }), - ], - }; - - await HyperwebBuild.build(options); - - // Check if the output file exists - const outfileExists = await fs.access(outfile) - .then(() => true) - .catch(() => false); - - expect(outfileExists).toBe(true); - - // Check if schema file exists - const schemafileExists = await fs.access(schemaOutputPath) - .then(() => true) - .catch(() => false); - - expect(schemafileExists).toBe(true); - - // Optionally, read the schema file and verify the contents - const schemaContent = await fs.readFile(schemaOutputPath, 'utf-8'); - const schemaData = JSON.parse(schemaContent); - - // Perform checks on schema structure (example: check if 'state' exists and has properties) - expect(schemaData).toHaveProperty('state'); - expect(schemaData.state).toHaveProperty('type', 'object'); - expect(schemaData.state).toHaveProperty('properties'); - - expect(schemaData).toMatchSnapshot(); - }); -});