|
1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
2 | 2 | import fs, { Stats } from 'fs' |
3 | 3 | import { Readable } from 'stream' |
4 | | -import { Instructions, SignatureOptions } from '../src/schemas.js' |
| 4 | +import type { Instructions, SignatureOptions } from '../src/schemas.js' |
| 5 | +import { BaseWatermarkPropertiesSchema, FilePartSchema } from '../src/schemas.js' |
5 | 6 | import { config as dotenvConfig } from 'dotenv' |
6 | 7 | import { performBuildCall } from '../src/dws/build.js' |
7 | 8 | import { performSignCall } from '../src/dws/sign.js' |
@@ -39,6 +40,121 @@ function createMockStream(content: string | Buffer): Readable { |
39 | 40 | return readable |
40 | 41 | } |
41 | 42 |
|
| 43 | +describe('Schema validation', () => { |
| 44 | + describe('HTMLLayoutSchema / FilePartSchema', () => { |
| 45 | + it('accepts a valid layout with orientation, size, and margin', () => { |
| 46 | + const result = FilePartSchema.safeParse({ |
| 47 | + file: '/test.html', |
| 48 | + layout: { |
| 49 | + orientation: 'portrait', |
| 50 | + size: { |
| 51 | + width: 210, |
| 52 | + height: 297, |
| 53 | + }, |
| 54 | + margin: { |
| 55 | + left: 10, |
| 56 | + top: 10, |
| 57 | + right: 10, |
| 58 | + bottom: 10, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }) |
| 62 | + |
| 63 | + expect(result.success).toBe(true) |
| 64 | + }) |
| 65 | + |
| 66 | + it('allows omitting layout entirely', () => { |
| 67 | + const result = FilePartSchema.safeParse({ file: '/test.html' }) |
| 68 | + expect(result.success).toBe(true) |
| 69 | + }) |
| 70 | + |
| 71 | + it('rejects negative margins', () => { |
| 72 | + const result = FilePartSchema.safeParse({ |
| 73 | + file: '/test.html', |
| 74 | + layout: { |
| 75 | + margin: { |
| 76 | + left: -1, |
| 77 | + top: 0, |
| 78 | + right: 0, |
| 79 | + bottom: 0, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }) |
| 83 | + |
| 84 | + expect(result.success).toBe(false) |
| 85 | + }) |
| 86 | + |
| 87 | + it('rejects zero or negative custom page sizes', () => { |
| 88 | + const zeroWidth = FilePartSchema.safeParse({ |
| 89 | + file: '/test.html', |
| 90 | + layout: { |
| 91 | + size: { |
| 92 | + width: 0, |
| 93 | + height: 100, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }) |
| 97 | + |
| 98 | + const negativeHeight = FilePartSchema.safeParse({ |
| 99 | + file: '/test.html', |
| 100 | + layout: { |
| 101 | + size: { |
| 102 | + width: 100, |
| 103 | + height: -1, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }) |
| 107 | + |
| 108 | + expect(zeroWidth.success).toBe(false) |
| 109 | + expect(negativeHeight.success).toBe(false) |
| 110 | + }) |
| 111 | + }) |
| 112 | + |
| 113 | + describe('Watermark positioning and font fields', () => { |
| 114 | + it('accepts valid positioning and font fields', () => { |
| 115 | + const result = BaseWatermarkPropertiesSchema.safeParse({ |
| 116 | + type: 'watermark', |
| 117 | + watermarkType: 'text', |
| 118 | + width: 100, |
| 119 | + height: 50, |
| 120 | + top: 10, |
| 121 | + right: '5%', |
| 122 | + bottom: 12, |
| 123 | + left: 8, |
| 124 | + text: 'DRAFT', |
| 125 | + fontFamily: 'Helvetica', |
| 126 | + fontSize: 12, |
| 127 | + fontStyle: ['bold', 'italic'], |
| 128 | + }) |
| 129 | + |
| 130 | + expect(result.success).toBe(true) |
| 131 | + }) |
| 132 | + |
| 133 | + it('rejects invalid fontSize values (0 or negative)', () => { |
| 134 | + const zero = BaseWatermarkPropertiesSchema.safeParse({ |
| 135 | + type: 'watermark', |
| 136 | + watermarkType: 'text', |
| 137 | + width: 100, |
| 138 | + height: 50, |
| 139 | + text: 'DRAFT', |
| 140 | + fontSize: 0, |
| 141 | + }) |
| 142 | + |
| 143 | + const negative = BaseWatermarkPropertiesSchema.safeParse({ |
| 144 | + type: 'watermark', |
| 145 | + watermarkType: 'text', |
| 146 | + width: 100, |
| 147 | + height: 50, |
| 148 | + text: 'DRAFT', |
| 149 | + fontSize: -1, |
| 150 | + }) |
| 151 | + |
| 152 | + expect(zero.success).toBe(false) |
| 153 | + expect(negative.success).toBe(false) |
| 154 | + }) |
| 155 | + }) |
| 156 | +}) |
| 157 | + |
42 | 158 | describe('API Functions', () => { |
43 | 159 | const originalEnv = process.env |
44 | 160 |
|
|
0 commit comments