@cloudcare/guance-front-tools 1.0.13 → 1.0.15

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/test/cli.test.mjs CHANGED
@@ -4,7 +4,7 @@ import fs from 'node:fs'
4
4
  import os from 'node:os'
5
5
  import path from 'node:path'
6
6
  import { spawnSync } from 'node:child_process'
7
- import { fileURLToPath } from 'node:url'
7
+ import { fileURLToPath, pathToFileURL } from 'node:url'
8
8
 
9
9
  import { covert } from '../lib/scripts/grafana-covert-to-guance-core.js'
10
10
  import {
@@ -15,6 +15,10 @@ const __filename = fileURLToPath(import.meta.url)
15
15
  const __dirname = path.dirname(__filename)
16
16
  const projectRoot = path.resolve(__dirname, '..')
17
17
  const cliPath = path.join(projectRoot, 'cli.js')
18
+ const skillCliPath = path.join(
19
+ projectRoot,
20
+ 'skills/grafana-to-guance-dashboard/scripts/convert-grafana-dashboard.mjs'
21
+ )
18
22
  const grafanaInputPath = path.join(projectRoot, 'lib/example/test2.json')
19
23
  const grafanaLoadInputPath = path.join(projectRoot, 'lib/example/grafana2.json')
20
24
 
@@ -26,6 +30,14 @@ function runCli(args, options = {}) {
26
30
  })
27
31
  }
28
32
 
33
+ function runSkillCli(args, options = {}) {
34
+ return spawnSync(process.execPath, [skillCliPath, ...args], {
35
+ cwd: projectRoot,
36
+ encoding: 'utf-8',
37
+ ...options,
38
+ })
39
+ }
40
+
29
41
  function createTempDir() {
30
42
  return fs.mkdtempSync(path.join(os.tmpdir(), 'guance-front-tools-cli-'))
31
43
  }
@@ -39,6 +51,54 @@ test('covert delegates to the shared converter implementation', () => {
39
51
  assert.deepEqual(actual, expected)
40
52
  })
41
53
 
54
+ test('published esm converter entry does not depend on skills path', async () => {
55
+ const esmCorePath = path.join(projectRoot, 'lib/esm/scripts/grafana-covert-to-guance-core.js')
56
+ const esmCoreSource = fs.readFileSync(esmCorePath, 'utf-8')
57
+ const esmConverterCorePath = path.join(projectRoot, 'lib/esm/scripts/convert-grafana-dashboard-core.js')
58
+ const esmConverterCoreSource = fs.readFileSync(esmConverterCorePath, 'utf-8')
59
+
60
+ assert.match(esmCoreSource, /convert-grafana-dashboard-core\.js/)
61
+ assert.doesNotMatch(esmCoreSource, /skills\/grafana-to-guance-dashboard/)
62
+ assert.doesNotMatch(esmConverterCoreSource, /from 'fs'|from "fs"|from 'path'|from "path"|from 'url'|from "url"|from 'ajv'|from "ajv"/)
63
+
64
+ const publishedModule = await import(pathToFileURL(esmCorePath).href)
65
+ const grafanaDashboard = JSON.parse(fs.readFileSync(grafanaLoadInputPath, 'utf-8'))
66
+ const actual = publishedModule.covert(grafanaDashboard)
67
+ const expected = convertDashboard(grafanaDashboard)
68
+
69
+ assert.deepEqual(actual, expected)
70
+ })
71
+
72
+ test('skill converter script ships wrapper and core files without importing lib paths', () => {
73
+ const skillWrapperPath = path.join(
74
+ projectRoot,
75
+ 'skills/grafana-to-guance-dashboard/scripts/convert-grafana-dashboard.mjs'
76
+ )
77
+ const skillCorePath = path.join(
78
+ projectRoot,
79
+ 'skills/grafana-to-guance-dashboard/scripts/convert-grafana-dashboard-core.js'
80
+ )
81
+ const skillWrapperSource = fs.readFileSync(skillWrapperPath, 'utf-8')
82
+ const skillCoreSource = fs.readFileSync(skillCorePath, 'utf-8')
83
+ const libWrapperSource = fs.readFileSync(
84
+ path.join(projectRoot, 'lib/scripts/convert-grafana-dashboard.js'),
85
+ 'utf-8'
86
+ )
87
+ const libCoreSource = fs.readFileSync(
88
+ path.join(projectRoot, 'lib/scripts/convert-grafana-dashboard-core.js'),
89
+ 'utf-8'
90
+ )
91
+
92
+ assert.doesNotMatch(skillWrapperSource, /lib\/scripts\/convert-grafana-dashboard/)
93
+ assert.equal(skillWrapperSource, libWrapperSource)
94
+ assert.equal(skillCoreSource, libCoreSource)
95
+ assert.match(skillWrapperSource, /import \{ convertDashboard \} from '\.\/convert-grafana-dashboard-core\.js'/)
96
+ assert.match(skillWrapperSource, /^#!\/usr\/bin\/env node/m)
97
+ assert.match(skillWrapperSource, /export function main\(/)
98
+ assert.match(skillWrapperSource, /export function validateDashboardFile\(/)
99
+ assert.match(skillCoreSource, /export function convertDashboard\(/)
100
+ })
101
+
42
102
  test('shared converter infers units from query text when panel unit is missing', () => {
43
103
  const grafanaDashboard = JSON.parse(fs.readFileSync(grafanaLoadInputPath, 'utf-8'))
44
104
 
@@ -371,3 +431,72 @@ test('cli writes default output file to current working directory', () => {
371
431
  const actual = JSON.parse(fs.readFileSync(outPath, 'utf-8'))
372
432
  assert.deepEqual(actual, expected)
373
433
  })
434
+
435
+ test('skill cli converts and validates a grafana dashboard with explicit output path', () => {
436
+ const tempDir = createTempDir()
437
+ const outPath = path.join(tempDir, 'skill', 'guance-dashboard.json')
438
+ const expected = convertDashboard(JSON.parse(fs.readFileSync(grafanaInputPath, 'utf-8')))
439
+
440
+ const result = runSkillCli([
441
+ '--input',
442
+ grafanaInputPath,
443
+ '--output',
444
+ outPath,
445
+ '--validate',
446
+ ])
447
+
448
+ assert.equal(result.status, 0, result.stderr || result.stdout)
449
+ assert.match(result.stdout, /Validated /)
450
+ assert.equal(fs.existsSync(outPath), true)
451
+
452
+ const actual = JSON.parse(fs.readFileSync(outPath, 'utf-8'))
453
+ assert.deepEqual(actual, expected)
454
+ })
455
+
456
+ test('skill cli supports guance promql compatible output', () => {
457
+ const tempDir = createTempDir()
458
+ const outPath = path.join(tempDir, 'skill', 'guance-promql.json')
459
+ const expected = convertDashboard(JSON.parse(fs.readFileSync(grafanaInputPath, 'utf-8')), {
460
+ guancePromqlCompatible: true,
461
+ })
462
+
463
+ const result = runSkillCli([
464
+ '--input',
465
+ grafanaInputPath,
466
+ '--output',
467
+ outPath,
468
+ '--validate',
469
+ '--guance-promql-compatible',
470
+ ])
471
+
472
+ assert.equal(result.status, 0, result.stderr || result.stdout)
473
+ assert.match(result.stdout, /Validated /)
474
+ assert.equal(fs.existsSync(outPath), true)
475
+
476
+ const actual = JSON.parse(fs.readFileSync(outPath, 'utf-8'))
477
+ assert.deepEqual(actual, expected)
478
+ })
479
+
480
+ test('skill cli supports keep grafana meta output', () => {
481
+ const tempDir = createTempDir()
482
+ const outPath = path.join(tempDir, 'skill', 'guance-keep-meta.json')
483
+ const expected = convertDashboard(JSON.parse(fs.readFileSync(grafanaInputPath, 'utf-8')), {
484
+ keepGrafanaMeta: true,
485
+ })
486
+
487
+ const result = runSkillCli([
488
+ '--input',
489
+ grafanaInputPath,
490
+ '--output',
491
+ outPath,
492
+ '--validate',
493
+ '--keep-grafana-meta',
494
+ ])
495
+
496
+ assert.equal(result.status, 0, result.stderr || result.stdout)
497
+ assert.match(result.stdout, /Validated /)
498
+ assert.equal(fs.existsSync(outPath), true)
499
+
500
+ const actual = JSON.parse(fs.readFileSync(outPath, 'utf-8'))
501
+ assert.deepEqual(actual, expected)
502
+ })