refactor(test): extract shared mock helper and add try-finally for env cleanup
This commit is contained in:
parent
88bf8268f5
commit
fbe3b5423d
@ -366,9 +366,7 @@ describe("session-notification", () => {
|
|||||||
expect(notificationCalls).toHaveLength(1)
|
expect(notificationCalls).toHaveLength(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
test("should use terminal-notifier with -activate when available on darwin", async () => {
|
function createSenderMockCtx() {
|
||||||
// given - terminal-notifier is available and __CFBundleIdentifier is set
|
|
||||||
spyOn(sender, "sendSessionNotification").mockRestore()
|
|
||||||
const notifyCalls: string[] = []
|
const notifyCalls: string[] = []
|
||||||
const mockCtx = {
|
const mockCtx = {
|
||||||
$: async (cmd: TemplateStringsArray | string, ...values: any[]) => {
|
$: async (cmd: TemplateStringsArray | string, ...values: any[]) => {
|
||||||
@ -379,10 +377,18 @@ describe("session-notification", () => {
|
|||||||
return { stdout: "", stderr: "", exitCode: 0 }
|
return { stdout: "", stderr: "", exitCode: 0 }
|
||||||
},
|
},
|
||||||
} as any
|
} as any
|
||||||
|
return { mockCtx, notifyCalls }
|
||||||
|
}
|
||||||
|
|
||||||
|
test("should use terminal-notifier with -activate when available on darwin", async () => {
|
||||||
|
// given - terminal-notifier is available and __CFBundleIdentifier is set
|
||||||
|
spyOn(sender, "sendSessionNotification").mockRestore()
|
||||||
|
const { mockCtx, notifyCalls } = createSenderMockCtx()
|
||||||
spyOn(utils, "getTerminalNotifierPath").mockResolvedValue("/usr/local/bin/terminal-notifier")
|
spyOn(utils, "getTerminalNotifierPath").mockResolvedValue("/usr/local/bin/terminal-notifier")
|
||||||
const originalEnv = process.env.__CFBundleIdentifier
|
const originalEnv = process.env.__CFBundleIdentifier
|
||||||
process.env.__CFBundleIdentifier = "com.mitchellh.ghostty"
|
process.env.__CFBundleIdentifier = "com.mitchellh.ghostty"
|
||||||
|
|
||||||
|
try {
|
||||||
// when - sendSessionNotification is called directly on darwin
|
// when - sendSessionNotification is called directly on darwin
|
||||||
await sender.sendSessionNotification(mockCtx, "darwin", "Test Title", "Test Message")
|
await sender.sendSessionNotification(mockCtx, "darwin", "Test Title", "Test Message")
|
||||||
|
|
||||||
@ -392,28 +398,19 @@ describe("session-notification", () => {
|
|||||||
expect(tnCall).toBeDefined()
|
expect(tnCall).toBeDefined()
|
||||||
expect(tnCall).toContain("-activate")
|
expect(tnCall).toContain("-activate")
|
||||||
expect(tnCall).toContain("com.mitchellh.ghostty")
|
expect(tnCall).toContain("com.mitchellh.ghostty")
|
||||||
|
} finally {
|
||||||
// cleanup
|
|
||||||
if (originalEnv !== undefined) {
|
if (originalEnv !== undefined) {
|
||||||
process.env.__CFBundleIdentifier = originalEnv
|
process.env.__CFBundleIdentifier = originalEnv
|
||||||
} else {
|
} else {
|
||||||
delete process.env.__CFBundleIdentifier
|
delete process.env.__CFBundleIdentifier
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
test("should fall back to osascript when terminal-notifier is not available", async () => {
|
test("should fall back to osascript when terminal-notifier is not available", async () => {
|
||||||
// given - terminal-notifier is NOT available
|
// given - terminal-notifier is NOT available
|
||||||
spyOn(sender, "sendSessionNotification").mockRestore()
|
spyOn(sender, "sendSessionNotification").mockRestore()
|
||||||
const notifyCalls: string[] = []
|
const { mockCtx, notifyCalls } = createSenderMockCtx()
|
||||||
const mockCtx = {
|
|
||||||
$: async (cmd: TemplateStringsArray | string, ...values: any[]) => {
|
|
||||||
const cmdStr = typeof cmd === "string"
|
|
||||||
? cmd
|
|
||||||
: cmd.reduce((acc, part, i) => acc + part + (values[i] ?? ""), "")
|
|
||||||
notifyCalls.push(cmdStr)
|
|
||||||
return { stdout: "", stderr: "", exitCode: 0 }
|
|
||||||
},
|
|
||||||
} as any
|
|
||||||
spyOn(utils, "getTerminalNotifierPath").mockResolvedValue(null)
|
spyOn(utils, "getTerminalNotifierPath").mockResolvedValue(null)
|
||||||
spyOn(utils, "getOsascriptPath").mockResolvedValue("/usr/bin/osascript")
|
spyOn(utils, "getOsascriptPath").mockResolvedValue("/usr/bin/osascript")
|
||||||
|
|
||||||
@ -431,20 +428,12 @@ describe("session-notification", () => {
|
|||||||
test("should use terminal-notifier without -activate when __CFBundleIdentifier is not set", async () => {
|
test("should use terminal-notifier without -activate when __CFBundleIdentifier is not set", async () => {
|
||||||
// given - terminal-notifier available but no bundle ID
|
// given - terminal-notifier available but no bundle ID
|
||||||
spyOn(sender, "sendSessionNotification").mockRestore()
|
spyOn(sender, "sendSessionNotification").mockRestore()
|
||||||
const notifyCalls: string[] = []
|
const { mockCtx, notifyCalls } = createSenderMockCtx()
|
||||||
const mockCtx = {
|
|
||||||
$: async (cmd: TemplateStringsArray | string, ...values: any[]) => {
|
|
||||||
const cmdStr = typeof cmd === "string"
|
|
||||||
? cmd
|
|
||||||
: cmd.reduce((acc, part, i) => acc + part + (values[i] ?? ""), "")
|
|
||||||
notifyCalls.push(cmdStr)
|
|
||||||
return { stdout: "", stderr: "", exitCode: 0 }
|
|
||||||
},
|
|
||||||
} as any
|
|
||||||
spyOn(utils, "getTerminalNotifierPath").mockResolvedValue("/usr/local/bin/terminal-notifier")
|
spyOn(utils, "getTerminalNotifierPath").mockResolvedValue("/usr/local/bin/terminal-notifier")
|
||||||
const originalEnv = process.env.__CFBundleIdentifier
|
const originalEnv = process.env.__CFBundleIdentifier
|
||||||
delete process.env.__CFBundleIdentifier
|
delete process.env.__CFBundleIdentifier
|
||||||
|
|
||||||
|
try {
|
||||||
// when - sendSessionNotification is called directly on darwin
|
// when - sendSessionNotification is called directly on darwin
|
||||||
await sender.sendSessionNotification(mockCtx, "darwin", "Test Title", "Test Message")
|
await sender.sendSessionNotification(mockCtx, "darwin", "Test Title", "Test Message")
|
||||||
|
|
||||||
@ -453,11 +442,10 @@ describe("session-notification", () => {
|
|||||||
const tnCall = notifyCalls.find(c => c.includes("terminal-notifier"))
|
const tnCall = notifyCalls.find(c => c.includes("terminal-notifier"))
|
||||||
expect(tnCall).toBeDefined()
|
expect(tnCall).toBeDefined()
|
||||||
expect(tnCall).not.toContain("-activate")
|
expect(tnCall).not.toContain("-activate")
|
||||||
|
} finally {
|
||||||
// cleanup
|
|
||||||
if (originalEnv !== undefined) {
|
if (originalEnv !== undefined) {
|
||||||
process.env.__CFBundleIdentifier = originalEnv
|
process.env.__CFBundleIdentifier = originalEnv
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user