scjson 0.2.1 → 0.3.1

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/tests/cli.test.js DELETED
@@ -1,172 +0,0 @@
1
- /**
2
- * Agent Name: js-cli-tests
3
- *
4
- * Part of the scjson project.
5
- * Developed by Softoboros Technology Inc.
6
- * Licensed under the BSD 1-Clause License.
7
- */
8
-
9
- const fs = require('fs');
10
- const os = require('os');
11
- const path = require('path');
12
- const { spawnSync } = require('child_process');
13
-
14
- const cliPath = path.resolve(__dirname, '../bin/scjson.js');
15
-
16
- function createScxml() {
17
- return '<scxml xmlns="http://www.w3.org/2005/07/scxml"/>';
18
- }
19
-
20
- function createScjson() {
21
- const json = {
22
- version: 1,
23
- datamodel_attribute: 'null',
24
- };
25
- return JSON.stringify(json, null, 2);
26
- }
27
-
28
- describe('scjson CLI', () => {
29
- test('shows help', () => {
30
- const res = spawnSync('node', [cliPath, '--help'], { encoding: 'utf8' });
31
- expect(res.stderr).not.toMatch(/Failed to convert/);
32
- expect(res.status).toBe(0);
33
- expect(res.stdout).toMatch(/scjson/);
34
- });
35
-
36
- test('single json conversion', () => {
37
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'scjson-'));
38
- const xmlPath = path.join(dir, 'sample.scxml');
39
- fs.writeFileSync(xmlPath, createScxml());
40
-
41
- const res = spawnSync('node', [cliPath, 'json', xmlPath], { encoding: 'utf8' });
42
- expect(res.stderr).not.toMatch(/Failed to convert/);
43
- expect(res.status).toBe(0);
44
-
45
- const outPath = path.join(dir, 'sample.scjson');
46
- expect(fs.existsSync(outPath)).toBe(true);
47
- const data = JSON.parse(fs.readFileSync(outPath, 'utf8'));
48
- expect(data.version).toBe(1);
49
- });
50
-
51
- test('directory json conversion', () => {
52
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'scjson-'));
53
- const srcDir = path.join(dir, 'src');
54
- fs.mkdirSync(srcDir);
55
- ['a', 'b'].forEach(n => {
56
- fs.writeFileSync(path.join(srcDir, `${n}.scxml`), createScxml());
57
- });
58
-
59
- const res = spawnSync('node', [cliPath, 'json', srcDir], { encoding: 'utf8' });
60
- expect(res.stderr).not.toMatch(/Failed to convert/);
61
- expect(res.status).toBe(0);
62
-
63
- ['a', 'b'].forEach(n => {
64
- expect(fs.existsSync(path.join(srcDir, `${n}.scjson`))).toBe(true);
65
- });
66
- });
67
-
68
- test('single xml conversion', () => {
69
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'scjson-'));
70
- const jsonPath = path.join(dir, 'sample.scjson');
71
- fs.writeFileSync(jsonPath, createScjson());
72
-
73
- const res = spawnSync('node', [cliPath, 'xml', jsonPath], { encoding: 'utf8' });
74
- expect(res.stderr).not.toMatch(/Failed to convert/);
75
- expect(res.status).toBe(0);
76
-
77
- const outPath = path.join(dir, 'sample.scxml');
78
- expect(fs.existsSync(outPath)).toBe(true);
79
- const data = fs.readFileSync(outPath, 'utf8');
80
- expect(data).toMatch(/scxml/);
81
- });
82
-
83
- test('directory xml conversion', () => {
84
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'scjson-'));
85
- const srcDir = path.join(dir, 'jsons');
86
- fs.mkdirSync(srcDir);
87
- ['x', 'y'].forEach(n => {
88
- fs.writeFileSync(path.join(srcDir, `${n}.scjson`), createScjson());
89
- });
90
-
91
- const res = spawnSync('node', [cliPath, 'xml', srcDir], { encoding: 'utf8' });
92
- expect(res.stderr).not.toMatch(/Failed to convert/);
93
- expect(res.status).toBe(0);
94
-
95
- ['x', 'y'].forEach(n => {
96
- expect(fs.existsSync(path.join(srcDir, `${n}.scxml`))).toBe(true);
97
- });
98
- });
99
-
100
- function buildDataset(base) {
101
- const d1 = path.join(base, 'level1');
102
- const d2 = path.join(d1, 'level2');
103
- fs.mkdirSync(d2, { recursive: true });
104
- ['a', 'b'].forEach(n => {
105
- fs.writeFileSync(path.join(d1, `${n}.scxml`), createScxml());
106
- fs.writeFileSync(path.join(d2, `${n}.scxml`), createScxml());
107
- });
108
- }
109
-
110
- test('recursive conversion', () => {
111
- const dataset = fs.mkdtempSync(path.join(os.tmpdir(), 'scjson-'));
112
- buildDataset(dataset);
113
- const scjsonDir = path.join(dataset, 'outjson');
114
- const scxmlDir = path.join(dataset, 'outxml');
115
-
116
- let res = spawnSync('node', [cliPath, 'json', dataset, '-o', scjsonDir, '-r'], { encoding: 'utf8' });
117
- expect(res.stderr).not.toMatch(/Failed to convert/);
118
- expect(res.status).toBe(0);
119
- res = spawnSync('node', [cliPath, 'xml', scjsonDir, '-o', scxmlDir, '-r'], { encoding: 'utf8' });
120
- expect(res.stderr).not.toMatch(/Failed to convert/);
121
- expect(res.status).toBe(0);
122
-
123
- const jsonFiles = require('glob').sync('**/*.scjson', { cwd: scjsonDir, nodir: true });
124
- const xmlFiles = require('glob').sync('**/*.scxml', { cwd: scxmlDir, nodir: true });
125
-
126
- expect(jsonFiles.length).toBeGreaterThan(0);
127
- expect(xmlFiles.length).toBeGreaterThan(0);
128
- expect(xmlFiles.length).toBeLessThanOrEqual(jsonFiles.length);
129
- });
130
-
131
- test('recursive validation', () => {
132
- const dataset = fs.mkdtempSync(path.join(os.tmpdir(), 'scjson-'));
133
- buildDataset(dataset);
134
- const scjsonDir = path.join(dataset, 'outjson');
135
- const scxmlDir = path.join(dataset, 'outxml');
136
-
137
- let res = spawnSync('node', [cliPath, 'json', dataset, '-o', scjsonDir, '-r'], { encoding: 'utf8' });
138
- expect(res.stderr).not.toMatch(/Failed to convert/);
139
- expect(res.status).toBe(0);
140
- res = spawnSync('node', [cliPath, 'xml', scjsonDir, '-o', scxmlDir, '-r'], { encoding: 'utf8' });
141
- expect(res.stderr).not.toMatch(/Failed to convert/);
142
- expect(res.status).toBe(0);
143
-
144
- // Corrupt one file to trigger failure
145
- fs.writeFileSync(path.join(scjsonDir, 'corrupt.scjson'), 'bad');
146
-
147
- const validateRes = spawnSync('node', [cliPath, 'validate', dataset, '-r'], { encoding: 'utf8' });
148
- expect(validateRes.stderr).toMatch(/Validation failed/);
149
- expect(validateRes.status).not.toBe(0);
150
- });
151
-
152
- test('recursive verify', () => {
153
- const dataset = fs.mkdtempSync(path.join(os.tmpdir(), 'scjson-'));
154
- buildDataset(dataset);
155
- const scjsonDir = path.join(dataset, 'outjson');
156
- const scxmlDir = path.join(dataset, 'outxml');
157
-
158
- let res = spawnSync('node', [cliPath, 'json', dataset, '-o', scjsonDir, '-r'], { encoding: 'utf8' });
159
- expect(res.stderr).not.toMatch(/Failed to convert/);
160
- expect(res.status).toBe(0);
161
- res = spawnSync('node', [cliPath, 'xml', scjsonDir, '-o', scxmlDir, '-r'], { encoding: 'utf8' });
162
- expect(res.stderr).not.toMatch(/Failed to convert/);
163
- expect(res.status).toBe(0);
164
-
165
- res = spawnSync('node', [cliPath, 'json', scxmlDir, '-r', '-v'], { encoding: 'utf8' });
166
- expect(res.stderr).not.toMatch(/Failed to convert/);
167
- expect(res.status).toBe(0);
168
- res = spawnSync('node', [cliPath, 'xml', scjsonDir, '-r', '-v'], { encoding: 'utf8' });
169
- expect(res.stderr).not.toMatch(/Failed to convert/);
170
- expect(res.status).toBe(0);
171
- });
172
- });
@@ -1,25 +0,0 @@
1
- declare module 'scjson/browser' {
2
- /**
3
- * Recursively removes nulls, empty arrays/objects, and empty strings from a value.
4
- * @param value Any input value (array, object, scalar).
5
- * @returns Sanitized value or `undefined`.
6
- */
7
- export function removeEmpty(value: any): any;
8
-
9
- /**
10
- * Converts SCXML (as XML string) to SCJSON (as JSON string).
11
- * @param xmlStr SCXML input as string.
12
- * @param omitEmpty Optional: if true, removes empty/null fields (default: true).
13
- * @returns Validated SCJSON as pretty-printed JSON string.
14
- * @throws Error if validation fails.
15
- */
16
- export function xmlToJson(xmlStr: string, omitEmpty?: boolean): string;
17
-
18
- /**
19
- * Converts SCJSON (as JSON string) to SCXML (as XML string).
20
- * @param jsonStr SCJSON input as string.
21
- * @returns Valid SCXML string.
22
- * @throws Error if validation fails.
23
- */
24
- export function jsonToXml(jsonStr: string): string;
25
- }