From 654a6bf732246d112aefd61127dbc3e627546379 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Wed, 26 Aug 2026 00:36:22 +0000 Subject: [PATCH] [Tests] Add unit tests for theme factories Add unit tests for `buildTheme`, `buildChecksum`, and `buildThemeAsset` in `packages/cli-kit/src/public/node/themes/factories.test.ts`. --- .../src/public/node/themes/factories.test.ts | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 packages/cli-kit/src/public/node/themes/factories.test.ts diff --git a/packages/cli-kit/src/public/node/themes/factories.test.ts b/packages/cli-kit/src/public/node/themes/factories.test.ts new file mode 100644 index 00000000000..ade9266edeb --- /dev/null +++ b/packages/cli-kit/src/public/node/themes/factories.test.ts @@ -0,0 +1,110 @@ +import {buildTheme, buildChecksum, buildThemeAsset} from './factories.js' +import {describe, test, expect} from 'vitest' + +describe('buildTheme', () => { + test('returns undefined when themeJson is undefined', () => { + expect(buildTheme()).toBeUndefined() + }) + + test('transforms main role to live role', () => { + const theme = buildTheme({ + id: 1, + name: 'Main Theme', + role: 'main', + }) + + expect(theme).toEqual({ + id: 1, + name: 'Main Theme', + role: 'live', + processing: false, + createdAtRuntime: false, + }) + }) + + test('preserves non-main roles and custom flags', () => { + const theme = buildTheme({ + id: 2, + name: 'Dev Theme', + role: 'development', + processing: true, + createdAtRuntime: true, + }) + + expect(theme).toEqual({ + id: 2, + name: 'Dev Theme', + role: 'development', + processing: true, + createdAtRuntime: true, + }) + }) +}) + +describe('buildChecksum', () => { + test('returns undefined when asset is undefined', () => { + expect(buildChecksum()).toBeUndefined() + }) + + test('extracts key and checksum from asset', () => { + const checksum = buildChecksum({ + key: 'assets/app.js', + checksum: '12345', + attachment: undefined, + value: 'console.log("hello")', + }) + + expect(checksum).toEqual({ + key: 'assets/app.js', + checksum: '12345', + }) + }) +}) + +describe('buildThemeAsset', () => { + test('returns undefined when asset is undefined', () => { + expect(buildThemeAsset(undefined)).toBeUndefined() + }) + + test('calculates size stats from string value', () => { + const value = 'body { color: red; }' + const asset = buildThemeAsset({ + key: 'assets/style.css', + checksum: 'abc', + attachment: undefined, + value, + }) + + expect(asset).toEqual({ + key: 'assets/style.css', + checksum: 'abc', + attachment: undefined, + value, + stats: { + size: value.length, + mtime: expect.any(Number), + }, + }) + }) + + test('calculates size stats from attachment when value is empty', () => { + const attachment = 'base64string==' + const asset = buildThemeAsset({ + key: 'assets/logo.png', + checksum: 'def', + attachment, + value: '', + }) + + expect(asset).toEqual({ + key: 'assets/logo.png', + checksum: 'def', + attachment, + value: '', + stats: { + size: attachment.length, + mtime: expect.any(Number), + }, + }) + }) +})