Skip to content

Commit 9f5f684

Browse files
committed
add coverage
1 parent d48497b commit 9f5f684

2 files changed

Lines changed: 206 additions & 0 deletions

File tree

test/gridsetImageDebug.test.ts

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import AdmZip from 'adm-zip';
2+
import { auditGridsetImages, formatImageAuditSummary } from '../src/processors/gridset/imageDebug';
3+
4+
describe('Image Debugging Utilities', () => {
5+
function createMinimalGridset(
6+
options: {
7+
includeImages?: boolean;
8+
includeBrokenImages?: boolean;
9+
includeSymbolLibrary?: boolean;
10+
} = {}
11+
): Buffer {
12+
const zip = new AdmZip();
13+
14+
// Add Settings
15+
zip.addFile(
16+
'Settings0/settings.xml',
17+
Buffer.from(
18+
'<?xml version="1.0"?><Settings><Workspaces><Workspace /></Workspaces></Settings>'
19+
)
20+
);
21+
22+
// Create a simple grid with images
23+
let gridXml = `<?xml version="1.0"?>
24+
<Grid xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
25+
<GridGuid>test-grid-guid</GridGuid>
26+
<Name>Test Grid</Name>
27+
<BackgroundColour>#FFFFFFFF</BackgroundColour>
28+
<ColumnDefinitions>
29+
<ColumnDefinition Width="Large" />
30+
<ColumnDefinition Width="Large" />
31+
</ColumnDefinitions>
32+
<RowDefinitions>
33+
<RowDefinition />
34+
<RowDefinition />
35+
</RowDefinitions>
36+
<Cells>
37+
<Cell X="1" Y="1">
38+
<Content>
39+
<CaptionAndImage>
40+
<Caption>Test Cell 1</Caption>`;
41+
42+
if (options.includeBrokenImages) {
43+
gridXml += ` <Image>-0-text-0.png</Image>`;
44+
} else if (options.includeSymbolLibrary) {
45+
gridXml += ` <Image>[widgit]symbols/food/apple.png</Image>`;
46+
} else if (options.includeImages) {
47+
gridXml += ` <Image>test-image.png</Image>`;
48+
}
49+
50+
gridXml += ` </CaptionAndImage>
51+
</Content>
52+
</Cell>
53+
<Cell X="2" Y="1">
54+
<Content>
55+
<CaptionAndImage>
56+
<Caption>Test Cell 2</Caption>`;
57+
58+
if (options.includeImages) {
59+
gridXml += ` <Image>another-image.png</Image>`;
60+
}
61+
62+
gridXml += ` </CaptionAndImage>
63+
</Content>
64+
</Cell>
65+
</Cells>
66+
</Grid>`;
67+
68+
zip.addFile('Grids/Test Grid/grid.xml', Buffer.from(gridXml));
69+
70+
// Add image files
71+
if (options.includeImages) {
72+
zip.addFile('Grids/Test Grid/test-image.png', Buffer.from('PNG-DATA'));
73+
zip.addFile('Grids/Test Grid/another-image.png', Buffer.from('PNG-DATA'));
74+
}
75+
76+
if (options.includeBrokenImages) {
77+
// Add image with coordinate prefix
78+
zip.addFile('Grids/Test Grid/1-1-0-text-0.png', Buffer.from('PNG-DATA'));
79+
}
80+
81+
return zip.toBuffer();
82+
}
83+
84+
describe('auditGridsetImages', () => {
85+
it('should audit gridset with all images resolved', async () => {
86+
const buffer = createMinimalGridset({ includeImages: true });
87+
const audit = await auditGridsetImages(buffer);
88+
89+
expect(audit.totalCells).toBe(2);
90+
expect(audit.cellsWithImages).toBe(2);
91+
expect(audit.resolvedImages).toBe(2);
92+
expect(audit.unresolvedImages).toBe(0);
93+
expect(audit.issues).toHaveLength(0);
94+
expect(audit.availableImages.length).toBeGreaterThan(0);
95+
});
96+
97+
it('should detect broken image references', async () => {
98+
const buffer = createMinimalGridset({ includeBrokenImages: true });
99+
const audit = await auditGridsetImages(buffer);
100+
101+
expect(audit.unresolvedImages).toBeGreaterThan(0);
102+
expect(audit.issues.length).toBeGreaterThan(0);
103+
104+
const issue = audit.issues[0];
105+
expect(issue.issue).toBe('not_found');
106+
expect(issue.gridName).toBe('Test Grid');
107+
expect(issue.cellX).toBe(1);
108+
expect(issue.cellY).toBe(1);
109+
});
110+
111+
it('should identify symbol library references', async () => {
112+
const buffer = createMinimalGridset({ includeSymbolLibrary: true });
113+
const audit = await auditGridsetImages(buffer);
114+
115+
expect(audit.unresolvedImages).toBeGreaterThan(0);
116+
117+
const issue = audit.issues.find((i: { issue: string }) => i.issue === 'symbol_library');
118+
expect(issue).toBeDefined();
119+
expect(issue?.declaredImage).toContain('widgit');
120+
expect(issue?.suggestion).toContain('symbol library');
121+
});
122+
123+
it('should provide available images list', async () => {
124+
const buffer = createMinimalGridset({ includeImages: true });
125+
const audit = await auditGridsetImages(buffer);
126+
127+
expect(audit.availableImages).toContain('Grids/Test Grid/test-image.png');
128+
expect(audit.availableImages).toContain('Grids/Test Grid/another-image.png');
129+
});
130+
});
131+
132+
describe('formatImageAuditSummary', () => {
133+
it('should format audit results as readable text', async () => {
134+
const buffer = createMinimalGridset({ includeImages: true });
135+
const audit = await auditGridsetImages(buffer);
136+
const summary = formatImageAuditSummary(audit);
137+
138+
expect(summary).toContain('Grid3 Image Audit Summary');
139+
expect(summary).toContain('Total cells: 2');
140+
expect(summary).toContain('Resolved images: 2');
141+
expect(summary).toContain('Unresolved images: 0');
142+
});
143+
144+
it('should include issue details when problems exist', async () => {
145+
const buffer = createMinimalGridset({ includeBrokenImages: true });
146+
const audit = await auditGridsetImages(buffer);
147+
const summary = formatImageAuditSummary(audit);
148+
149+
expect(summary).toContain('Image Issues');
150+
expect(summary).toContain('NOT_FOUND');
151+
});
152+
153+
it('should group issues by type', async () => {
154+
const buffer = createMinimalGridset({ includeSymbolLibrary: true });
155+
const audit = await auditGridsetImages(buffer);
156+
const summary = formatImageAuditSummary(audit);
157+
158+
expect(summary).toContain('SYMBOL_LIBRARY');
159+
});
160+
});
161+
});

test/gridsetResolver.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,49 @@ describe('resolveGrid3CellImage', () => {
6262
});
6363
expect(p2).toBe('builtin://home');
6464
});
65+
66+
it('resolves coordinate-prefixed image names starting with "-"', async () => {
67+
const zip = mkZip({
68+
'Grids/Home/1-4-0-text-0.jpeg': 'IMG',
69+
'Grids/Home/2-3-0-text-0.png': 'PNG',
70+
});
71+
const p1 = resolveGrid3CellImage(zip, {
72+
baseDir: 'Grids/Home/',
73+
imageName: '-0-text-0.jpeg',
74+
x: 1,
75+
y: 4,
76+
});
77+
expect(p1).toBe('Grids/Home/1-4-0-text-0.jpeg');
78+
79+
const p2 = resolveGrid3CellImage(zip, {
80+
baseDir: 'Grids/Home/',
81+
imageName: '-0-text-0.png',
82+
x: 2,
83+
y: 3,
84+
});
85+
expect(p2).toBe('Grids/Home/2-3-0-text-0.png');
86+
});
87+
88+
it('returns null for coordinate-prefixed names when file does not exist', async () => {
89+
const zip = mkZip({});
90+
const p = resolveGrid3CellImage(zip, {
91+
baseDir: 'Grids/Home/',
92+
imageName: '-0-text-0.png',
93+
x: 1,
94+
y: 4,
95+
});
96+
expect(p).toBeNull();
97+
});
98+
99+
it('returns null for coordinate-prefixed names when coordinates are missing', async () => {
100+
const zip = mkZip({
101+
'Grids/Home/1-4-0-text-0.jpeg': 'IMG',
102+
});
103+
const p = resolveGrid3CellImage(zip, {
104+
baseDir: 'Grids/Home/',
105+
imageName: '-0-text-0.jpeg',
106+
// No x, y provided
107+
});
108+
expect(p).toBeNull();
109+
});
65110
});

0 commit comments

Comments
 (0)