@tldiagram/core-ui 2.0.6 → 2.0.7

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,95 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import type { WatchDiff } from '../api/client'
3
+ import type { ExploreData } from '../types'
4
+ import {
5
+ buildWatchDiffLocations,
6
+ changedResourceCount,
7
+ formatDiagramResourceSummary,
8
+ formatStatLine,
9
+ isWatchDiffChange,
10
+ summarizeWatchDiffs,
11
+ totalResourceCount,
12
+ } from './watchDiffSummary'
13
+
14
+ function diff(overrides: Partial<WatchDiff>): WatchDiff {
15
+ return {
16
+ id: 1,
17
+ version_id: 1,
18
+ owner_type: 'symbol',
19
+ owner_key: 'go:main.go:function:Main',
20
+ change_type: 'initialized',
21
+ resource_type: 'element',
22
+ resource_id: 101,
23
+ ...overrides,
24
+ }
25
+ }
26
+
27
+ describe('watch diff summary', () => {
28
+ const data: ExploreData = {
29
+ tree: [{
30
+ id: 1,
31
+ owner_element_id: null,
32
+ name: 'Root',
33
+ description: null,
34
+ level_label: null,
35
+ level: 1,
36
+ depth: 0,
37
+ created_at: '2024-01-01',
38
+ updated_at: '2024-01-01',
39
+ parent_view_id: null,
40
+ children: [],
41
+ }],
42
+ navigations: [],
43
+ views: {
44
+ 1: {
45
+ placements: [{
46
+ id: 1,
47
+ view_id: 1,
48
+ element_id: 101,
49
+ position_x: 0,
50
+ position_y: 0,
51
+ name: 'Main',
52
+ description: null,
53
+ kind: 'service',
54
+ technology: null,
55
+ url: null,
56
+ logo_url: null,
57
+ technology_connectors: [],
58
+ tags: [],
59
+ has_view: false,
60
+ view_label: null,
61
+ }],
62
+ connectors: [],
63
+ },
64
+ },
65
+ }
66
+
67
+ it('does not describe clean initial resources as changed', () => {
68
+ const summary = summarizeWatchDiffs([
69
+ diff({ id: 1, change_type: 'initialized', resource_type: 'element', resource_id: 101 }),
70
+ diff({ id: 2, change_type: 'initialized', resource_type: 'element', resource_id: 102 }),
71
+ diff({ id: 3, change_type: 'initialized', resource_type: 'connector', resource_id: 201 }),
72
+ ])
73
+
74
+ expect(changedResourceCount(summary.elements)).toBe(0)
75
+ expect(totalResourceCount(summary.elements)).toBe(2)
76
+ expect(formatDiagramResourceSummary(summary)).toBe('3 initialized resources')
77
+ expect(formatStatLine('element', summary.elements)).toBe('2 elements initialized')
78
+ expect(isWatchDiffChange('initialized')).toBe(false)
79
+ expect(buildWatchDiffLocations(data, [
80
+ diff({ id: 1, change_type: 'initialized', resource_type: 'element', resource_id: 101 }),
81
+ ])).toEqual([])
82
+ })
83
+
84
+ it('keeps changed and initialized resources separate in labels', () => {
85
+ const summary = summarizeWatchDiffs([
86
+ diff({ id: 1, change_type: 'updated', resource_type: 'element', resource_id: 101 }),
87
+ diff({ id: 2, change_type: 'deleted', resource_type: 'connector', resource_id: 201 }),
88
+ diff({ id: 3, change_type: 'initialized', resource_type: 'element', resource_id: 102 }),
89
+ ])
90
+
91
+ expect(formatDiagramResourceSummary(summary)).toBe('2 changed, 1 initialized resources')
92
+ expect(formatStatLine('connector', summary.connectors)).toBe('1 connector changed')
93
+ expect(formatStatLine('element', summary.elements)).toBe('1 element changed, 1 element initialized')
94
+ })
95
+ })
@@ -31,11 +31,24 @@ export interface WatchDiffSummary {
31
31
  connectors: WatchResourceStat
32
32
  }
33
33
 
34
+ export function changedResourceCount(stat: WatchResourceStat): number {
35
+ return stat.added + stat.updated + stat.deleted
36
+ }
37
+
38
+ export function totalResourceCount(stat: WatchResourceStat): number {
39
+ return changedResourceCount(stat) + stat.initialized
40
+ }
41
+
34
42
  export function normalizeWatchChangeType(value: string): WatchChangeType {
35
43
  if (value === 'added' || value === 'updated' || value === 'deleted' || value === 'initialized') return value
36
44
  return 'updated'
37
45
  }
38
46
 
47
+ export function isWatchDiffChange(value: string | null | undefined): boolean {
48
+ const change = normalizeWatchChangeType(value ?? '')
49
+ return change === 'added' || change === 'updated' || change === 'deleted'
50
+ }
51
+
39
52
  export function emptyWatchResourceStat(): WatchResourceStat {
40
53
  return { added: 0, updated: 0, deleted: 0, initialized: 0, addedLines: 0, removedLines: 0 }
41
54
  }
@@ -64,13 +77,32 @@ export function summarizeWatchDiffs(diffs: WatchDiff[] | null | undefined): Watc
64
77
  }
65
78
 
66
79
  export function formatStatLine(label: string, stat: WatchResourceStat): string {
67
- const total = stat.added + stat.updated + stat.deleted + stat.initialized
68
- const parts = [`${total} ${label}${total === 1 ? '' : 's'} changed`]
80
+ const changed = changedResourceCount(stat)
81
+ const initialized = stat.initialized
82
+ const parts = []
83
+ if (changed > 0) parts.push(`${changed} ${label}${changed === 1 ? '' : 's'} changed`)
84
+ if (initialized > 0) parts.push(`${initialized} ${label}${initialized === 1 ? '' : 's'} initialized`)
85
+ if (parts.length === 0) parts.push(`0 ${label}s changed`)
69
86
  if (stat.addedLines > 0) parts.push(`+${stat.addedLines}`)
70
87
  if (stat.removedLines > 0) parts.push(`-${stat.removedLines}`)
71
88
  return parts.join(', ')
72
89
  }
73
90
 
91
+ export function formatDiagramResourceSummary(summary: WatchDiffSummary): string {
92
+ const changed = changedResourceCount(summary.elements) + changedResourceCount(summary.connectors)
93
+ const initialized = summary.elements.initialized + summary.connectors.initialized
94
+ if (changed > 0 && initialized > 0) {
95
+ return `${changed} changed, ${initialized} initialized resources`
96
+ }
97
+ if (changed > 0) {
98
+ return `${changed} changed resource${changed === 1 ? '' : 's'}`
99
+ }
100
+ if (initialized > 0) {
101
+ return `${initialized} initialized resource${initialized === 1 ? '' : 's'}`
102
+ }
103
+ return 'No diagram resource changes'
104
+ }
105
+
74
106
  export function formatTldStatLine(summary: WatchDiffSummary): string {
75
107
  return [
76
108
  formatStatLine('element', summary.elements),
@@ -131,6 +163,7 @@ export function buildWatchDiffLocations(data: ExploreData, diffs: WatchDiff[] |
131
163
 
132
164
  const locations: WatchDiffLocation[] = []
133
165
  ;(Array.isArray(diffs) ? diffs : []).forEach((diff) => {
166
+ if (!isWatchDiffChange(diff.change_type)) return
134
167
  if (!diff.resource_id) return
135
168
  const base = {
136
169
  changeType: normalizeWatchChangeType(diff.change_type),