@empiricalrun/test-gen 0.1.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @empiricalrun/test-gen
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - f993c4c: feat: add support for creating test case using google sheet
8
+
9
+ ### Patch Changes
10
+
11
+ - f993c4c: fix: decoding error of new line in evnironment variable
12
+
13
+ ## 0.2.0
14
+
15
+ ### Minor Changes
16
+
17
+ - 5333d3b: feat: add support for creating test case using google sheet
18
+
3
19
  ## 0.1.3
4
20
 
5
21
  ### Patch Changes
package/dist/bin/index.js CHANGED
@@ -7,13 +7,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
7
7
  const fs_extra_1 = __importDefault(require("fs-extra"));
8
8
  const openai_1 = __importDefault(require("openai"));
9
9
  const path_1 = __importDefault(require("path"));
10
- // const { scenarios } = require("./scenarios");
11
10
  const typescript_1 = __importDefault(require("typescript"));
12
11
  const prettier_1 = __importDefault(require("prettier"));
13
12
  const eslint_1 = require("eslint");
14
13
  const dotenv_1 = __importDefault(require("dotenv"));
15
- const yaml_1 = require("yaml");
16
14
  const logger_1 = require("./logger");
15
+ const scenarios_1 = require("./scenarios");
17
16
  dotenv_1.default.config({
18
17
  path: [".env.local", ".env"],
19
18
  });
@@ -237,18 +236,14 @@ async function generateTest(scenarios, file) {
237
236
  logger.error("Please provide path to scenarios using command:", "npx @empiricalrun/test-gen <SCENARIOS_FILE_PATH>");
238
237
  process.exit(1);
239
238
  }
240
- else {
241
- const scenariosPath = process.argv[2];
242
- const file = fs_extra_1.default.readFileSync(path_1.default.resolve(process.cwd(), scenariosPath), 'utf8');
243
- const fileName = scenariosPath.split('/').pop()?.split(".")[0];
244
- const config = (0, yaml_1.parse)(file);
245
- const scenarios = config.scenarios;
246
- const fileDir = config.dir || "./tests";
247
- const filePath = `${fileDir}/${fileName}.spec.ts`;
248
- if (!fs_extra_1.default.existsSync(filePath)) {
249
- logger.log(`Creating a new spec file: ${filePath}`);
250
- fs_extra_1.default.createFileSync(filePath);
239
+ const scenariosPath = process.argv[2];
240
+ const testGenConfigs = await (0, scenarios_1.generateScenarios)(scenariosPath);
241
+ for (const testGenConfig of testGenConfigs) {
242
+ const specPath = testGenConfig.specPath;
243
+ if (!fs_extra_1.default.existsSync(specPath)) {
244
+ logger.log(`Creating a new spec file: ${specPath}`);
245
+ fs_extra_1.default.createFileSync(specPath);
251
246
  }
252
- await generateTest(scenarios, filePath);
247
+ await generateTest(testGenConfig.scenarios, specPath);
253
248
  }
254
249
  })();
@@ -0,0 +1,7 @@
1
+ import { Scenario } from '../types';
2
+ declare function generateScenarios(scenariosPath: string): Promise<{
3
+ specPath: string;
4
+ scenarios: Scenario[];
5
+ }[]>;
6
+ export { generateScenarios };
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/bin/scenarios/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAsEpC,iBAAe,iBAAiB,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,QAAQ,EAAE,CAAA;CAAC,EAAE,CAAC,CAM7G;AAID,OAAO,EACH,iBAAiB,EACpB,CAAA"}
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.generateScenarios = void 0;
7
+ const google_auth_library_1 = require("google-auth-library");
8
+ const slugify_1 = __importDefault(require("slugify"));
9
+ const fs_extra_1 = __importDefault(require("fs-extra"));
10
+ const path_1 = __importDefault(require("path"));
11
+ const yaml_1 = require("yaml");
12
+ /**
13
+ * Method to update / add scenarios to the repo.
14
+ * @param path
15
+ * @returns updated paths of scenarios
16
+ */
17
+ async function generateScenariosUsingGsheet(path) {
18
+ const { GoogleSpreadsheet } = await import("google-spreadsheet");
19
+ const url = new URL(path);
20
+ const docId = url.pathname.split("/")[3];
21
+ const searchParams = new URLSearchParams(url.hash.split("#")[1]);
22
+ const sheetId = Number(searchParams.get("gid")) || 0;
23
+ // TODO: use oauth 2
24
+ const serviceAccountAuth = new google_auth_library_1.JWT({
25
+ email: process.env.GOOGLE_SERVICE_EMAIL,
26
+ key: Buffer.from(process.env.GOOGLE_SERVICE_EMAIL_PRIVATE_KEY, "base64").toString(),
27
+ scopes: ['https://www.googleapis.com/auth/spreadsheets'],
28
+ });
29
+ const doc = new GoogleSpreadsheet(docId, serviceAccountAuth);
30
+ await doc.loadInfo();
31
+ const sheet = doc.sheetsById[sheetId];
32
+ const rows = await sheet.getRows();
33
+ const map = new Map();
34
+ rows.forEach(r => {
35
+ // TODO: fix for case insensitive
36
+ const category = r.get("Category");
37
+ const name = r.get("Scenario");
38
+ const steps = (r.get("Steps").split("\n")).map((s) => s.trim()).filter((s) => !!s.length);
39
+ const assert = r.get("Assert");
40
+ const specPath = category ? `./tests/${category}.spec.ts` : `./tests/${(0, slugify_1.default)(name)}.spec.ts`;
41
+ const scenario = {
42
+ steps,
43
+ name,
44
+ assert
45
+ };
46
+ if (!map.get(specPath)) {
47
+ map.set(specPath, [scenario]);
48
+ }
49
+ else {
50
+ const scenarios = map.get(specPath);
51
+ scenarios.push(scenario);
52
+ map.set(specPath, scenarios);
53
+ }
54
+ });
55
+ const results = [];
56
+ for (const [specPath, scenarios] of map.entries()) {
57
+ results.push({
58
+ specPath,
59
+ scenarios
60
+ });
61
+ }
62
+ return results;
63
+ }
64
+ async function generateScenariosUsingYAML(scenariosPath) {
65
+ const file = await fs_extra_1.default.readFile(path_1.default.resolve(process.cwd(), scenariosPath), 'utf8');
66
+ const fileName = scenariosPath.split('/').pop()?.split(".")[0];
67
+ const config = (0, yaml_1.parse)(file);
68
+ const fileDir = config.dir || "./tests";
69
+ config.specPath = `${fileDir}/${fileName}.spec.ts`;
70
+ return [config];
71
+ }
72
+ async function generateScenarios(scenariosPath) {
73
+ if (scenariosPath.startsWith("https://docs.google.com/spreadsheets")) {
74
+ return await generateScenariosUsingGsheet(scenariosPath);
75
+ }
76
+ else {
77
+ return await generateScenariosUsingYAML(scenariosPath);
78
+ }
79
+ }
80
+ exports.generateScenarios = generateScenarios;
@@ -0,0 +1,15 @@
1
+ export type FileContent = {
2
+ filePath: string;
3
+ content: string;
4
+ };
5
+ export type TestGenConfig = {
6
+ dir?: string;
7
+ specPath?: string;
8
+ scenarios: Scenario[];
9
+ };
10
+ export type Scenario = {
11
+ name: string;
12
+ steps: string[];
13
+ assert: string;
14
+ };
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/bin/types/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,GAAG;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAA;AAEH,MAAM,MAAM,aAAa,GAAG;IACxB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,QAAQ,EAAE,CAAC;CACzB,CAAA;AAED,MAAM,MAAM,QAAQ,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;CAClB,CAAA"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@empiricalrun/test-gen",
3
- "version": "0.1.3",
3
+ "version": "0.3.0",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org/",
6
6
  "access": "public"
@@ -18,9 +18,12 @@
18
18
  "dotenv": "^16.4.5",
19
19
  "eslint": "^8.57.0",
20
20
  "fs-extra": "^11.2.0",
21
+ "google-auth-library": "^9.10.0",
22
+ "google-spreadsheet": "^4.1.2",
21
23
  "openai": "^4.47.2",
22
24
  "picocolors": "^1.0.1",
23
25
  "prettier": "^3.2.5",
26
+ "slugify": "^1.6.6",
24
27
  "typescript": "^5.3.3",
25
28
  "yaml": "^2.4.2"
26
29
  },