Skip to content

Commit 7055038

Browse files
committed
test: add tests
1 parent d6ce919 commit 7055038

File tree

1 file changed

+114
-1
lines changed

1 file changed

+114
-1
lines changed

src/unityChangeset.test.ts

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@ import {
33
assertEquals,
44
assertNotEquals,
55
assertRejects,
6+
assertThrows,
67
} from "std/testing/asserts";
78
import {
89
filterChangesets,
910
getUnityChangeset,
1011
groupChangesets,
12+
listChangesets,
1113
searchChangesets,
1214
searchChangesetsFromDb,
1315
GroupMode,
16+
OutputMode,
17+
FormatMode,
1418
SearchMode,
1519
UnityChangeset,
1620
UnityReleaseEntitlement,
1721
UnityReleaseStream,
1822
} from "./index.ts";
19-
import { searchModeToStreams } from "./utils.ts";
23+
import { searchModeToStreams, sanitizeVersion, validateFilterOptions, groupBy } from "./utils.ts";
2024

2125
Deno.test("UnityChangeset.toNumber min", () => {
2226
assertEquals(UnityChangeset.toNumber("2018.3", false), 201803000000);
@@ -26,6 +30,42 @@ Deno.test("UnityChangeset.toNumber max", () => {
2630
assertEquals(UnityChangeset.toNumber("2018.3", true), 201803992599);
2731
});
2832

33+
Deno.test("UnityChangeset constructor", () => {
34+
const changeset = new UnityChangeset("2018.3.0f1", "abc123");
35+
assertEquals(changeset.version, "2018.3.0f1");
36+
assertEquals(changeset.changeset, "abc123");
37+
assertEquals(changeset.stream, UnityReleaseStream.UNDEFINED);
38+
assertEquals(changeset.entitlements, []);
39+
assertEquals(changeset.lts, false);
40+
assertEquals(changeset.xlts, false);
41+
});
42+
43+
Deno.test("UnityChangeset constructor with LTS", () => {
44+
const changeset = new UnityChangeset("2018.4.0f1", "abc123", UnityReleaseStream.LTS);
45+
assertEquals(changeset.stream, UnityReleaseStream.LTS);
46+
assertEquals(changeset.lts, true);
47+
assertEquals(changeset.xlts, false);
48+
});
49+
50+
Deno.test("UnityChangeset constructor with XLTS", () => {
51+
const changeset = new UnityChangeset("2018.4.0f1", "abc123", UnityReleaseStream.LTS, [UnityReleaseEntitlement.XLTS]);
52+
assertEquals(changeset.stream, UnityReleaseStream.LTS);
53+
assertEquals(changeset.lts, true);
54+
assertEquals(changeset.xlts, true);
55+
assertEquals(changeset.entitlements, [UnityReleaseEntitlement.XLTS]);
56+
});
57+
58+
Deno.test("UnityChangeset constructor errors", () => {
59+
assertThrows(() => new UnityChangeset("", "abc123"), Error, "Version must be a non-empty string");
60+
assertThrows(() => new UnityChangeset("2018.3.0f1", ""), Error, "Changeset must be a non-empty string");
61+
assertThrows(() => new UnityChangeset("2018.3.0f1", "abc123", UnityReleaseStream.LTS, "invalid" as unknown as UnityReleaseEntitlement[]), Error, "Entitlements must be an array");
62+
});
63+
64+
Deno.test("UnityChangeset.toString", () => {
65+
const changeset = new UnityChangeset("2018.3.0f1", "abc123");
66+
assertEquals(changeset.toString(), "2018.3.0f1\tabc123");
67+
});
68+
2969
// getUnityChangeset
3070
[
3171
{ version: "2018.3.0f1", expected: "f023c421e164" },
@@ -110,6 +150,7 @@ const changesetsForTest = [
110150
{ options: { min: "2019", max: "", grep: "", allLifecycles: true, xlts: false, }, expected: 13, },
111151
{ options: { min: "2019", max: "", grep: "b", allLifecycles: true, xlts: false, }, expected: 4, },
112152
{ options: { min: "", max: "", grep: "", allLifecycles: false, xlts: true, }, expected: 14, },
153+
{ options: { min: "", max: "", grep: "2018", allLifecycles: false, xlts: false, }, expected: 8, },
113154
].forEach((testcase) => {
114155
Deno.test(`filterChangesets(${JSON.stringify(testcase.options)})`, () => {
115156
const changesets = filterChangesets(changesetsForTest, testcase.options);
@@ -134,3 +175,75 @@ Deno.test("scrapeArchivedChangesetsFromDb", async () => {
134175
const changesets = await searchChangesetsFromDb(searchModeToStreams(SearchMode.Default));
135176
assertNotEquals(changesets.length, 0);
136177
});
178+
179+
Deno.test("listChangesets", async () => {
180+
const result = await listChangesets(
181+
SearchMode.Default,
182+
{ min: "", max: "", grep: "", allLifecycles: false, xlts: false },
183+
GroupMode.All,
184+
OutputMode.Changeset,
185+
FormatMode.Json,
186+
);
187+
assertNotEquals(result.length, 0);
188+
// Should be valid JSON
189+
JSON.parse(result);
190+
});
191+
192+
Deno.test("searchModeToStreams", () => {
193+
assertEquals(searchModeToStreams(SearchMode.All), [
194+
UnityReleaseStream.LTS,
195+
UnityReleaseStream.SUPPORTED,
196+
UnityReleaseStream.TECH,
197+
UnityReleaseStream.BETA,
198+
UnityReleaseStream.ALPHA,
199+
]);
200+
assertEquals(searchModeToStreams(SearchMode.Default), [
201+
UnityReleaseStream.LTS,
202+
UnityReleaseStream.SUPPORTED,
203+
UnityReleaseStream.TECH,
204+
]);
205+
assertEquals(searchModeToStreams(SearchMode.PreRelease), [
206+
UnityReleaseStream.ALPHA,
207+
UnityReleaseStream.BETA,
208+
]);
209+
assertEquals(searchModeToStreams(SearchMode.LTS), [
210+
UnityReleaseStream.LTS,
211+
]);
212+
assertEquals(searchModeToStreams(SearchMode.Supported), [
213+
UnityReleaseStream.SUPPORTED,
214+
]);
215+
});
216+
217+
Deno.test("sanitizeVersion", () => {
218+
assertEquals(sanitizeVersion("2018.3.0f1"), "2018.3.0f1");
219+
assertEquals(sanitizeVersion("2019.1.0a9"), "2019.1.0a9");
220+
assertThrows(() => sanitizeVersion("2018.3<script>"), Error, "Version contains invalid characters");
221+
assertThrows(() => sanitizeVersion(123 as unknown as string), Error, "Version must be a string");
222+
});
223+
224+
Deno.test("validateFilterOptions", () => {
225+
// Valid options
226+
validateFilterOptions({ min: "2018.3", max: "2019.1", grep: "2018" });
227+
validateFilterOptions({});
228+
229+
// Invalid types
230+
assertThrows(() => validateFilterOptions({ min: 123 as unknown as string }), Error, "Min version must be a string");
231+
assertThrows(() => validateFilterOptions({ max: 123 as unknown as string }), Error, "Max version must be a string");
232+
assertThrows(() => validateFilterOptions({ grep: 123 as unknown as string }), Error, "Grep pattern must be a string");
233+
234+
// Invalid regex
235+
assertThrows(() => validateFilterOptions({ grep: "[invalid" }), Error, "Invalid grep pattern");
236+
});
237+
238+
Deno.test("groupBy", () => {
239+
const items = [
240+
{ id: 1, category: "A" },
241+
{ id: 2, category: "B" },
242+
{ id: 3, category: "A" },
243+
];
244+
const grouped = groupBy(items, (item) => item.category);
245+
assertEquals(grouped, {
246+
A: [{ id: 1, category: "A" }, { id: 3, category: "A" }],
247+
B: [{ id: 2, category: "B" }],
248+
});
249+
});

0 commit comments

Comments
 (0)