@astryxdesign/native 0.7.6-canary.dfc83ee → 0.7.6-canary.e30ffbf

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.
Files changed (32) hide show
  1. package/apple/Package.swift +0 -2
  2. package/apple/README.md +18 -3
  3. package/apple/Sources/Astryx/Avatar/Avatar.swift +2 -9
  4. package/apple/Sources/Astryx/Base/SafeDestination.swift +48 -0
  5. package/apple/Sources/Astryx/Button/Button.swift +3 -21
  6. package/apple/Sources/Astryx/Item/Item.swift +354 -0
  7. package/apple/Sources/Astryx/Wire/AstryxNodeView.swift +9 -0
  8. package/apple/Sources/AstryxNative/Generated/AstryxNode.swift +3 -0
  9. package/apple/Sources/AstryxNative/Generated/Item.swift +173 -0
  10. package/apple/Tests/AstryxTests/ItemTests.swift +295 -0
  11. package/dist/components/Item.d.ts +10 -0
  12. package/dist/components/Item.d.ts.map +1 -0
  13. package/dist/components/Item.js +22 -0
  14. package/dist/components/index.d.ts.map +1 -1
  15. package/dist/components/index.js +2 -0
  16. package/dist/types/android.d.ts +16 -3
  17. package/dist/types/core.d.ts +18 -0
  18. package/dist/types/ios.d.ts +16 -3
  19. package/dist/types/macos.d.ts +16 -3
  20. package/dist/types/shared.d.ts +16 -3
  21. package/dist/types/windows.d.ts +16 -3
  22. package/package.json +3 -3
  23. package/src/components/Item.ts +34 -0
  24. package/src/components/index.ts +2 -0
  25. package/src/contract/contracts.test.ts +39 -0
  26. package/src/theme/swiftIOSTheme.test.ts +372 -38
  27. package/apple/Sources/Astryx/Avatar/Avatar.spec.md +0 -219
  28. package/apple/Sources/Astryx/Avatar/AvatarStatusDot.spec.md +0 -162
  29. package/apple/Sources/Astryx/Button/Button.spec.md +0 -174
  30. package/apple/Sources/Astryx/Icon/Icon.spec.md +0 -189
  31. package/apple/Sources/Astryx/TextInput/TextInput.spec.md +0 -179
  32. package/apple/Sources/Astryx/Thumbnail/Thumbnail.spec.md +0 -191
@@ -2,23 +2,26 @@
2
2
 
3
3
  import fs from 'node:fs';
4
4
  import path from 'node:path';
5
- import {describe, expect, it} from 'vitest';
5
+ import {describe, expect, it, vi} from 'vitest';
6
6
  import {
7
7
  CORE_DEFAULTS,
8
8
  SWIFT_THEME_DIR,
9
9
  SWIFT_TOKENS_FILE,
10
10
  defineTheme,
11
+ finishRun,
11
12
  localTokensLeftOut,
12
13
  publishedThemePackages,
13
14
  renderAll,
14
15
  renderSwiftTheme,
15
16
  swiftMember,
16
17
  swiftThemeName,
18
+ toColor,
17
19
  toFamilies,
18
- toHexColor,
20
+ toLeading,
19
21
  toPoints,
20
22
  renderTokenTypes,
21
23
  toShadow,
24
+ toWeight,
22
25
  tokenSurface,
23
26
  valueForMode,
24
27
  } from '../../scripts/swift-ios-theme.mjs';
@@ -105,25 +108,128 @@ describe('swift theme generator: names (G6)', () => {
105
108
  });
106
109
  });
107
110
 
111
+ // Every row is exact to the 8-bit channel (tolerance 0 of 255). The lab(),
112
+ // lch(), oklab(), and oklch() rows are CSS Color 4's own examples of #7D2329
113
+ // and #C65D06; the rest agree with an independent CSS Color 4 implementation.
114
+ // Outside Display P3, each channel clips, as Chromium 154 draws it.
115
+ const P3_CLIPPED = 'outside Display P3; clipped to match the web';
116
+ const COLORS: [css: string, swift: string, note?: string][] = [
117
+ ['#0064E0', '#0064E0FF'],
118
+ ['#abc8', '#AABBCC88'],
119
+ ['rebeccapurple', '#663399FF'],
120
+ ['transparent', '#00000000'],
121
+ ['rgba(5, 54, 89, 0.1)', '#0536591A'],
122
+ ['rgb(10% 20% 30% / 50%)', '#1A334D80'],
123
+ ['rgb(none 0 0)', '#000000FF'],
124
+ ['rgb(calc(255 / 2) 0 0)', '#800000FF'],
125
+ ['hsl(210 100% 50%)', '#0080FFFF'],
126
+ ['hsla(120, 100%, 25%, 0.5)', '#00800080'],
127
+ ['hsl(0.5turn 50% 50%)', '#40BFBFFF'],
128
+ ['hsl(200grad 50% 50%)', '#40BFBFFF'],
129
+ ['hsl(3.14159rad 50% 50%)', '#40BFBFFF'],
130
+ ['hwb(194 0% 0% / .5)', '#00C4FF80'],
131
+ ['hwb(0 60% 60%)', '#808080FF'],
132
+ ['lab(29.2345% 39.3825 20.0664)', '#7D2329FF'],
133
+ ['lch(52.2345% 72.2 56.2)', '#C65D06FF'],
134
+ ['oklab(40.101% 0.1147 0.0453)', '#7D2329FF'],
135
+ ['oklch(40.101% 0.12332 21.555)', '#7D2329FF'],
136
+ ['oklch(0 0 0 / 5%)', '#0000000D'],
137
+ ['oklch(50% 0 0)', '#636363FF'],
138
+ ['color(srgb 0.5 0.2 0.1)', '#80331AFF'],
139
+ ['color(srgb-linear 0.5 0.2 0.1)', '#BC7C59FF'],
140
+ ['color(display-p3 0.4 0.6 0.2)', '#559B17FF'],
141
+ ['color(a98-rgb 0.4 0.6 0.2)', '#439A22FF'],
142
+ ['color(prophoto-rgb 0.4 0.6 0.2)', '#52B204FF'],
143
+ // rec2020 decodes with the BT.2020 curve, as Chromium 154 draws it.
144
+ ['color(rec2020 0.4 0.6 0.2)', '#4AA829FF'],
145
+ ['color(rec2020 0.5 0.5 0.5)', '#8B8B8BFF'],
146
+ ['color(xyz-d65 0.2 0.3 0.4)', 'display-p3 #44A4A3FF'],
147
+ ['color(xyz-d50 0.2 0.3 0.4)', 'display-p3 #2CA5BAFF'],
148
+ ['color(display-p3 1 0 0)', 'display-p3 #FF0000FF'],
149
+ ['color-mix(in srgb, red, blue)', '#800080FF'],
150
+ ['color-mix(in srgb, red 30%, blue)', '#4D00B3FF'],
151
+ ['color-mix(in srgb, red 20%, blue 20%)', '#80008066'],
152
+ ['color-mix(in srgb, rgb(255 0 0 / .5), blue)', '#5500AABF'],
153
+ ['color-mix(in srgb, rgb(none 50 0), rgb(10 200 0))', '#0A7D00FF'],
154
+ ['color-mix(in srgb-linear, red, blue)', '#BC00BCFF'],
155
+ ['color-mix(in xyz, red, blue)', '#BC00BCFF'],
156
+ ['color-mix(in display-p3, red, blue)', '#800A91FF'],
157
+ ['color-mix(in oklab, red, blue)', '#8C53A2FF'],
158
+ ['color-mix(red, blue)', '#8C53A2FF'],
159
+ ['color-mix(in lab, red, blue)', 'display-p3 #B01185FF'],
160
+ ['color-mix(in hsl, red, blue)', '#FF00FFFF'],
161
+ ['color-mix(in oklch, red, blue)', 'display-p3 #AA00BBFF', P3_CLIPPED],
162
+ [
163
+ 'color-mix(in oklch longer hue, red, blue)',
164
+ 'display-p3 #009000FF',
165
+ P3_CLIPPED,
166
+ ],
167
+ ['rgb(from #123456 calc(r + 10) g b)', '#1C3456FF'],
168
+ ['rgb(from red r g b / 50%)', '#FF000080'],
169
+ ['hsl(from blue calc(h + 180) s l)', '#FFFF00FF'],
170
+ ['oklch(0.7 0.4 150)', 'display-p3 #00D100FF', P3_CLIPPED],
171
+ ['oklch(0.9 0.4 300)', 'display-p3 #FB7CFFFF', P3_CLIPPED],
172
+ // A relative color keeps its origin's gamut instead of clamping to sRGB.
173
+ ['rgb(from color(display-p3 1 0 0) r g b / 50%)', 'display-p3 #FF000080'],
174
+ ['hwb(from color(display-p3 1 0 0) h w b)', 'display-p3 #FF0000FF'],
175
+ // A missing alpha takes the other color's before premultiplying.
176
+ ['color-mix(in srgb, rgb(255 0 0 / none), rgb(0 0 255 / 0.5))', '#80008080'],
177
+ ['color-mix(in srgb, rgb(255 0 0 / none), rgb(0 0 255 / none))', '#80008000'],
178
+ // A missing blue is not Lab's b: only analogous channels carry over.
179
+ ['color-mix(in lab, rgb(0 0 none), lab(50 20 30))', '#4F3525FF'],
180
+ // HSL lightness is analogous to Lab's, so a missing one carries over.
181
+ ['color-mix(in lab, hsl(0 50% none), lab(50 20 30))', '#8E705EFF'],
182
+ // hsl() saturation clamps at 100% in both syntaxes, as browsers read it.
183
+ ['hsl(120, 200%, 40%)', '#00CC00FF'],
184
+ ['hsl(120 200% 40%)', '#00CC00FF'],
185
+ // Hues are taken modulo 360° before interpolating (400° is 40°).
186
+ ['color-mix(in oklch, oklch(0.6 0.1 400), oklch(0.6 0.1 20))', '#B4685CFF'],
187
+ ];
188
+
108
189
  describe('swift theme generator: values (G7)', () => {
109
- it('reads CSS colors, lengths, and font stacks as Swift values', () => {
110
- expect(toHexColor('#0064E0')).toBe('#0064E0FF');
111
- expect(toHexColor('#fff')).toBe('#FFFFFFFF');
112
- expect(toHexColor('rgba(5, 54, 89, 0.1)')).toBe('#0536591A');
113
- expect(toHexColor('transparent')).toBe('#00000000');
114
- expect(toHexColor('oklch(0 0 0 / 5%)')).toBe('#0000000D');
115
- expect(toHexColor('oklch(1 0 0 / 8%)')).toBe('#FFFFFF14');
116
- expect(toHexColor('oklch(0.5 0 0)')).toBe('#636363FF');
117
- expect(toHexColor('oklch(50% 0 0)')).toBe('#636363FF');
118
- expect(toHexColor('color-mix(in srgb, red, blue)')).toBeNull();
119
- expect(toHexColor('light-dark(#000, #fff)')).toBeNull();
120
- expect(toHexColor('hsl(0 100% 50%)')).toBe('#FF0000FF');
121
- expect(toHexColor('hsl(210 100% 50%)')).toBe('#0080FFFF');
122
- expect(toHexColor('hsla(120, 100%, 25%, 0.5)')).toBe('#00800080');
123
- expect(toPoints('0.875rem')).toBe(14);
124
- expect(toPoints('32px')).toBe(32);
125
- expect(toPoints('600')).toBe(600);
126
- expect(toPoints('calc(1px + 1rem)')).toBeNull();
190
+ it.each(COLORS)('reads the color %s as %s', (css, swift, note) => {
191
+ const notes: string[] = [];
192
+ expect(toColor(css, notes)).toBe(swift);
193
+ expect(notes).toEqual(note ? [note] : []);
194
+ });
195
+
196
+ it('reads lengths, weights, and line heights', () => {
197
+ const lengths: [string, number][] = [
198
+ ['0.875rem', 14],
199
+ ['32px', 32],
200
+ ['12pt', 16],
201
+ ['1in', 96],
202
+ ['0', 0],
203
+ ['calc(1rem + 2px)', 18],
204
+ ['calc((1rem - 2px) * 2)', 28],
205
+ ['max(4px, 0.5rem)', 8],
206
+ ['clamp(2px, 1rem, 12px)', 12],
207
+ ];
208
+ for (const [css, points] of lengths) {
209
+ expect(toPoints(css), css).toBe(points);
210
+ }
211
+ // thin, medium, and thick are lengths only for a border width.
212
+ expect(toPoints('thin', [], {borderWidth: true})).toBe(1);
213
+ expect(toPoints('medium', [], {borderWidth: true})).toBe(3);
214
+ expect(() => toPoints('medium')).toThrow(/names a border width/);
215
+ const notes: string[] = [];
216
+ expect(toPoints('1.5em', notes)).toBe(24);
217
+ expect(notes).toEqual(['em read against the 16px root font size']);
218
+ expect(toWeight('bold')).toBe(700);
219
+ expect(toWeight('normal')).toBe(400);
220
+ expect(toWeight('650')).toBe(650);
221
+ expect(toWeight('bolder', notes)).toBe(700);
222
+ expect(toLeading('1.5', 14)).toBe(1.5);
223
+ expect(toLeading('150%', 14)).toBe(1.5);
224
+ expect(toLeading('21px', 14)).toBe(1.5);
225
+ // An em counts against the style's own size, not the 16-point root.
226
+ expect(toLeading('1.5em', 14)).toBe(1.5);
227
+ expect(toLeading('calc(1em + 7px)', 14)).toBe(1.5);
228
+ expect(toLeading('normal', 14, notes)).toBe(1.2);
229
+ expect(notes.slice(1)).toEqual([
230
+ "bolder read against normal (400), the root's inherited weight",
231
+ 'normal depends on the font; read as 1.2',
232
+ ]);
127
233
  expect(toFamilies('"Figtree", -apple-system, sans-serif')).toEqual([
128
234
  'Figtree',
129
235
  '-apple-system',
@@ -132,51 +238,279 @@ describe('swift theme generator: values (G7)', () => {
132
238
  expect(toFamilies('var(--font-family-body)')).toBeNull();
133
239
  });
134
240
 
135
- it('reads box-shadow lists as drop layers and inset rings (G7)', () => {
241
+ it('fails on a value with no static form, saying why', () => {
242
+ const cases: [() => unknown, RegExp][] = [
243
+ [() => toColor('currentColor'), /currentColor depends on where/],
244
+ [() => toColor('Canvas'), /system color/],
245
+ [() => toColor('var(--brand)'), /var\(--brand\) is not a token/],
246
+ [() => toColor('color(--custom 1 0 0)'), /not a color space/],
247
+ [() => toPoints('50%'), /percentage depends on the box/],
248
+ [() => toPoints('10vh'), /viewport units/],
249
+ [() => toPoints('2ch'), /font-relative units/],
250
+ [() => toPoints('calc(1px + 50%)'), /adds length and percent/],
251
+ [() => toPoints('env(safe-area-inset-top)'), /env\(\) is known only/],
252
+ [() => toWeight('1001'), /from 1 to 1000/],
253
+ [
254
+ () => toShadow('inset 0 1px 2px #000'),
255
+ /SwiftUI draws only inset rings/,
256
+ ],
257
+ ];
258
+ for (const [run, message] of cases) {
259
+ expect(run).toThrow(message);
260
+ }
261
+ });
262
+
263
+ it('reads box-shadow lists as drop layers and inset rings', () => {
136
264
  const neutralLow =
137
265
  '0 2px 4px light-dark(oklch(0 0 0 / 5%), oklch(0 0 0 / 25%)), 0 4px 8px light-dark(oklch(0 0 0 / 10%), oklch(0 0 0 / 40%)), inset 0 0 0 1px light-dark(transparent, oklch(1 0 0 / 8%))';
138
- expect(toShadow(neutralLow, 'light')).toEqual([
266
+ expect(toShadow(valueForMode(neutralLow, 'light'))).toEqual([
139
267
  {x: 0, y: 2, blur: 4, spread: 0, isInset: false, color: '#0000000D'},
140
268
  {x: 0, y: 4, blur: 8, spread: 0, isInset: false, color: '#0000001A'},
141
269
  {x: 0, y: 0, blur: 0, spread: 1, isInset: true, color: '#00000000'},
142
270
  ]);
143
- expect(toShadow(neutralLow, 'dark')?.[2]?.color).toBe('#FFFFFF14');
144
- expect(toShadow('0 2px 4px #4a35200D', 'light')).toEqual([
145
- {x: 0, y: 2, blur: 4, spread: 0, isInset: false, color: '#4A35200D'},
271
+ expect(toShadow(valueForMode(neutralLow, 'dark'))[2]?.color).toBe(
272
+ '#FFFFFF14',
273
+ );
274
+ expect(toShadow('none')).toEqual([]);
275
+ const notes: string[] = [];
276
+ expect(toShadow('#000 0 calc(1px + 1px) 0.25rem 1px', notes)).toEqual([
277
+ {x: 0, y: 2, blur: 4, spread: 0, isInset: false, color: '#000000FF'},
278
+ ]);
279
+ expect(notes).toEqual([
280
+ 'drop layer "#000 0 calc(1px + 1px) 0.25rem 1px" drawn without its spread',
146
281
  ]);
147
- // No exact SwiftUI form: a drop spread, or a blurred or offset inset.
148
- expect(toShadow('0 2px 4px 1px #000', 'light')).toBeNull();
149
- expect(toShadow('inset 0 1px 2px #000', 'light')).toBeNull();
150
282
  });
151
283
 
152
- it('takes light-dark() per mode (G1)', async () => {
284
+ it('takes light-dark() per mode at any depth (G1)', () => {
153
285
  expect(valueForMode('light-dark(#000, #fff)', 'light')).toBe('#000');
154
286
  expect(valueForMode('light-dark(#000, #fff)', 'dark')).toBe('#fff');
155
- expect(
156
- valueForMode('light-dark(rgb(0, 0, 0), rgb(255, 255, 255))', 'dark'),
157
- ).toBe('rgb(255, 255, 255)');
287
+ const mixed = 'color-mix(in srgb, light-dark(red, blue), white)';
288
+ expect(toColor(valueForMode(mixed, 'light'))).toBe('#FF8080FF');
289
+ expect(toColor(valueForMode(mixed, 'dark'))).toBe('#8080FFFF');
158
290
  expect(valueForMode('#123', 'dark')).toBe('#123');
291
+ });
292
+
293
+ it('generates a [light, dark] tuple and a light-dark() value per mode', async () => {
159
294
  const theme = await defineTheme({
160
295
  name: 'acme-brand',
161
- tokens: {'--color-accent': 'light-dark(#000, #fff)'},
296
+ tokens: {
297
+ '--color-accent': 'light-dark(#000, #fff)',
298
+ '--color-border': [
299
+ 'oklch(40.101% 0.12332 21.555)',
300
+ 'color-mix(in srgb, light-dark(red, blue) 50%, white)',
301
+ ],
302
+ '--spacing-4': ['4px', '0.75rem'],
303
+ },
162
304
  });
163
305
  const swift = await renderSwiftTheme(theme);
164
- expect(swift).toContain(
306
+ const [light, dark] = swift.split('dark: Tokens(');
307
+ expect(light).toContain(
165
308
  'accent: Color(.sRGB, red: 0, green: 0, blue: 0, opacity: 1)',
166
309
  );
167
- expect(swift).toContain(
310
+ expect(dark).toContain(
168
311
  'accent: Color(.sRGB, red: 1, green: 1, blue: 1, opacity: 1)',
169
312
  );
313
+ expect(light).toContain(
314
+ 'border: Color(.sRGB, red: 0.49019607843137253, green: 0.13725490196078433, blue: 0.1607843137254902, opacity: 1)',
315
+ );
316
+ expect(dark).toContain(
317
+ 'border: Color(.sRGB, red: 0.5019607843137255, green: 0.5019607843137255, blue: 1, opacity: 1)',
318
+ );
319
+ expect(light).toContain('s4: 4,');
320
+ expect(dark).toContain('s4: 12,');
321
+ });
322
+
323
+ // A value with no static form takes core's default and warns (G15).
324
+ it.each([
325
+ ['--font-family-body', 'inherit', 'inherit depends on'],
326
+ ['--font-family-body', '"Figtree", env(x)', 'env() is known'],
327
+ ['--shadow-low', 'inherit', 'inherit depends on'],
328
+ ['--shadow-low', '0 1px 2px', 'draws in currentColor'],
329
+ ])('falls back from %s: %s, warning why', async (token, value, reason) => {
330
+ const theme = await defineTheme({name: 'acme', tokens: {[token]: value}});
331
+ const warnings: string[] = [];
332
+ await renderSwiftTheme(theme, {warnings});
333
+ expect(warnings).toHaveLength(2);
334
+ for (const warning of warnings) {
335
+ expect(warning).toContain(reason);
336
+ expect(warning).toContain("core's default");
337
+ }
338
+ });
339
+
340
+ // A form the reader does not read fails, saying so (G7).
341
+ it.each([
342
+ ['--font-family-body', '"Figtree", inherit', 'cannot be one name'],
343
+ ['--color-accent', 'color(display-p3-linear 1 0 0)', 'not a color'],
344
+ ])(
345
+ 'fails %s: %s as a form it does not read',
346
+ async (token, value, reason) => {
347
+ const theme = await defineTheme({name: 'acme', tokens: {[token]: value}});
348
+ const {message} = await renderSwiftTheme(theme, {warnings: []}).catch(
349
+ error => error,
350
+ );
351
+ expect(message).toContain(reason);
352
+ expect(
353
+ message.endsWith('; the Swift theme generator does not read this form'),
354
+ ).toBe(true);
355
+ },
356
+ );
357
+
358
+ it('reads + and - in calc() only with a space on each side, as CSS does', () => {
359
+ expect(toPoints('calc(1rem - 2px)')).toBe(14);
360
+ expect(() => toPoints('calc(4px+2px)')).toThrow(/space on each side of \+/);
361
+ expect(() => toPoints('calc(1rem-2px)')).toThrow(/space on each side of -/);
362
+ });
363
+
364
+ it.each([
365
+ ['hwb(0, 0%, 0%)', /no comma syntax/],
366
+ ['rgb(255, none, 0)', /cannot hold none/],
367
+ ['rgba(255, 0, 0, none)', /cannot hold none/],
368
+ ['rgb(255, 50%, 0)', /all numbers or all percentages/],
369
+ ['hsl(120, 50, 40)', /saturation and lightness as percentages/],
370
+ ])('fails the comma syntax CSS does not allow: %s', (css, reason) => {
371
+ expect(() => toColor(css)).toThrow(reason);
372
+ });
373
+
374
+ it("writes a color outside sRGB as SwiftUI's Display P3 color", async () => {
375
+ const theme = await defineTheme({
376
+ name: 'acme-brand',
377
+ tokens: {'--color-accent': 'color(display-p3 1 0 0 / 50%)'},
378
+ });
379
+ expect(await renderSwiftTheme(theme)).toContain(
380
+ 'accent: Color(.displayP3, red: 1, green: 0, blue: 0, opacity: 0.5019607843137255)',
381
+ );
382
+ });
383
+
384
+ it("keeps a relative color in its origin's gamut through core's var()", async () => {
385
+ const theme = await defineTheme({
386
+ name: 'acme-brand',
387
+ tokens: {
388
+ '--color-accent': 'color(display-p3 1 0 0)',
389
+ '--color-border': 'rgb(from var(--color-accent) r g b / 50%)',
390
+ },
391
+ });
392
+ expect(await renderSwiftTheme(theme)).toContain(
393
+ 'border: Color(.displayP3, red: 1, green: 0, blue: 0, opacity: 0.5019607843137255)',
394
+ );
395
+ });
396
+
397
+ it('turns a length line height into a multiplier of its style size', async () => {
398
+ const theme = await defineTheme({
399
+ name: 'acme-brand',
400
+ tokens: {'--text-body-size': '16px', '--text-body-leading': '20px'},
401
+ });
402
+ expect(await renderSwiftTheme(theme)).toMatch(
403
+ /body: TypeStyleTokens\(size: 16, weight: \d+, leading: 1\.25\)/,
404
+ );
405
+ });
406
+ });
407
+
408
+ describe('swift theme generator: diagnostics (G15)', () => {
409
+ it('warns on a lossy value with theme, mode, token, value, and output', async () => {
410
+ const theme = await defineTheme({
411
+ name: 'acme-brand',
412
+ tokens: {'--color-accent': 'oklch(0.7 0.4 150)'},
413
+ });
414
+ const warnings: string[] = [];
415
+ const swift = await renderSwiftTheme(theme, {warnings});
416
+ expect(swift).toContain(
417
+ 'accent: Color(.displayP3, red: 0, green: 0.8196078431372549, blue: 0, opacity: 1)',
418
+ );
419
+ expect(warnings).toEqual([
420
+ `acme-brand light: --color-accent = oklch(0.7 0.4 150) → Color(.displayP3, red: 0, green: 0.8196078431372549, blue: 0, opacity: 1): ${P3_CLIPPED}`,
421
+ `acme-brand dark: --color-accent = oklch(0.7 0.4 150) → Color(.displayP3, red: 0, green: 0.8196078431372549, blue: 0, opacity: 1): ${P3_CLIPPED}`,
422
+ ]);
170
423
  });
171
424
 
172
- it('fails on a value Swift cannot read, naming theme, mode, token, and value', async () => {
425
+ it("takes core's default for a value with no static form, with a warning", async () => {
173
426
  const theme = await defineTheme({
174
427
  name: 'acme-brand',
175
- tokens: {'--color-accent': 'color-mix(in srgb, red, blue)'},
428
+ tokens: {'--spacing-4': '2vw'},
429
+ });
430
+ const warnings: string[] = [];
431
+ const swift = await renderSwiftTheme(theme, {warnings});
432
+ const core = await renderSwiftTheme(CORE_DEFAULTS);
433
+ const s4 = (text: string) => /s4: [\d.]+/.exec(text)?.[0];
434
+ expect(s4(swift)).toBe(s4(core));
435
+ expect(warnings[0]).toMatch(
436
+ /^acme-brand light: --spacing-4 = 2vw → \d+: viewport units depend on the screen; core's default \S+ used instead$/,
437
+ );
438
+ });
439
+
440
+ it('fails when a value has no static form and core has no default', async () => {
441
+ const theme = await defineTheme({
442
+ name: 'acme-brand',
443
+ tokens: {'--color-acme-only': 'currentColor'},
176
444
  });
177
445
  await expect(renderSwiftTheme(theme)).rejects.toThrow(
178
- 'acme-brand light: --color-accent is not a color Swift can read: color-mix(in srgb, red, blue)',
446
+ 'acme-brand light: --color-acme-only = currentColor: currentColor depends on where the token is used; Swift has no static value',
447
+ );
448
+ });
449
+
450
+ it('prints warnings to standard error when no list is passed', async () => {
451
+ const theme = await defineTheme({
452
+ name: 'acme-brand',
453
+ tokens: {'--color-accent': 'oklch(0.7 0.4 150)'},
454
+ });
455
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
456
+ try {
457
+ await renderSwiftTheme(theme);
458
+ expect(warn).toHaveBeenCalledTimes(2);
459
+ expect(warn.mock.calls[0][0]).toMatch(
460
+ /^warning: acme-brand light: --color-accent = oklch\(0\.7 0\.4 150\) → /,
461
+ );
462
+ } finally {
463
+ warn.mockRestore();
464
+ }
465
+ });
466
+
467
+ it("drops a failed value's notes before falling back to core's default", async () => {
468
+ const theme = await defineTheme({
469
+ name: 'acme-brand',
470
+ tokens: {'--spacing-4': 'calc(1em + 2vw)'},
471
+ });
472
+ const warnings: string[] = [];
473
+ await renderSwiftTheme(theme, {warnings});
474
+ expect(warnings).toHaveLength(2);
475
+ for (const warning of warnings) {
476
+ expect(warning).toMatch(
477
+ /viewport units depend on the screen; core's default/,
478
+ );
479
+ expect(warning).not.toMatch(/em read/);
480
+ }
481
+ });
482
+
483
+ it("prints a token's note once per mode", async () => {
484
+ const theme = await defineTheme({
485
+ name: 'acme-brand',
486
+ tokens: {'--spacing-4': 'calc(1em + 1em)'},
487
+ });
488
+ const warnings: string[] = [];
489
+ await renderSwiftTheme(theme, {warnings});
490
+ expect(warnings).toEqual([
491
+ 'acme-brand light: --spacing-4 = calc(1em + 1em) → 32: em read against the 16px root font size',
492
+ 'acme-brand dark: --spacing-4 = calc(1em + 1em) → 32: em read against the 16px root font size',
493
+ ]);
494
+ });
495
+
496
+ it('turns warnings into errors with --strict, before anything is written', async () => {
497
+ const theme = await defineTheme({
498
+ name: 'acme-brand',
499
+ tokens: {'--color-accent': 'oklch(0.7 0.4 150)'},
500
+ });
501
+ await expect(renderSwiftTheme(theme, {strict: true})).rejects.toThrow(
502
+ /2 warning\(s\), which --strict treats as errors:\nerror: acme-brand light: --color-accent/,
503
+ );
504
+ let wrote = false;
505
+ const write = () => {
506
+ wrote = true;
507
+ };
508
+ expect(() => finishRun(['a warning'], true, write)).toThrow(
509
+ /error: a warning/,
179
510
  );
511
+ expect(wrote).toBe(false);
512
+ finishRun([], true, write);
513
+ expect(wrote).toBe(true);
180
514
  });
181
515
  });
182
516