@zohodesk/testinglibrary 0.0.48-n20-experimental → 0.0.50-n20-experimental

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.
@@ -28,7 +28,7 @@ export async function generateAndCacheTestData(executionContext, type, identifie
28
28
 
29
29
  const { response, generators } = await dataGenerator.generate(testInfo, actorInfo, type, identifier, scenarioName, dataTable ? dataTable.hashes() : []);
30
30
  if (cacheLayer._trackForCleanup) {
31
- cacheLayer._trackForCleanup(entityName, response.data, generators, actorInfo);
31
+ cacheLayer._trackForCleanup(entityName, response.data, generators, actorInfo, response.logs, identifier);
32
32
  } else {
33
33
  cacheLayer.set(entityName, response.data);
34
34
  }
@@ -10,17 +10,22 @@ var _fs = _interopRequireDefault(require("fs"));
10
10
  var _logger = require("../../../utils/logger");
11
11
  var _DataGeneratorHelper = require("../../dataGenerator/DataGeneratorHelper");
12
12
  var _readConfigFile = require("../readConfigFile");
13
- var _configConstants = _interopRequireDefault(require("../constants/configConstants"));
14
- var _ConfigurationHelper = require("../configuration/ConfigurationHelper");
15
13
  var _jsonpath = _interopRequireDefault(require("jsonpath"));
16
14
  let cleanupRegistry = null;
15
+ function getModulesRoot() {
16
+ const configConstants = require('../constants/configConstants');
17
+ const {
18
+ getRunStage
19
+ } = require('../configuration/ConfigurationHelper');
20
+ const stage = getRunStage();
21
+ return _path.default.join(process.cwd(), configConstants.TEST_SLICE_FOLDER, stage, 'modules');
22
+ }
17
23
  function buildCleanupRegistry() {
18
- const stage = (0, _ConfigurationHelper.getRunStage)();
19
- const modulesRoot = _path.default.join(process.cwd(), _configConstants.default.TEST_SLICE_FOLDER, stage, 'modules');
24
+ const modulesRoot = getModulesRoot();
20
25
  const registry = {};
21
26
  if (!_fs.default.existsSync(modulesRoot)) return registry;
22
27
  scanDir(modulesRoot, registry);
23
- _logger.Logger.log(_logger.Logger.INFO_TYPE, `Cleanup registry built: ${Object.keys(registry).length} rules from ${modulesRoot}`);
28
+ _logger.Logger.log(_logger.Logger.INFO_TYPE, `Cleanup registry built: ${Object.keys(registry).length} generator chains from ${modulesRoot}`);
24
29
  return registry;
25
30
  }
26
31
  function scanDir(dir, registry) {
@@ -31,27 +36,37 @@ function scanDir(dir, registry) {
31
36
  const fullPath = _path.default.join(dir, entry.name);
32
37
  if (entry.isDirectory()) {
33
38
  scanDir(fullPath, registry);
34
- } else if (entry.name.endsWith('.cleanup.json')) {
39
+ } else if (entry.name.endsWith('.cleanup.js')) {
35
40
  try {
36
- const data = JSON.parse(_fs.default.readFileSync(fullPath, 'utf8'));
37
- for (const [operationId, config] of Object.entries(data)) {
38
- if (!registry[operationId]) {
39
- registry[operationId] = config;
41
+ const cleanupModule = require(fullPath);
42
+ for (const [generatorName, chain] of Object.entries(cleanupModule)) {
43
+ if (registry[generatorName]) {
44
+ throw new Error(`Duplicate cleanup chain for generator "${generatorName}" in ${fullPath}. ` + `Each generator must have exactly one cleanup chain.`);
40
45
  }
46
+ registry[generatorName] = chain;
41
47
  }
42
48
  } catch (err) {
43
- _logger.Logger.log(_logger.Logger.FAILURE_TYPE, `Failed to parse cleanup file: ${fullPath} - ${err.message}`);
49
+ if (err.message.includes('Duplicate cleanup chain')) {
50
+ throw err;
51
+ }
52
+ _logger.Logger.log(_logger.Logger.FAILURE_TYPE, `Failed to load cleanup file: ${fullPath} - ${err.message}`);
44
53
  }
45
54
  }
46
55
  }
47
56
  }
48
- function extractId(cachedData, idPath) {
49
- const result = _jsonpath.default.query(cachedData, idPath);
57
+ function extractId(data, idPath) {
58
+ const result = _jsonpath.default.query(data, idPath);
50
59
  if (result.length === 0) {
51
60
  throw new Error(`Could not extract ID using path "${idPath}"`);
52
61
  }
53
62
  return result[0];
54
63
  }
64
+ function parseLogBody(stepLog) {
65
+ var _stepLog$response;
66
+ if (!(stepLog !== null && stepLog !== void 0 && (_stepLog$response = stepLog.response) !== null && _stepLog$response !== void 0 && _stepLog$response.body)) return null;
67
+ const body = stepLog.response.body;
68
+ return typeof body === 'string' ? JSON.parse(body) : body;
69
+ }
55
70
  async function cleanupViaOAS(config, entityId, actorInfo) {
56
71
  const dataGeneratorObj = actorInfo['data-generator'];
57
72
  if (!dataGeneratorObj) {
@@ -102,13 +117,15 @@ var _default = exports.default = {
102
117
  cacheLayer: async ({}, use) => {
103
118
  const cache = new Map();
104
119
  const cleanupEntries = [];
105
- cache._trackForCleanup = (entityName, data, generators, actorInfo) => {
120
+ cache._trackForCleanup = (entityName, data, generators, actorInfo, logs, generatorName) => {
106
121
  cache.set(entityName, data);
107
122
  cleanupEntries.push({
108
123
  entityName,
109
124
  data,
110
125
  generators,
111
- actorInfo
126
+ actorInfo,
127
+ logs: logs || [],
128
+ generatorName
112
129
  });
113
130
  };
114
131
  await use(cache);
@@ -129,32 +146,36 @@ var _default = exports.default = {
129
146
  let skipped = 0;
130
147
  let failed = 0;
131
148
  for (const entry of [...cleanupEntries].reverse()) {
132
- _logger.Logger.log(_logger.Logger.INFO_TYPE, `Cleanup entity: "${entry.entityName}" (${entry.generators.length} steps)`);
133
- for (const step of [...entry.generators].reverse()) {
134
- const operationId = step.generatorOperationId;
135
- const cleanupConfig = cleanupRegistry[operationId];
136
- if (!cleanupConfig) {
137
- skipped++;
138
- continue;
139
- }
149
+ const cleanupChain = cleanupRegistry[entry.generatorName];
150
+ if (!cleanupChain) {
151
+ _logger.Logger.log(_logger.Logger.INFO_TYPE, `Cleanup: no chain for generator "${entry.generatorName}" — skipping`);
152
+ skipped++;
153
+ continue;
154
+ }
155
+ _logger.Logger.log(_logger.Logger.INFO_TYPE, `Cleanup entity: "${entry.entityName}" generator: "${entry.generatorName}" (${cleanupChain.length} steps)`);
156
+ for (const cleanupStep of cleanupChain) {
140
157
  try {
141
- const entityId = extractId(entry.data, cleanupConfig.idPath);
142
- const actionDesc = cleanupConfig.operationId || `${cleanupConfig.method} ${cleanupConfig.apiPath}`;
143
- _logger.Logger.log(_logger.Logger.INFO_TYPE, `Cleanup [${cleanupConfig.type}] ${step.name}: ${actionDesc} (id: ${entityId})`);
144
- if (cleanupConfig.type === 'oas') {
145
- await cleanupViaOAS(cleanupConfig, entityId, entry.actorInfo);
146
- } else if (cleanupConfig.type === 'api') {
147
- await cleanupViaAPI(cleanupConfig, entityId);
158
+ // Find the step's response from logs by matching operationId
159
+ const stepLog = entry.logs.find(log => log.generationOperationId === cleanupStep.operationId || log.name === cleanupStep.operationId);
160
+ const stepData = parseLogBody(stepLog) || entry.data;
161
+ const entityId = extractId(stepData, cleanupStep.idPath);
162
+ const actionDesc = cleanupStep.operationId || `${cleanupStep.method} ${cleanupStep.apiPath}`;
163
+ _logger.Logger.log(_logger.Logger.INFO_TYPE, `Cleanup [${cleanupStep.type}] ${actionDesc} (id: ${entityId})`);
164
+ if (cleanupStep.type === 'oas') {
165
+ await cleanupViaOAS(cleanupStep, entityId, entry.actorInfo);
166
+ } else if (cleanupStep.type === 'api') {
167
+ await cleanupViaAPI(cleanupStep, entityId);
148
168
  }
149
169
  cleaned++;
150
- _logger.Logger.log(_logger.Logger.SUCCESS_TYPE, `Cleanup success: ${step.name} (id: ${entityId})`);
170
+ _logger.Logger.log(_logger.Logger.SUCCESS_TYPE, `Cleanup success: ${actionDesc} (id: ${entityId})`);
151
171
  } catch (err) {
152
172
  failed++;
153
- _logger.Logger.log(_logger.Logger.FAILURE_TYPE, `Cleanup failed: ${step.name} ${err.message}`);
173
+ const actionDesc = cleanupStep.operationId || `${cleanupStep.method} ${cleanupStep.apiPath}`;
174
+ _logger.Logger.log(_logger.Logger.FAILURE_TYPE, `Cleanup failed: ${actionDesc} — ${err.message}`);
154
175
  }
155
176
  }
156
177
  }
157
- _logger.Logger.log(_logger.Logger.INFO_TYPE, `Cleanup completed: ${cleaned} cleaned, ${skipped} skipped (no cleanup rule), ${failed} failed`);
178
+ _logger.Logger.log(_logger.Logger.INFO_TYPE, `Cleanup completed: ${cleaned} cleaned, ${skipped} skipped (no chain), ${failed} failed`);
158
179
  cleanupEntries.length = 0;
159
180
  cache.clear();
160
181
  }
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@zohodesk/testinglibrary",
3
- "version": "0.0.48-n20-experimental",
3
+ "version": "0.0.50-n20-experimental",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@zohodesk/testinglibrary",
9
- "version": "0.0.48-n20-experimental",
9
+ "version": "0.0.50-n20-experimental",
10
10
  "hasInstallScript": true,
11
11
  "license": "ISC",
12
12
  "dependencies": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zohodesk/testinglibrary",
3
- "version": "0.0.48-n20-experimental",
3
+ "version": "0.0.50-n20-experimental",
4
4
  "main": "./build/index.js",
5
5
  "scripts": {
6
6
  "postinstall": "node bin/postinstall.js",