-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathupgrade-notify.test.ts
More file actions
140 lines (115 loc) · 4.83 KB
/
upgrade-notify.test.ts
File metadata and controls
140 lines (115 loc) · 4.83 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { afterEach, describe, expect, test } from "bun:test"
import { Installation } from "../../src/installation"
import { UPGRADE_KV_KEY, getAvailableVersion } from "../../src/cli/cmd/tui/component/upgrade-indicator-utils"
const fetch0 = globalThis.fetch
afterEach(() => {
globalThis.fetch = fetch0
})
describe("upgrade notification flow", () => {
describe("event definitions", () => {
test("UpdateAvailable has correct event type", () => {
expect(Installation.Event.UpdateAvailable.type).toBe("installation.update-available")
})
test("Updated has correct event type", () => {
expect(Installation.Event.Updated.type).toBe("installation.updated")
})
test("UpdateAvailable schema validates version string", () => {
const result = Installation.Event.UpdateAvailable.properties.safeParse({ version: "1.2.3" })
expect(result.success).toBe(true)
if (result.success) {
expect(result.data.version).toBe("1.2.3")
}
})
test("UpdateAvailable schema rejects missing version", () => {
const result = Installation.Event.UpdateAvailable.properties.safeParse({})
expect(result.success).toBe(false)
})
test("UpdateAvailable schema rejects non-string version", () => {
const result = Installation.Event.UpdateAvailable.properties.safeParse({ version: 123 })
expect(result.success).toBe(false)
})
})
describe("Installation.VERSION", () => {
test("is a non-empty string", () => {
expect(typeof Installation.VERSION).toBe("string")
expect(Installation.VERSION.length).toBeGreaterThan(0)
})
})
describe("latest version fetch", () => {
test("returns version from GitHub releases for unknown method", async () => {
globalThis.fetch = (async () =>
new Response(JSON.stringify({ tag_name: "v5.0.0" }), {
status: 200,
headers: { "content-type": "application/json" },
})) as unknown as typeof fetch
const latest = await Installation.latest("unknown")
expect(latest).toBe("5.0.0")
})
test("strips v prefix from GitHub tag", async () => {
globalThis.fetch = (async () =>
new Response(JSON.stringify({ tag_name: "v10.20.30" }), {
status: 200,
headers: { "content-type": "application/json" },
})) as unknown as typeof fetch
const latest = await Installation.latest("unknown")
expect(latest).toBe("10.20.30")
})
test("returns npm version for npm method", async () => {
globalThis.fetch = (async () =>
new Response(JSON.stringify({ version: "4.0.0" }), {
status: 200,
headers: { "content-type": "application/json" },
})) as unknown as typeof fetch
const latest = await Installation.latest("npm")
expect(latest).toBe("4.0.0")
})
})
})
describe("KV-based upgrade indicator integration", () => {
test("UPGRADE_KV_KEY is consistent", () => {
expect(UPGRADE_KV_KEY).toBe("update_available_version")
})
test("simulated KV store correctly tracks update version", () => {
const store: Record<string, any> = {}
store[UPGRADE_KV_KEY] = "999.0.0"
expect(store[UPGRADE_KV_KEY]).toBe("999.0.0")
})
test("indicator hidden when stored version is older (prevents downgrade arrow)", () => {
// F2 fix: user on 0.5.3, KV has stale "0.5.0" → should NOT show downgrade
// In dev mode (VERSION="local"), semver parsing can't compare, so indicator shows
const result = getAvailableVersion("0.5.0")
if (Installation.VERSION === "local") {
expect(result).toBe("0.5.0")
} else {
expect(result).toBeUndefined()
}
})
test("indicator shown when stored version is newer than current", () => {
const store: Record<string, any> = {}
store[UPGRADE_KV_KEY] = "999.0.0"
const result = getAvailableVersion(store[UPGRADE_KV_KEY])
expect(result).toBe("999.0.0")
})
test("indicator hidden when key is absent", () => {
const store: Record<string, any> = {}
const result = getAvailableVersion(store[UPGRADE_KV_KEY])
expect(result).toBeUndefined()
})
test("KV value can be overwritten with newer version", () => {
const store: Record<string, any> = {}
store[UPGRADE_KV_KEY] = "998.0.0"
store[UPGRADE_KV_KEY] = "999.0.0"
expect(store[UPGRADE_KV_KEY]).toBe("999.0.0")
const result = getAvailableVersion(store[UPGRADE_KV_KEY])
expect(result).toBe("999.0.0")
})
test("end-to-end: event → KV → indicator → reset on Updated", () => {
const store: Record<string, any> = {}
// Step 1: UpdateAvailable event stores version
store[UPGRADE_KV_KEY] = "999.0.0"
expect(getAvailableVersion(store[UPGRADE_KV_KEY])).toBe("999.0.0")
// Step 2: Updated event sets KV to current version (F1 fix)
store[UPGRADE_KV_KEY] = Installation.VERSION
expect(getAvailableVersion(store[UPGRADE_KV_KEY])).toBeUndefined()
})
})