// #region AuthStoreBrowserOffTest [C:2] [TYPE Module] [SEMANTICS test, auth, store, browser-offline, ssr]
// @BRIEF Unit tests for auth store when running in non-browser environment (SSR).
// @LAYER Tests
// @RELATION BINDS_TO -> [AuthStore]
// @TEST_CONTRACT: When browser=false, localStorage is never accessed.
import { describe, it, expect, vi, beforeEach } from 'vitest';

// Mock $app/environment with browser=false
vi.mock('$app/environment', () => ({ browser: false }));

describe('AuthStore — when browser is false', () => {
	beforeEach(() => {
		localStorage.clear();
		vi.resetModules();
	});

	it('initializes token as null (no localStorage read)', async () => {
		localStorage.setItem('auth_token', 'should-be-ignored');
		const { auth } = await import('$lib/auth/store.svelte.js');
		const subFn = vi.fn();
		auth.subscribe(subFn);
		const initial = subFn.mock.calls[0][0];
		expect(initial.token).toBeNull();
	});

	it('setToken does not call localStorage.setItem', async () => {
		const { auth } = await import('$lib/auth/store.svelte.js');
		auth.setToken('no-localstorage');
		expect(localStorage.getItem('auth_token')).toBeNull();
	});

	it('logout does not call localStorage.removeItem when no token was stored', async () => {
		const { auth } = await import('$lib/auth/store.svelte.js');
		auth.setToken('some-token');
		auth.logout();
		expect(localStorage.getItem('auth_token')).toBeNull();
	});
});
// #endregion
