@ -28,33 +28,6 @@ vi.mock("./credentials-write.runtime.js", () => ({
touchMatrixCredentials : touchMatrixCredentialsMock ,
} ) ) ;
let getMatrixScopedEnvVarNames : typeof import ( "./client/config.js" ) . getMatrixScopedEnvVarNames ;
let resolveImplicitMatrixAccountId : typeof import ( "./client/config.js" ) . resolveImplicitMatrixAccountId ;
let resolveMatrixConfig : typeof import ( "./client/config.js" ) . resolveMatrixConfig ;
let resolveMatrixConfigForAccount : typeof import ( "./client/config.js" ) . resolveMatrixConfigForAccount ;
let resolveMatrixAuth : typeof import ( "./client/config.js" ) . resolveMatrixAuth ;
let resolveMatrixAuthContext : typeof import ( "./client/config.js" ) . resolveMatrixAuthContext ;
let resolveValidatedMatrixHomeserverUrl : typeof import ( "./client/config.js" ) . resolveValidatedMatrixHomeserverUrl ;
let validateMatrixHomeserverUrl : typeof import ( "./client/config.js" ) . validateMatrixHomeserverUrl ;
let credentialsReadModule : typeof import ( "./credentials-read.js" ) ;
let sdkModule : typeof import ( "./sdk.js" ) ;
beforeEach ( async ( ) = > {
vi . resetModules ( ) ;
( {
getMatrixScopedEnvVarNames ,
resolveImplicitMatrixAccountId ,
resolveMatrixConfig ,
resolveMatrixConfigForAccount ,
resolveMatrixAuth ,
resolveMatrixAuthContext ,
resolveValidatedMatrixHomeserverUrl ,
validateMatrixHomeserverUrl ,
} = await import ( "./client/config.js" ) ) ;
credentialsReadModule = await import ( "./credentials-read.js" ) ;
sdkModule = await import ( "./sdk.js" ) ;
} ) ;
vi . mock ( "matrix-js-sdk" , async ( importOriginal ) = > {
const actual = await importOriginal < typeof import ( " matrix - js - sdk " ) > ( ) ;
return {
@ -66,6 +39,24 @@ vi.mock("matrix-js-sdk", async (importOriginal) => {
} ;
} ) ;
const {
getMatrixScopedEnvVarNames ,
resolveImplicitMatrixAccountId ,
resolveMatrixConfig ,
resolveMatrixConfigForAccount ,
resolveMatrixAuth ,
resolveMatrixAuthContext ,
resolveValidatedMatrixHomeserverUrl ,
validateMatrixHomeserverUrl ,
} = await import ( "./client/config.js" ) ;
let credentialsReadModule : typeof import ( "./credentials-read.js" ) | undefined ;
let sdkModule : typeof import ( "./sdk.js" ) | undefined ;
beforeEach ( ( ) = > {
installMatrixTestRuntime ( ) ;
} ) ;
describe ( "resolveMatrixConfig" , ( ) = > {
it ( "prefers config over env" , ( ) = > {
const cfg = {
@ -600,18 +591,30 @@ describe("resolveMatrixConfig", () => {
} ) ;
describe ( "resolveMatrixAuth" , ( ) = > {
beforeEach ( async ( ) = > {
credentialsReadModule ? ? = await import ( "./credentials-read.js" ) ;
sdkModule ? ? = await import ( "./sdk.js" ) ;
vi . mocked ( credentialsReadModule . loadMatrixCredentials ) . mockReset ( ) ;
vi . mocked ( credentialsReadModule . loadMatrixCredentials ) . mockReturnValue ( null ) ;
vi . mocked ( credentialsReadModule . credentialsMatchConfig ) . mockReset ( ) ;
vi . mocked ( credentialsReadModule . credentialsMatchConfig ) . mockReturnValue ( false ) ;
saveMatrixCredentialsMock . mockReset ( ) ;
touchMatrixCredentialsMock . mockReset ( ) ;
} ) ;
afterEach ( ( ) = > {
vi . restoreAllMocks ( ) ;
vi . unstubAllGlobals ( ) ;
saveMatrixCredentialsMock . mockReset ( ) ;
} ) ;
it ( "uses the hardened client request path for password login and persists deviceId" , async ( ) = > {
const doRequestSpy = vi . spyOn ( sdkModule . MatrixClient . prototype , "doRequest" ) . mockResolvedValue ( {
access_token : "tok-123" ,
user_id : "@bot:example.org" ,
device_id : "DEVICE123" ,
} ) ;
const doRequestSpy = vi
. spyOn ( sdkModule ! . MatrixClient . prototype , "doRequest" )
. mockResolvedValue ( {
access_token : "tok-123" ,
user_id : "@bot:example.org" ,
device_id : "DEVICE123" ,
} ) ;
const cfg = {
channels : {
@ -658,7 +661,7 @@ describe("resolveMatrixAuth", () => {
} ) ;
it ( "surfaces password login errors when account credentials are invalid" , async ( ) = > {
const doRequestSpy = vi . spyOn ( sdkModule . MatrixClient . prototype , "doRequest" ) ;
const doRequestSpy = vi . spyOn ( sdkModule ! . MatrixClient . prototype , "doRequest" ) ;
doRequestSpy . mockRejectedValueOnce ( new Error ( "Invalid username or password" ) ) ;
const cfg = {
@ -690,14 +693,14 @@ describe("resolveMatrixAuth", () => {
} ) ;
it ( "uses cached matching credentials when access token is not configured" , async ( ) = > {
vi . mocked ( credentialsReadModule . loadMatrixCredentials ) . mockReturnValue ( {
vi . mocked ( credentialsReadModule ! . loadMatrixCredentials ) . mockReturnValue ( {
homeserver : "https://matrix.example.org" ,
userId : "@bot:example.org" ,
accessToken : "cached-token" ,
deviceId : "CACHEDDEVICE" ,
createdAt : "2026-01-01T00:00:00.000Z" ,
} ) ;
vi . mocked ( credentialsReadModule . credentialsMatchConfig ) . mockReturnValue ( true ) ;
vi . mocked ( credentialsReadModule ! . credentialsMatchConfig ) . mockReturnValue ( true ) ;
const cfg = {
channels : {
@ -740,13 +743,13 @@ describe("resolveMatrixAuth", () => {
} ) ;
it ( "falls back to config deviceId when cached credentials are missing it" , async ( ) = > {
vi . mocked ( credentialsReadModule . loadMatrixCredentials ) . mockReturnValue ( {
vi . mocked ( credentialsReadModule ! . loadMatrixCredentials ) . mockReturnValue ( {
homeserver : "https://matrix.example.org" ,
userId : "@bot:example.org" ,
accessToken : "tok-123" ,
createdAt : "2026-01-01T00:00:00.000Z" ,
} ) ;
vi . mocked ( credentialsReadModule . credentialsMatchConfig ) . mockReturnValue ( true ) ;
vi . mocked ( credentialsReadModule ! . credentialsMatchConfig ) . mockReturnValue ( true ) ;
const cfg = {
channels : {
@ -799,10 +802,12 @@ describe("resolveMatrixAuth", () => {
} ) ;
it ( "resolves token-only non-default account userId from whoami instead of inheriting the base user" , async ( ) = > {
const doRequestSpy = vi . spyOn ( sdkModule . MatrixClient . prototype , "doRequest" ) . mockResolvedValue ( {
user_id : "@ops:example.org" ,
device_id : "OPSDEVICE" ,
} ) ;
const doRequestSpy = vi
. spyOn ( sdkModule ! . MatrixClient . prototype , "doRequest" )
. mockResolvedValue ( {
user_id : "@ops:example.org" ,
device_id : "OPSDEVICE" ,
} ) ;
const cfg = {
channels : {
@ -831,13 +836,15 @@ describe("resolveMatrixAuth", () => {
} ) ;
it ( "uses named-account password auth instead of inheriting the base access token" , async ( ) = > {
vi . mocked ( credentialsReadModule . loadMatrixCredentials ) . mockReturnValue ( null ) ;
vi . mocked ( credentialsReadModule . credentialsMatchConfig ) . mockReturnValue ( false ) ;
const doRequestSpy = vi . spyOn ( sdkModule . MatrixClient . prototype , "doRequest" ) . mockResolvedValue ( {
access_token : "ops-token" ,
user_id : "@ops:example.org" ,
device_id : "OPSDEVICE" ,
} ) ;
vi . mocked ( credentialsReadModule ! . loadMatrixCredentials ) . mockReturnValue ( null ) ;
vi . mocked ( credentialsReadModule ! . credentialsMatchConfig ) . mockReturnValue ( false ) ;
const doRequestSpy = vi
. spyOn ( sdkModule ! . MatrixClient . prototype , "doRequest" )
. mockResolvedValue ( {
access_token : "ops-token" ,
user_id : "@ops:example.org" ,
device_id : "OPSDEVICE" ,
} ) ;
const cfg = {
channels : {
@ -881,10 +888,12 @@ describe("resolveMatrixAuth", () => {
} ) ;
it ( "resolves missing whoami identity fields for token auth" , async ( ) = > {
const doRequestSpy = vi . spyOn ( sdkModule . MatrixClient . prototype , "doRequest" ) . mockResolvedValue ( {
user_id : "@bot:example.org" ,
device_id : "DEVICE123" ,
} ) ;
const doRequestSpy = vi
. spyOn ( sdkModule ! . MatrixClient . prototype , "doRequest" )
. mockResolvedValue ( {
user_id : "@bot:example.org" ,
device_id : "DEVICE123" ,
} ) ;
const cfg = {
channels : {
@ -918,10 +927,12 @@ describe("resolveMatrixAuth", () => {
await fs . writeFile ( secretPath , "file-token\n" , "utf8" ) ;
await fs . chmod ( secretPath , 0 o600 ) ;
const doRequestSpy = vi . spyOn ( sdkModule . MatrixClient . prototype , "doRequest" ) . mockResolvedValue ( {
user_id : "@bot:example.org" ,
device_id : "DEVICE123" ,
} ) ;
const doRequestSpy = vi
. spyOn ( sdkModule ! . MatrixClient . prototype , "doRequest" )
. mockResolvedValue ( {
user_id : "@bot:example.org" ,
device_id : "DEVICE123" ,
} ) ;
try {
const cfg = {
@ -961,10 +972,12 @@ describe("resolveMatrixAuth", () => {
} ) ;
it ( "does not resolve inactive password SecretRefs when scoped token auth wins" , async ( ) = > {
const doRequestSpy = vi . spyOn ( sdkModule . MatrixClient . prototype , "doRequest" ) . mockResolvedValue ( {
user_id : "@ops:example.org" ,
device_id : "OPSDEVICE" ,
} ) ;
const doRequestSpy = vi
. spyOn ( sdkModule ! . MatrixClient . prototype , "doRequest" )
. mockResolvedValue ( {
user_id : "@ops:example.org" ,
device_id : "OPSDEVICE" ,
} ) ;
const cfg = {
channels : {
@ -1006,13 +1019,13 @@ describe("resolveMatrixAuth", () => {
} ) ;
it ( "uses config deviceId with cached credentials when token is loaded from cache" , async ( ) = > {
vi . mocked ( credentialsReadModule . loadMatrixCredentials ) . mockReturnValue ( {
vi . mocked ( credentialsReadModule ! . loadMatrixCredentials ) . mockReturnValue ( {
homeserver : "https://matrix.example.org" ,
userId : "@bot:example.org" ,
accessToken : "tok-123" ,
createdAt : "2026-01-01T00:00:00.000Z" ,
} ) ;
vi . mocked ( credentialsReadModule . credentialsMatchConfig ) . mockReturnValue ( true ) ;
vi . mocked ( credentialsReadModule ! . credentialsMatchConfig ) . mockReturnValue ( true ) ;
const cfg = {
channels : {