summaryrefslogtreecommitdiffhomepage
path: root/packages/surface-loaded-extensions/src/spec.test.ts
blob: 9c1aa6aa9294a775f275961d8a8346932db7f78d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import type { Manifest } from "@dispatch/kernel";
import type { StatField } from "@dispatch/ui-contract";
import { describe, expect, it } from "vitest";
import { buildLoadedExtensionsSpec } from "./spec.js";

function fakeManifest(id: string, name: string, version: string): Manifest {
	return {
		id,
		name,
		version,
		apiVersion: "^0.1.0",
		trust: "bundled",
	};
}

describe("buildLoadedExtensionsSpec", () => {
	it("returns a count stat of '0' and no extension stats for empty manifests", () => {
		const spec = buildLoadedExtensionsSpec([]);

		expect(spec.id).toBe("loaded-extensions");
		expect(spec.region).toBe("side");
		expect(spec.title).toBe("Loaded Extensions");
		expect(spec.fields).toHaveLength(1);
		expect(spec.fields[0]).toEqual({
			kind: "stat",
			label: "Loaded",
			value: "0",
		});
	});

	it("returns a count stat plus one stat per manifest in order", () => {
		const manifests = [
			fakeManifest("alpha", "Alpha", "1.0.0"),
			fakeManifest("beta", "Beta", "2.3.1"),
			fakeManifest("gamma", "Gamma", "0.5.0"),
		];

		const spec = buildLoadedExtensionsSpec(manifests);

		expect(spec.fields).toHaveLength(4);
		expect(spec.fields[0]).toEqual({
			kind: "stat",
			label: "Loaded",
			value: "3",
		});
		expect(spec.fields[1]).toEqual({
			kind: "stat",
			label: "Alpha",
			value: "1.0.0",
		});
		expect(spec.fields[2]).toEqual({
			kind: "stat",
			label: "Beta",
			value: "2.3.1",
		});
		expect(spec.fields[3]).toEqual({
			kind: "stat",
			label: "Gamma",
			value: "0.5.0",
		});
	});

	it("preserves input order of manifests", () => {
		const manifests = [
			fakeManifest("z-last", "Z Last", "1.0.0"),
			fakeManifest("a-first", "A First", "2.0.0"),
		];

		const spec = buildLoadedExtensionsSpec(manifests);

		expect((spec.fields[1] as StatField).label).toBe("Z Last");
		expect((spec.fields[2] as StatField).label).toBe("A First");
	});

	it("sets the surface id, region, and title correctly", () => {
		const spec = buildLoadedExtensionsSpec([]);

		expect(spec.id).toBe("loaded-extensions");
		expect(spec.region).toBe("side");
		expect(spec.title).toBe("Loaded Extensions");
	});

	it("uses manifest.name as label and manifest.version as value", () => {
		const manifests = [fakeManifest("my-ext", "My Extension", "3.2.1")];

		const spec = buildLoadedExtensionsSpec(manifests);

		expect(spec.fields[1]).toEqual({
			kind: "stat",
			label: "My Extension",
			value: "3.2.1",
		});
	});
});