Skip to content

Commit 8266929

Browse files
committed
error if provided version is leq stored version
1 parent f0d59dc commit 8266929

File tree

2 files changed

+64
-38
lines changed

2 files changed

+64
-38
lines changed

packages/internal/src/utils/__tests__/version-utils.test.ts

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77
parseVersion,
88
stringifyVersion,
99
incrementPatchVersion,
10-
getMaximumVersion,
10+
isGreater,
1111
getLatestAgentVersion,
1212
determineNextVersion,
1313
versionExists,
@@ -90,32 +90,33 @@ describe('version-utils', () => {
9090
})
9191
})
9292

93-
describe('getMaximumVersion', () => {
94-
it('should return version with higher major version', () => {
93+
describe('isGreater', () => {
94+
it('should return true when first version has higher major version', () => {
9595
const v1 = { major: 2, minor: 0, patch: 0 }
9696
const v2 = { major: 1, minor: 9, patch: 9 }
97-
expect(getMaximumVersion(v1, v2)).toEqual(v1)
98-
expect(getMaximumVersion(v2, v1)).toEqual(v1)
97+
expect(isGreater(v1, v2)).toBe(true)
98+
expect(isGreater(v2, v1)).toBe(false)
9999
})
100100

101-
it('should return version with higher minor version when major is same', () => {
101+
it('should return true when first version has higher minor version and same major', () => {
102102
const v1 = { major: 1, minor: 2, patch: 0 }
103103
const v2 = { major: 1, minor: 1, patch: 9 }
104-
expect(getMaximumVersion(v1, v2)).toEqual(v1)
105-
expect(getMaximumVersion(v2, v1)).toEqual(v1)
104+
expect(isGreater(v1, v2)).toBe(true)
105+
expect(isGreater(v2, v1)).toBe(false)
106106
})
107107

108-
it('should return version with higher patch version when major and minor are same', () => {
108+
it('should return true when first version has higher patch version and same major/minor', () => {
109109
const v1 = { major: 1, minor: 2, patch: 4 }
110110
const v2 = { major: 1, minor: 2, patch: 3 }
111-
expect(getMaximumVersion(v1, v2)).toEqual(v1)
112-
expect(getMaximumVersion(v2, v1)).toEqual(v1)
111+
expect(isGreater(v1, v2)).toBe(true)
112+
expect(isGreater(v2, v1)).toBe(false)
113113
})
114114

115-
it('should return first version when versions are equal', () => {
115+
it('should return false when versions are equal', () => {
116116
const v1 = { major: 1, minor: 2, patch: 3 }
117117
const v2 = { major: 1, minor: 2, patch: 3 }
118-
expect(getMaximumVersion(v1, v2)).toEqual(v1)
118+
expect(isGreater(v1, v2)).toBe(false)
119+
expect(isGreater(v2, v1)).toBe(false)
119120
})
120121
})
121122

@@ -201,7 +202,7 @@ describe('version-utils', () => {
201202
expect(result).toEqual({ major: 1, minor: 2, patch: 4 })
202203
})
203204

204-
it('should use provided version when higher than incremented latest', async () => {
205+
it('should use provided version when higher than latest', async () => {
205206
spyOn(versionUtils, 'getLatestAgentVersion').mockResolvedValue({
206207
major: 0,
207208
minor: 0,
@@ -216,19 +217,32 @@ describe('version-utils', () => {
216217
expect(result).toEqual({ major: 2, minor: 0, patch: 0 })
217218
})
218219

219-
it('should use maximum of latest and provided version', async () => {
220+
it('should throw error when provided version is not greater than latest', async () => {
220221
spyOn(versionUtils, 'getLatestAgentVersion').mockResolvedValue({
221222
major: 2,
222223
minor: 0,
223224
patch: 0,
224225
})
225226

226-
const result = await determineNextVersion(
227-
'test-agent',
228-
'test-publisher',
229-
'1.5.0',
227+
await expect(
228+
determineNextVersion('test-agent', 'test-publisher', '1.5.0'),
229+
).rejects.toThrow(
230+
'Provided version 1.5.0 must be greater than the latest version (2.0.0)',
231+
)
232+
})
233+
234+
it('should throw error when provided version equals latest', async () => {
235+
spyOn(versionUtils, 'getLatestAgentVersion').mockResolvedValue({
236+
major: 1,
237+
minor: 5,
238+
patch: 0,
239+
})
240+
241+
await expect(
242+
determineNextVersion('test-agent', 'test-publisher', '1.5.0'),
243+
).rejects.toThrow(
244+
'Provided version 1.5.0 must be greater than the latest version (1.5.0)',
230245
)
231-
expect(result).toEqual({ major: 2, minor: 0, patch: 1 })
232246
})
233247

234248
it('should throw error for invalid provided version', async () => {
@@ -297,19 +311,21 @@ describe('version-utils', () => {
297311
// Test the complete flow of version operations
298312
const version1 = parseVersion('1.2.3')
299313
const version2 = parseVersion('1.2.4')
300-
const maxVersion = getMaximumVersion(version1, version2)
301-
const nextVersion = incrementPatchVersion(maxVersion)
314+
const isV2Greater = isGreater(version2, version1)
315+
const nextVersion = incrementPatchVersion(version2)
302316
const versionString = stringifyVersion(nextVersion)
303317

318+
expect(isV2Greater).toBe(true)
304319
expect(versionString).toBe('1.2.5')
305320
})
306321

307322
it('should handle edge cases with versionOne', () => {
308323
const one = versionOne()
309324
const incremented = incrementPatchVersion(one)
310-
const max = getMaximumVersion(one, incremented)
325+
const isIncrementedGreater = isGreater(incremented, one)
311326

312-
expect(max).toEqual({ major: 0, minor: 0, patch: 2 })
327+
expect(isIncrementedGreater).toBe(true)
328+
expect(incremented).toEqual({ major: 0, minor: 0, patch: 2 })
313329
})
314330
})
315331
})

packages/internal/src/utils/version-utils.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ export function incrementPatchVersion(version: Version): Version {
3434
return { ...version, patch: version.patch + 1 }
3535
}
3636

37-
export function getMaximumVersion(v1: Version, v2: Version): Version {
37+
export function isGreater(v1: Version, v2: Version): boolean {
3838
if (v1.major !== v2.major) {
39-
return v1.major > v2.major ? v1 : v2
39+
return v1.major > v2.major
4040
}
4141

4242
if (v1.minor !== v2.minor) {
43-
return v1.minor > v2.minor ? v1 : v2
43+
return v1.minor > v2.minor
4444
}
4545

46-
return v1.patch > v2.patch ? v1 : v2
46+
return v1.patch > v2.patch
4747
}
4848

4949
/**
@@ -94,18 +94,28 @@ export async function determineNextVersion(
9494
): Promise<Version> {
9595
const latestVersion = await getLatestAgentVersion(agentId, publisherId)
9696

97-
let version: Version = versionOne()
98-
if (providedVersion) {
99-
try {
100-
version = parseVersion(providedVersion)
101-
} catch (error) {
102-
throw new Error(
103-
`Invalid version format: ${providedVersion}. Must be in semver format (e.g., 1.0.0)`,
104-
)
105-
}
97+
if (!providedVersion) {
98+
return incrementPatchVersion(latestVersion)
10699
}
107100

108-
return getMaximumVersion(incrementPatchVersion(latestVersion), version)
101+
let version: Version
102+
try {
103+
version = parseVersion(providedVersion)
104+
} catch (error) {
105+
throw new Error(
106+
`Invalid version format: ${providedVersion}. Must be in semver format (e.g., 1.0.0)`,
107+
)
108+
}
109+
110+
if (!isGreater(version, latestVersion)) {
111+
throw new Error(
112+
`Provided version ${providedVersion} must be greater than the latest version (${stringifyVersion(
113+
latestVersion,
114+
)})`,
115+
)
116+
}
117+
118+
return version
109119
}
110120

111121
/**

0 commit comments

Comments
 (0)