Skip to content

Commit c2ed51f

Browse files
test(config): assert the error types profile reads and deletes answer with
Spec-path-only change answering the review asks on PR 2416. getProfile rejects a name miss and an id miss with ProviderSettingsNotFoundError, checked by instance rather than by message text; deleteConfig rethrows the same type unchanged for a missing config; a failed profile read and the last-remaining-config refusal stay plain Errors under their own prefixes; and getCurrentProfileName is covered by a focused test of the value it returns and of the wrapper it uses when the read fails. No production line changes, so no executable line inside a mutation-tested package moves.
1 parent 6e3cfda commit c2ed51f

1 file changed

Lines changed: 108 additions & 1 deletion

File tree

‎src/core/config/__tests__/ProviderSettingsManager.spec.ts‎

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import { clearAllMocks } from "../../../test-utils/reset"
1313
import { makeExtensionContext } from "../../../test-utils/vscode"
1414

15-
import { ProviderSettingsManager, ProviderProfiles } from "../ProviderSettingsManager"
15+
import { ProviderSettingsManager, ProviderSettingsNotFoundError, ProviderProfiles } from "../ProviderSettingsManager"
1616

1717
// `export()` builds an API handler per profile to read model capabilities. Mock
1818
// buildApiHandler with the real @roo-code/types model definitions so the token-field
@@ -1829,4 +1829,111 @@ describe("ProviderSettingsManager", () => {
18291829
expect(result.activeProfileId).toBe("local-id")
18301830
})
18311831
})
1832+
1833+
describe("profile reads and deletes answer with the type callers branch on", () => {
1834+
const profilesWith = (apiConfigs: Record<string, unknown>, currentApiConfigName = "default") =>
1835+
JSON.stringify({
1836+
currentApiConfigName,
1837+
apiConfigs,
1838+
modeApiConfigs: {},
1839+
migrations: { rateLimitSecondsMigrated: false },
1840+
})
1841+
1842+
it("rejects a name lookup that misses with ProviderSettingsNotFoundError", async () => {
1843+
mockSecrets.get.mockResolvedValue(profilesWith({ default: { id: "default" } }))
1844+
1845+
const error = await providerSettingsManager.getProfile({ name: "absent" }).catch((cause: unknown) => cause)
1846+
1847+
if (!(error instanceof ProviderSettingsNotFoundError)) {
1848+
throw new Error(`expected ProviderSettingsNotFoundError, got ${String(error)}`)
1849+
}
1850+
expect(error.name).toBe("ProviderSettingsNotFoundError")
1851+
expect(error.message).toBe("Config with name 'absent' not found")
1852+
})
1853+
1854+
it("rejects an id lookup that misses with ProviderSettingsNotFoundError", async () => {
1855+
mockSecrets.get.mockResolvedValue(profilesWith({ default: { id: "default" } }))
1856+
1857+
const error = await providerSettingsManager
1858+
.getProfile({ id: "missing-id" })
1859+
.catch((cause: unknown) => cause)
1860+
1861+
if (!(error instanceof ProviderSettingsNotFoundError)) {
1862+
throw new Error(`expected ProviderSettingsNotFoundError, got ${String(error)}`)
1863+
}
1864+
expect(error.message).toBe("Config with ID 'missing-id' not found")
1865+
})
1866+
1867+
it("keeps a missed lookup out of the wrapped-read branch", async () => {
1868+
mockSecrets.get.mockResolvedValue(profilesWith({ default: { id: "default" } }))
1869+
1870+
await expect(providerSettingsManager.getProfile({ name: "absent" })).rejects.not.toThrow(
1871+
"Failed to get profile:",
1872+
)
1873+
})
1874+
1875+
it("surfaces a failed profile read as a plain Error naming the read", async () => {
1876+
mockSecrets.get.mockRejectedValue(new Error("storage unavailable"))
1877+
1878+
const error = await providerSettingsManager.getProfile({ name: "default" }).catch((cause: unknown) => cause)
1879+
1880+
if (!(error instanceof Error)) {
1881+
throw new Error(`expected an Error, got ${String(error)}`)
1882+
}
1883+
expect(error).not.toBeInstanceOf(ProviderSettingsNotFoundError)
1884+
expect(error.name).toBe("Error")
1885+
expect(error.message.startsWith("Failed to get profile:")).toBe(true)
1886+
expect(error.message.includes("storage unavailable")).toBe(true)
1887+
})
1888+
1889+
it("rethrows a missing config from deleteConfig as the typed error", async () => {
1890+
mockSecrets.get.mockResolvedValue(profilesWith({ default: { id: "default" }, other: { id: "other-id" } }))
1891+
1892+
const error = await providerSettingsManager.deleteConfig("absent").catch((cause: unknown) => cause)
1893+
1894+
if (!(error instanceof ProviderSettingsNotFoundError)) {
1895+
throw new Error(`expected ProviderSettingsNotFoundError, got ${String(error)}`)
1896+
}
1897+
expect(error.message).toBe("Config 'absent' not found")
1898+
})
1899+
1900+
it("keeps the last-remaining-config refusal a plain Error under the delete prefix", async () => {
1901+
mockSecrets.get.mockResolvedValue(profilesWith({ default: { id: "default" } }))
1902+
1903+
const error = await providerSettingsManager.deleteConfig("default").catch((cause: unknown) => cause)
1904+
1905+
if (!(error instanceof Error)) {
1906+
throw new Error(`expected an Error, got ${String(error)}`)
1907+
}
1908+
expect(error).not.toBeInstanceOf(ProviderSettingsNotFoundError)
1909+
expect(error.message).toBe("Failed to delete config: Error: Cannot delete the last remaining configuration")
1910+
})
1911+
})
1912+
1913+
describe("getCurrentProfileName", () => {
1914+
it("returns the profile the manager store marks current", async () => {
1915+
mockSecrets.get.mockResolvedValue(
1916+
JSON.stringify({
1917+
currentApiConfigName: "work",
1918+
apiConfigs: { default: { id: "default" }, work: { id: "work-id" } },
1919+
modeApiConfigs: {},
1920+
migrations: { rateLimitSecondsMigrated: false },
1921+
}),
1922+
)
1923+
1924+
await expect(providerSettingsManager.getCurrentProfileName()).resolves.toBe("work")
1925+
})
1926+
1927+
it("wraps a failed read in an Error naming the active profile", async () => {
1928+
mockSecrets.get.mockRejectedValue(new Error("storage unavailable"))
1929+
1930+
const error = await providerSettingsManager.getCurrentProfileName().catch((cause: unknown) => cause)
1931+
1932+
if (!(error instanceof Error)) {
1933+
throw new Error(`expected an Error, got ${String(error)}`)
1934+
}
1935+
expect(error).not.toBeInstanceOf(ProviderSettingsNotFoundError)
1936+
expect(error.message.startsWith("Failed to read the active profile:")).toBe(true)
1937+
})
1938+
})
18321939
})

0 commit comments

Comments
 (0)