@pagopa/it-wallet-conformance-tool 1.1.5

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.
@@ -0,0 +1,170 @@
1
+ /**
2
+ * Common Vitest configuration factory
3
+ *
4
+ * Creates test configuration for different test types (issuance, presentation)
5
+ * with automatic tests directory resolution from config.ini
6
+ */
7
+
8
+ import { createConsola } from "consola";
9
+ import { parse } from "ini";
10
+ import * as fs from "node:fs";
11
+ import * as path from "node:path";
12
+ import process from "node:process";
13
+ import { pathToFileURL } from "node:url";
14
+ import { configDefaults, defineConfig } from "vitest/config";
15
+
16
+ const packageRoot = import.meta.dirname;
17
+ const sourceTestsRoot = path.join(packageRoot, "tests");
18
+ const builtTestsRoot = path.join(packageRoot, "dist/tests");
19
+ const useBuiltTests =
20
+ !fs.existsSync(sourceTestsRoot) && fs.existsSync(builtTestsRoot);
21
+ const testsRoot = useBuiltTests ? builtTestsRoot : sourceTestsRoot;
22
+ const testFileExtension = useBuiltTests ? "js" : "ts";
23
+ const builtReporterPath = path.join(packageRoot, "dist/src/report/reporter.js");
24
+ const sourceReporterPath = path.join(packageRoot, "src/report/reporter.ts");
25
+ const reporterModulePath = fs.existsSync(builtReporterPath)
26
+ ? builtReporterPath
27
+ : sourceReporterPath;
28
+ const { ConformanceReporter } = await import(
29
+ pathToFileURL(reporterModulePath).href
30
+ );
31
+
32
+ const log = createConsola({ level: 3 });
33
+
34
+ const exclude = buildExcludePatterns(useBuiltTests);
35
+
36
+ export function buildExcludePatterns(runsBuiltTests) {
37
+ return runsBuiltTests
38
+ ? configDefaults.exclude.filter(
39
+ (pattern) => !["**/dist/**", "**/node_modules/**"].includes(pattern),
40
+ )
41
+ : configDefaults.exclude;
42
+ }
43
+
44
+ export function buildIncludePattern(testType, testsDir, userConfigured) {
45
+ const normalizedTestsDir = testsDir.replace(/\\/g, "/");
46
+
47
+ return userConfigured
48
+ ? `${normalizedTestsDir}/**/*.${testType}.spec.{js,ts}`
49
+ : `${normalizedTestsDir}/**/*.${testType}.spec.${useBuiltTests ? "js" : "ts"}`;
50
+ }
51
+
52
+ /**
53
+ * Create Vitest configuration for a specific test type
54
+ *
55
+ * @param {string} testType - Type of test ('issuance' or 'presentation')
56
+ * @returns {import('vitest/config').UserConfig} Vitest configuration
57
+ */
58
+ export function createTestConfig(testType) {
59
+ const { testsDir, userConfigured } = getTestsDir(testType);
60
+ const includePattern = buildIncludePattern(
61
+ testType,
62
+ testsDir,
63
+ userConfigured,
64
+ );
65
+
66
+ log.debug(`[${testType}] Tests directory: ${testsDir}`);
67
+ log.debug(`[${testType}] Include pattern: ${includePattern}`);
68
+
69
+ return defineConfig({
70
+ resolve: {
71
+ alias: {
72
+ "#": useBuiltTests
73
+ ? path.join(packageRoot, "dist/tests")
74
+ : path.join(packageRoot, "tests"),
75
+ "@": useBuiltTests
76
+ ? path.join(packageRoot, "dist/src")
77
+ : path.join(packageRoot, "src"),
78
+ },
79
+ },
80
+ root: packageRoot,
81
+ test: {
82
+ exclude,
83
+ fileParallelism: false,
84
+ globalSetup: path.join(testsRoot, `global-setup.${testFileExtension}`),
85
+ hookTimeout: 120000,
86
+ include: [includePattern],
87
+ reporters: ["dot", new ConformanceReporter(testType)],
88
+ setupFiles: [path.join(testsRoot, `setup-tls.${testFileExtension}`)],
89
+ },
90
+ });
91
+ }
92
+
93
+ export function resolveConfigPath(
94
+ launchDir = process.cwd(),
95
+ rootDir = packageRoot,
96
+ ) {
97
+ if (process.env.CONFIG_FILE_INI) {
98
+ return path.isAbsolute(process.env.CONFIG_FILE_INI)
99
+ ? process.env.CONFIG_FILE_INI
100
+ : path.resolve(launchDir, process.env.CONFIG_FILE_INI);
101
+ }
102
+
103
+ const localConfigPath = path.resolve(launchDir, "config.ini");
104
+ if (fs.existsSync(localConfigPath)) {
105
+ return localConfigPath;
106
+ }
107
+
108
+ const packageConfigPath = path.join(rootDir, "config.ini");
109
+ if (fs.existsSync(packageConfigPath)) {
110
+ return packageConfigPath;
111
+ }
112
+
113
+ return path.join(rootDir, "config.example.ini");
114
+ }
115
+
116
+ /**
117
+ * Get tests directory for a specific test type
118
+ * Supports CLI override via environment variables
119
+ *
120
+ * @param {string} testType - Type of test ('issuance' or 'presentation')
121
+ * @returns {string} Tests directory path
122
+ */
123
+ function getTestsDir(testType) {
124
+ const envVarMap = {
125
+ issuance: "CONFIG_ISSUANCE_TESTS_DIR",
126
+ presentation: "CONFIG_PRESENTATION_TESTS_DIR",
127
+ };
128
+
129
+ const defaultDirMap = {
130
+ issuance: useBuiltTests
131
+ ? path.join(packageRoot, "dist/tests/conformance/issuance")
132
+ : path.join(packageRoot, "tests/conformance/issuance"),
133
+ presentation: useBuiltTests
134
+ ? path.join(packageRoot, "dist/tests/conformance/presentation")
135
+ : path.join(packageRoot, "tests/conformance/presentation"),
136
+ };
137
+
138
+ // CLI override via environment variable takes precedence
139
+ const envVar = envVarMap[testType];
140
+ if (process.env[envVar]) {
141
+ return {
142
+ testsDir: path.isAbsolute(process.env[envVar])
143
+ ? process.env[envVar]
144
+ : path.resolve(process.cwd(), process.env[envVar]),
145
+ userConfigured: true,
146
+ };
147
+ }
148
+
149
+ // Read from config.ini
150
+ try {
151
+ const configPath = resolveConfigPath();
152
+ const configContent = fs.readFileSync(configPath, "utf-8");
153
+ const config = parse(configContent);
154
+ const testsDir = config[testType]?.tests_dir;
155
+ if (!testsDir) {
156
+ return { testsDir: defaultDirMap[testType], userConfigured: false };
157
+ }
158
+ return {
159
+ testsDir: path.isAbsolute(testsDir)
160
+ ? testsDir
161
+ : path.resolve(path.dirname(configPath), testsDir),
162
+ userConfigured: true,
163
+ };
164
+ } catch {
165
+ log.debug(
166
+ `Could not read config.ini, using default tests directory: ${defaultDirMap[testType]}`,
167
+ );
168
+ return { testsDir: defaultDirMap[testType], userConfigured: false };
169
+ }
170
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Vitest configuration for issuance tests
3
+ * Uses common configuration factory to avoid duplication
4
+ */
5
+
6
+ import { createTestConfig } from "./vitest.common.js";
7
+
8
+ export default createTestConfig("issuance");
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Vitest configuration for presentation tests
3
+ * Uses common configuration factory to avoid duplication
4
+ */
5
+
6
+ import { createTestConfig } from "./vitest.common.js";
7
+
8
+ export default createTestConfig("presentation");