blok0 0.1.0 โ†’ 0.1.2

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-ast.js ADDED
@@ -0,0 +1,150 @@
1
+ const { updatePageCollectionConfig, updateRenderBlocksComponent, findPagesCollection, extractComponentName } = require('./dist/ast/index.js');
2
+ const { slugToIdentifier } = require('./dist/blocks/index.js');
3
+ const fs = require('fs');
4
+
5
+ // Test case: Add editorial--carousel block
6
+ // Simulates: blok0 add-block https://www.blok0.xyz/api/cli/sections/editorial--carousel
7
+
8
+ console.log('๐Ÿงช Testing Editorial Carousel Block Addition');
9
+ console.log('==========================================');
10
+ console.log('');
11
+
12
+ // Change to Payload project directory
13
+ const payloadProjectPath = '../newcms';
14
+ console.log(`๐Ÿ“ Changing to Payload project directory: ${payloadProjectPath}`);
15
+ try {
16
+ process.chdir(payloadProjectPath);
17
+ console.log('โœ… Successfully changed directory');
18
+ } catch (error) {
19
+ console.log('โŒ Failed to change directory:', error.message);
20
+ process.exit(1);
21
+ }
22
+
23
+ console.log('');
24
+ console.log('๐Ÿ” Finding Pages collection...');
25
+ const pagesPath = findPagesCollection();
26
+ console.log('Pages collection path:', pagesPath);
27
+
28
+ if (pagesPath) {
29
+ console.log('');
30
+ console.log('๐Ÿงช Testing AST update for editorial--carousel block...');
31
+
32
+ const blockSlug = 'editorial--carousel';
33
+ const blockIdentifier = slugToIdentifier(blockSlug);
34
+ const blockConfigPath = `@/blocks/${blockSlug}/config`;
35
+
36
+ console.log(`Block slug: ${blockSlug}`);
37
+ console.log(`Block identifier: ${blockIdentifier}`);
38
+ console.log(`Config path: ${blockConfigPath}`);
39
+ console.log('');
40
+
41
+ // Read file before modification
42
+ const originalContent = fs.readFileSync(pagesPath, 'utf-8');
43
+ const hasOriginalImport = originalContent.includes(`import { ${blockIdentifier} } from '${blockConfigPath}'`);
44
+ const hasOriginalBlock = originalContent.includes(blockIdentifier);
45
+
46
+ console.log('๐Ÿ“‹ Before modification:');
47
+ console.log(` - Import present: ${hasOriginalImport}`);
48
+ console.log(` - Block in array: ${hasOriginalBlock}`);
49
+ console.log('');
50
+
51
+ try {
52
+ updatePageCollectionConfig(pagesPath, blockConfigPath, blockIdentifier);
53
+
54
+ // Read file after modification
55
+ const updatedContent = fs.readFileSync(pagesPath, 'utf-8');
56
+ const hasUpdatedImport = updatedContent.includes(`import { ${blockIdentifier} } from '${blockConfigPath}'`);
57
+ const hasUpdatedBlock = updatedContent.includes(blockIdentifier);
58
+
59
+ console.log('๐Ÿ“‹ After Pages modification:');
60
+ console.log(` - Import present: ${hasUpdatedImport}`);
61
+ console.log(` - Block in array: ${hasUpdatedBlock}`);
62
+ console.log('');
63
+
64
+ if (hasUpdatedImport && hasUpdatedBlock) {
65
+ console.log('โœ… Pages collection update successful!');
66
+ } else {
67
+ console.log('โš ๏ธ Pages collection update issues:');
68
+ if (!hasUpdatedImport) console.log(' - โŒ Import was not added');
69
+ if (!hasUpdatedBlock) console.log(' - โŒ Block was not added to array');
70
+ process.exit(1);
71
+ }
72
+
73
+ // Now test RenderBlocks update
74
+ console.log('๐Ÿงช Testing RenderBlocks component update...');
75
+
76
+ const renderBlocksPath = '../newcms/src/blocks/RenderBlocks.tsx';
77
+ const componentPath = `./${blockSlug}/Component`;
78
+
79
+ // Check if the component file exists (it won't for editorial-carousel in test)
80
+ const fullComponentPath = require('path').resolve(componentPath.replace('./', 'src/blocks/') + '.tsx');
81
+ const componentExists = fs.existsSync(fullComponentPath);
82
+
83
+ if (componentExists) {
84
+ console.log('๐Ÿ“„ Component file exists, testing full RenderBlocks update...');
85
+
86
+ // Extract the actual component name
87
+ const actualComponentName = extractComponentName(fullComponentPath);
88
+ const blockTypeKey = 'editorialCarousel';
89
+
90
+ console.log(`Extracted component name: ${actualComponentName}`);
91
+ console.log(`Block type key: ${blockTypeKey}`);
92
+
93
+ // Read RenderBlocks before modification
94
+ const originalRenderBlocksContent = fs.readFileSync(renderBlocksPath, 'utf-8');
95
+ const hasOriginalComponentImport = originalRenderBlocksContent.includes(`import { ${actualComponentName} } from '${componentPath}'`);
96
+ const hasOriginalBlockComponent = originalRenderBlocksContent.includes(`'${blockTypeKey}': ${actualComponentName}`);
97
+
98
+ console.log('๐Ÿ“‹ RenderBlocks before modification:');
99
+ console.log(` - Component import present: ${hasOriginalComponentImport}`);
100
+ console.log(` - Block component mapping present: ${hasOriginalBlockComponent}`);
101
+ console.log('');
102
+
103
+ updateRenderBlocksComponent(renderBlocksPath, blockSlug, componentPath);
104
+
105
+ // Read RenderBlocks after modification
106
+ const updatedRenderBlocksContent = fs.readFileSync(renderBlocksPath, 'utf-8');
107
+ const hasUpdatedComponentImport = updatedRenderBlocksContent.includes(`import { ${actualComponentName} } from '${componentPath}'`);
108
+ const hasUpdatedBlockComponent = updatedRenderBlocksContent.includes(`'${blockTypeKey}': ${actualComponentName}`);
109
+
110
+ console.log('๐Ÿ“‹ RenderBlocks after modification:');
111
+ console.log(` - Component import present: ${hasUpdatedComponentImport}`);
112
+ console.log(` - Block component mapping present: ${hasUpdatedBlockComponent}`);
113
+ console.log('');
114
+
115
+ if (hasUpdatedComponentImport && hasUpdatedBlockComponent) {
116
+ console.log('โœ… RenderBlocks update successful! Component and mapping added.');
117
+ console.log('');
118
+ console.log('๐ŸŽ‰ Complete test passed! Pages collection and RenderBlocks updates work correctly.');
119
+ } else {
120
+ console.log('โš ๏ธ RenderBlocks update issues:');
121
+ if (!hasUpdatedComponentImport) console.log(' - โŒ Component import was not added');
122
+ if (!hasUpdatedBlockComponent) console.log(' - โŒ Block component mapping was not added');
123
+ console.log('');
124
+ console.log('๐Ÿ’ฅ Test failed! RenderBlocks implementation has issues.');
125
+ process.exit(1);
126
+ }
127
+ } else {
128
+ console.log('๐Ÿ“„ Component file does not exist (expected for test), testing error handling...');
129
+
130
+ try {
131
+ updateRenderBlocksComponent(renderBlocksPath, blockSlug, componentPath);
132
+ console.log('โŒ Expected error but function succeeded unexpectedly');
133
+ process.exit(1);
134
+ } catch (error) {
135
+ console.log('โœ… Function correctly failed with error:', error.message);
136
+ console.log('');
137
+ console.log('๐ŸŽ‰ Test passed! Function properly handles missing component files.');
138
+ }
139
+ }
140
+ } catch (error) {
141
+ console.log('โŒ AST update failed:', error.message);
142
+ console.log('');
143
+ console.log('๐Ÿ’ฅ Test failed! The bug may not be fully fixed.');
144
+ process.exit(1);
145
+ }
146
+ } else {
147
+ console.log('โŒ Could not find Pages collection in Payload project');
148
+ console.log('Make sure you are running this from a valid Payload CMS project directory.');
149
+ process.exit(1);
150
+ }