1+ import { describe , test , expect , beforeEach } from 'vitest' ;
2+ import Ajv from 'ajv' ;
3+ import { sharedSchema } from './shared.schema' ;
4+
5+ describe ( 'shared schema validation' , ( ) => {
6+ let ajv : Ajv ;
7+
8+ beforeEach ( ( ) => {
9+ ajv = new Ajv ( { strict : false } ) ;
10+ } ) ;
11+
12+ describe ( 'Token validation' , ( ) => {
13+ test ( 'accepts valid secret token format' , ( ) => {
14+ const tokenSchema = sharedSchema . definitions ! . Token ;
15+ const validate = ajv . compile ( tokenSchema ) ;
16+
17+ const validToken = { secret : 'my-secret-name' } ;
18+ const isValid = validate ( validToken ) ;
19+
20+ expect ( isValid ) . toBe ( true ) ;
21+ expect ( validate . errors ) . toBeNull ( ) ;
22+ } ) ;
23+
24+ test ( 'accepts valid environment variable token format' , ( ) => {
25+ const tokenSchema = sharedSchema . definitions ! . Token ;
26+ const validate = ajv . compile ( tokenSchema ) ;
27+
28+ const validToken = { env : 'MY_TOKEN_VAR' } ;
29+ const isValid = validate ( validToken ) ;
30+
31+ expect ( isValid ) . toBe ( true ) ;
32+ expect ( validate . errors ) . toBeNull ( ) ;
33+ } ) ;
34+
35+ test ( 'rejects string tokens (security measure)' , ( ) => {
36+ const tokenSchema = sharedSchema . definitions ! . Token ;
37+ const validate = ajv . compile ( tokenSchema ) ;
38+
39+ const stringToken = 'direct-string-token' ;
40+ const isValid = validate ( stringToken ) ;
41+
42+ expect ( isValid ) . toBe ( false ) ;
43+ expect ( validate . errors ) . toBeTruthy ( ) ;
44+ expect ( validate . errors ! [ 0 ] . message ) . toContain ( 'must be object' ) ;
45+ } ) ;
46+
47+ test ( 'rejects empty string tokens' , ( ) => {
48+ const tokenSchema = sharedSchema . definitions ! . Token ;
49+ const validate = ajv . compile ( tokenSchema ) ;
50+
51+ const emptyStringToken = '' ;
52+ const isValid = validate ( emptyStringToken ) ;
53+
54+ expect ( isValid ) . toBe ( false ) ;
55+ expect ( validate . errors ) . toBeTruthy ( ) ;
56+ } ) ;
57+
58+ test ( 'rejects malformed token objects' , ( ) => {
59+ const tokenSchema = sharedSchema . definitions ! . Token ;
60+ const validate = ajv . compile ( tokenSchema ) ;
61+
62+ const malformedToken = { invalid : 'format' } ;
63+ const isValid = validate ( malformedToken ) ;
64+
65+ expect ( isValid ) . toBe ( false ) ;
66+ expect ( validate . errors ) . toBeTruthy ( ) ;
67+ } ) ;
68+
69+ test ( 'rejects token objects with both secret and env' , ( ) => {
70+ const tokenSchema = sharedSchema . definitions ! . Token ;
71+ const validate = ajv . compile ( tokenSchema ) ;
72+
73+ const invalidToken = { secret : 'my-secret' , env : 'MY_VAR' } ;
74+ const isValid = validate ( invalidToken ) ;
75+
76+ expect ( isValid ) . toBe ( false ) ;
77+ expect ( validate . errors ) . toBeTruthy ( ) ;
78+ } ) ;
79+
80+ test ( 'rejects empty secret name (security measure)' , ( ) => {
81+ const tokenSchema = sharedSchema . definitions ! . Token ;
82+ const validate = ajv . compile ( tokenSchema ) ;
83+
84+ const tokenWithEmptySecret = { secret : '' } ;
85+ const isValid = validate ( tokenWithEmptySecret ) ;
86+
87+ expect ( isValid ) . toBe ( false ) ;
88+ expect ( validate . errors ) . toBeTruthy ( ) ;
89+ } ) ;
90+
91+ test ( 'rejects empty environment variable name (security measure)' , ( ) => {
92+ const tokenSchema = sharedSchema . definitions ! . Token ;
93+ const validate = ajv . compile ( tokenSchema ) ;
94+
95+ const tokenWithEmptyEnv = { env : '' } ;
96+ const isValid = validate ( tokenWithEmptyEnv ) ;
97+
98+ expect ( isValid ) . toBe ( false ) ;
99+ expect ( validate . errors ) . toBeTruthy ( ) ;
100+ } ) ;
101+
102+ test ( 'rejects token objects with additional properties' , ( ) => {
103+ const tokenSchema = sharedSchema . definitions ! . Token ;
104+ const validate = ajv . compile ( tokenSchema ) ;
105+
106+ const invalidToken = { secret : 'my-secret' , extra : 'property' } ;
107+ const isValid = validate ( invalidToken ) ;
108+
109+ expect ( isValid ) . toBe ( false ) ;
110+ expect ( validate . errors ) . toBeTruthy ( ) ;
111+ } ) ;
112+ } ) ;
113+
114+ describe ( 'GitRevisions validation' , ( ) => {
115+ test ( 'accepts valid GitRevisions object' , ( ) => {
116+ const revisionsSchema = sharedSchema . definitions ! . GitRevisions ;
117+ const validate = ajv . compile ( revisionsSchema ) ;
118+
119+ const validRevisions = {
120+ branches : [ 'main' , 'develop' ] ,
121+ tags : [ 'v1.0.0' , 'latest' ]
122+ } ;
123+ const isValid = validate ( validRevisions ) ;
124+
125+ expect ( isValid ) . toBe ( true ) ;
126+ expect ( validate . errors ) . toBeNull ( ) ;
127+ } ) ;
128+
129+ test ( 'accepts empty GitRevisions object' , ( ) => {
130+ const revisionsSchema = sharedSchema . definitions ! . GitRevisions ;
131+ const validate = ajv . compile ( revisionsSchema ) ;
132+
133+ const emptyRevisions = { } ;
134+ const isValid = validate ( emptyRevisions ) ;
135+
136+ expect ( isValid ) . toBe ( true ) ;
137+ expect ( validate . errors ) . toBeNull ( ) ;
138+ } ) ;
139+
140+ test ( 'rejects GitRevisions with additional properties' , ( ) => {
141+ const revisionsSchema = sharedSchema . definitions ! . GitRevisions ;
142+ const validate = ajv . compile ( revisionsSchema ) ;
143+
144+ const invalidRevisions = {
145+ branches : [ 'main' ] ,
146+ tags : [ 'v1.0.0' ] ,
147+ invalid : 'property'
148+ } ;
149+ const isValid = validate ( invalidRevisions ) ;
150+
151+ expect ( isValid ) . toBe ( false ) ;
152+ expect ( validate . errors ) . toBeTruthy ( ) ;
153+ } ) ;
154+ } ) ;
155+ } ) ;
0 commit comments