|
1 | 1 | const test = require('tape') |
2 | 2 | const { |
3 | | - parseFlatData, |
| 3 | + parseLinkValue, |
| 4 | + parseLinkedFields, |
4 | 5 | parseContent, |
5 | 6 | normalizeEntryName, |
| 7 | + parseFlatData, |
6 | 8 | parseTextEntry |
7 | 9 | } = require('./parseTextEntry') |
8 | 10 |
|
| 11 | +test('parseLinkValue removes leading plus and splits by slash', (t) => { |
| 12 | + const value = '+folder/subfolder/item' |
| 13 | + |
| 14 | + const result = parseLinkValue(value) |
| 15 | + |
| 16 | + t.deepEqual( |
| 17 | + result, |
| 18 | + ['folder', 'subfolder', 'item'], |
| 19 | + 'removes plus and splits by slash' |
| 20 | + ) |
| 21 | + |
| 22 | + t.end() |
| 23 | +}) |
| 24 | + |
| 25 | +test('parseLinkValue handles single segment path', (t) => { |
| 26 | + const value = '+folder' |
| 27 | + |
| 28 | + const result = parseLinkValue(value) |
| 29 | + |
| 30 | + t.deepEqual( |
| 31 | + result, |
| 32 | + ['folder'], |
| 33 | + 'returns array with single segment' |
| 34 | + ) |
| 35 | + |
| 36 | + t.end() |
| 37 | +}) |
| 38 | + |
| 39 | +test('parseLinkValue filters out empty segments', (t) => { |
| 40 | + const value = '+folder//item' |
| 41 | + |
| 42 | + const result = parseLinkValue(value) |
| 43 | + |
| 44 | + t.deepEqual( |
| 45 | + result, |
| 46 | + ['folder', 'item'], |
| 47 | + 'removes empty segments from consecutive slashes' |
| 48 | + ) |
| 49 | + |
| 50 | + t.end() |
| 51 | +}) |
| 52 | + |
| 53 | +test('parseLinkValue preserves case sensitivity', (t) => { |
| 54 | + const value = '+PEOPLE/users/ADMINS/john/PROFILE' |
| 55 | + |
| 56 | + const result = parseLinkValue(value) |
| 57 | + |
| 58 | + t.deepEqual( |
| 59 | + result, |
| 60 | + ['PEOPLE', 'users', 'ADMINS', 'john', 'PROFILE'], |
| 61 | + 'preserves mixed case with uppercase and lowercase segments' |
| 62 | + ) |
| 63 | + |
| 64 | + t.end() |
| 65 | +}) |
| 66 | + |
| 67 | +test('parseLinkValue ignores trailing slash', (t) => { |
| 68 | + const value = '+folder/subfolder/' |
| 69 | + |
| 70 | + const result = parseLinkValue(value) |
| 71 | + |
| 72 | + t.deepEqual( |
| 73 | + result, |
| 74 | + ['folder', 'subfolder'], |
| 75 | + 'filters out empty segment from trailing slash' |
| 76 | + ) |
| 77 | + |
| 78 | + t.end() |
| 79 | +}) |
| 80 | + |
| 81 | +test('parseLinkedFields converts single string linked field', (t) => { |
| 82 | + const fields = { author: '+people/users/admins/john' } |
| 83 | + |
| 84 | + const result = parseLinkedFields(fields) |
| 85 | + |
| 86 | + t.deepEqual( |
| 87 | + result.author, |
| 88 | + { key: 'author', linkPath: ['people', 'users', 'admins', 'john'] }, |
| 89 | + 'converts linked field with multiple path segments' |
| 90 | + ) |
| 91 | + |
| 92 | + t.end() |
| 93 | +}) |
| 94 | + |
| 95 | +test('parseLinkedFields converts array of linked fields', (t) => { |
| 96 | + const fields = { tags: ['+categories/tech/frontend/react', '+categories/js/backend/node'] } |
| 97 | + |
| 98 | + const result = parseLinkedFields(fields) |
| 99 | + |
| 100 | + t.deepEqual( |
| 101 | + result.tags[0], |
| 102 | + { key: 'tags', linkPath: ['categories', 'tech', 'frontend', 'react'] }, |
| 103 | + 'converts first array element with deep path' |
| 104 | + ) |
| 105 | + |
| 106 | + t.deepEqual( |
| 107 | + result.tags[1], |
| 108 | + { key: 'tags', linkPath: ['categories', 'js', 'backend', 'node'] }, |
| 109 | + 'converts second array element with deep path' |
| 110 | + ) |
| 111 | + |
| 112 | + t.end() |
| 113 | +}) |
| 114 | + |
| 115 | +test('parseLinkedFields preserves non-linked string fields', (t) => { |
| 116 | + const fields = { title: 'My Title', description: 'Plain text' } |
| 117 | + |
| 118 | + const result = parseLinkedFields(fields) |
| 119 | + |
| 120 | + t.equal( |
| 121 | + result.title, |
| 122 | + 'My Title', |
| 123 | + 'preserves non-linked string field' |
| 124 | + ) |
| 125 | + |
| 126 | + t.equal( |
| 127 | + result.description, |
| 128 | + 'Plain text', |
| 129 | + 'preserves other non-linked fields' |
| 130 | + ) |
| 131 | + |
| 132 | + t.end() |
| 133 | +}) |
| 134 | + |
| 135 | +test('parseLinkedFields converts only the linked items in mixed arrays', (t) => { |
| 136 | + const fields = { mixed: ['+category/tech', 'plain-text', '+category/other'] } |
| 137 | + |
| 138 | + const result = parseLinkedFields(fields) |
| 139 | + |
| 140 | + t.deepEqual( |
| 141 | + result.mixed[0], |
| 142 | + { key: 'mixed', linkPath: ['category', 'tech'] }, |
| 143 | + 'converts first linked field' |
| 144 | + ) |
| 145 | + |
| 146 | + t.equal( |
| 147 | + result.mixed[1], |
| 148 | + 'plain-text', |
| 149 | + 'preserves non-linked item' |
| 150 | + ) |
| 151 | + |
| 152 | + t.deepEqual( |
| 153 | + result.mixed[2], |
| 154 | + { key: 'mixed', linkPath: ['category', 'other'] }, |
| 155 | + 'converts linked field that comes after non-linked item' |
| 156 | + ) |
| 157 | + |
| 158 | + t.end() |
| 159 | +}) |
| 160 | + |
| 161 | +test('parseLinkedFields handles mix of linked and non-linked fields', (t) => { |
| 162 | + const fields = { |
| 163 | + author: '+users/john', |
| 164 | + title: 'Article Title', |
| 165 | + tags: ['+tags/featured', '+tags/new'], |
| 166 | + description: 'An article' |
| 167 | + } |
| 168 | + |
| 169 | + const result = parseLinkedFields(fields) |
| 170 | + |
| 171 | + t.deepEqual( |
| 172 | + result.author, |
| 173 | + { key: 'author', linkPath: ['users', 'john'] }, |
| 174 | + 'converts linked field' |
| 175 | + ) |
| 176 | + |
| 177 | + t.equal( |
| 178 | + result.title, |
| 179 | + 'Article Title', |
| 180 | + 'preserves non-linked string' |
| 181 | + ) |
| 182 | + |
| 183 | + t.deepEqual( |
| 184 | + result.tags[0], |
| 185 | + { key: 'tags', linkPath: ['tags', 'featured'] }, |
| 186 | + 'converts first array element' |
| 187 | + ) |
| 188 | + |
| 189 | + t.deepEqual( |
| 190 | + result.tags[1], |
| 191 | + { key: 'tags', linkPath: ['tags', 'new'] }, |
| 192 | + 'converts second array element' |
| 193 | + ) |
| 194 | + |
| 195 | + t.equal( |
| 196 | + result.description, |
| 197 | + 'An article', |
| 198 | + 'preserves non-linked fields among linked ones' |
| 199 | + ) |
| 200 | + |
| 201 | + t.end() |
| 202 | +}) |
| 203 | + |
9 | 204 | test('parseContent returns content as-is for HTML files', (t) => { |
10 | 205 | const node = { extension: '.html' } |
11 | 206 | const content = '<div>HTML content</div>' |
|
0 commit comments