@benup/bensdk 1.6.2 → 1.7.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.
@@ -34,22 +34,21 @@ async function main() {
34
34
 
35
35
  const fileName = `${state.toLocaleLowerCase()}.handler.ts`;
36
36
  let stateHandler = stateHandlerTemplate;
37
- const actionSize = action
38
- .split('_')
37
+ const actionSize = action.split('_');
39
38
  let actionInCamelCase = '';
40
- if(actionSize.length === 1){
41
- actionInCamelCase = action.split('')
42
- .map((word, index) => index === 0 ? word.toUpperCase() : word.toLowerCase())
43
- .join('');
39
+ if (actionSize.length === 1) {
40
+ actionInCamelCase = action
41
+ .split('')
42
+ .map((word, index) => (index === 0 ? word.toUpperCase() : word.toLowerCase()))
43
+ .join('');
44
44
  } else {
45
45
  actionInCamelCase = action
46
- .split('_')[1]
47
- .split('')
48
- .map((word, index) => index === 0 ? word.toUpperCase() : word.toLowerCase())
49
- .join('');
46
+ .split('_')[1]
47
+ .split('')
48
+ .map((word, index) => (index === 0 ? word.toUpperCase() : word.toLowerCase()))
49
+ .join('');
50
50
  }
51
51
 
52
-
53
52
  stateHandler = stateHandler.replace(
54
53
  '// StateMachine,',
55
54
  `const stateMachine = benefitDefinition.stateMachine.${action}.${state}; `
@@ -60,13 +59,13 @@ async function main() {
60
59
  `import benefitDefinition from '../benefit-definition';`
61
60
  );
62
61
 
63
- if(action === 'GRANT' || action === 'REVOKE'){
62
+ if (action === 'GRANT' || action === 'REVOKE') {
64
63
  stateHandler = stateHandler.replace(
65
64
  '// import {Action}',
66
65
  `import { ActionBaseGrantRevoke as Action} from '@benup/bensdk/bin/lib/types/action.types'`
67
66
  );
68
67
  } else {
69
- stateHandler = stateHandler.replace(
68
+ stateHandler = stateHandler.replace(
70
69
  '// import {Action}',
71
70
  `import { ActionBase${actionInCamelCase} as Action} from '@benup/bensdk/bin/lib/types/action.types'`
72
71
  );
@@ -88,6 +87,35 @@ async function main() {
88
87
  }
89
88
  fs.writeFileSync(`${handlersDir}/${fileName}`, stateHandler, 'utf-8');
90
89
  });
90
+
91
+ const testsDir = `${process.cwd()}/__tests__`;
92
+
93
+ if (!fs.existsSync(testsDir)) {
94
+ fs.mkdirSync(testsDir, { recursive: true });
95
+ }
96
+
97
+ // Read the action test template
98
+ let actionTestTemplate = fs.readFileSync(
99
+ process.cwd() + '/bin/cli/templates/action.test.template.ts',
100
+ 'utf-8'
101
+ );
102
+
103
+ // Replaces the action placeholder with the current action name
104
+ actionTestTemplate = actionTestTemplate.replaceAll('CHANGE_ACTION_NAME_PLACEHOLDER', action);
105
+
106
+ fs.writeFileSync(`${testsDir}/${action.toLowerCase()}.test.ts`, actionTestTemplate, 'utf-8');
107
+
108
+ /**
109
+ * Creates the utils.ts for the tests if it doesn't exists
110
+ */
111
+ if (!fs.existsSync(`${testsDir}/utils.ts`)) {
112
+ const utilsTestTemplate = fs.readFileSync(
113
+ process.cwd() + '/bin/cli/templates/utils.template.ts',
114
+ 'utf-8'
115
+ );
116
+
117
+ fs.writeFileSync(`${testsDir}/utils.ts`, utilsTestTemplate, 'utf-8');
118
+ }
91
119
  });
92
120
  }
93
121
 
@@ -0,0 +1,34 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { app } from '../bin/server/app';
3
+ import { getUrl } from './utils';
4
+
5
+ /**
6
+ * Write your state-machine actions tests here!
7
+ * How to do that? This is the test-suite for CHANGE_ACTION_NAME_PLACEHOLDER, We recommend that
8
+ * for each state from the action you write the possible scenarios
9
+ */
10
+ describe('SM Tests for CHANGE_ACTION_NAME_PLACEHOLDER', () => {
11
+ const action = 'CHANGE_ACTION_NAME_PLACEHOLDER';
12
+
13
+ it('[REQUEST_CHANGE_ACTION_NAME_PLACEHOLDER] should fail and return 500 for invalid payload', async () => {
14
+ const res = await app.request(`${getUrl()}`, {
15
+ method: 'POST',
16
+ headers: { 'Content-Type': 'application/json' },
17
+
18
+ /**
19
+ * We recommend you to create an utils or some method that create this payload
20
+ */
21
+ body: JSON.stringify({
22
+ message: {},
23
+ requestPayload: {
24
+ action: 'CHANGE_ACTION_NAME_PLACEHOLDER'
25
+ },
26
+ currentContext: {}
27
+ })
28
+ });
29
+
30
+ const data = await res.text();
31
+
32
+ expect(res.status).toBe(500);
33
+ });
34
+ });
@@ -0,0 +1 @@
1
+ export const getUrl = () => 'http://localhost:3000/run-action';
@@ -69,7 +69,7 @@ wss.on('connection', (ws) => {
69
69
  });
70
70
  });
71
71
 
72
- export async function startServer() {
72
+ async function startServer() {
73
73
  return new Promise((res: any, rej: any) => {
74
74
  const server = serve({ fetch: app.fetch, port: 3000 });
75
75
  server.on('upgrade', (request, socket, head) => {
@@ -85,3 +85,5 @@ export async function startServer() {
85
85
  res();
86
86
  });
87
87
  }
88
+
89
+ export { app, startServer };
@@ -4,7 +4,7 @@
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1",
7
+ "test": "vitest --run",
8
8
  "format": "npx prettier --write .",
9
9
  "lint": "npx tsc --noemit && npx prettier --check . && npx eslint .",
10
10
  "start": "cd bin/viewer && tsx startup.ts",
@@ -34,7 +34,7 @@
34
34
  "pino": "^9.6.0",
35
35
  "hono": "^4.7.7",
36
36
  "@hono/node-server": "^1.14.1",
37
- "vitest": "^3.1.2",
37
+ "vitest": "^3.2.4",
38
38
  "@sveltejs/adapter-auto": "^4.0.0",
39
39
  "@sveltejs/kit": "^2.16.0",
40
40
  "@sveltejs/vite-plugin-svelte": "^5.0.0",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@benup/bensdk",
3
- "version": "1.6.2",
3
+ "version": "1.7.0",
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "scripts": {