Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 45 additions & 19 deletions packages/compass-preferences-model/src/storage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import { mkdtempSync, rmdirSync, existsSync, readFileSync } from 'fs';
import fs from 'fs/promises';
import path from 'path';
import os from 'os';
import { UserStorage, StoragePreferences, type User } from './storage';
import { expect } from 'chai';
import type { UserPreferences } from './preferences';

const getPreferencesFolder = (tmpDir: string) => {
return path.join(tmpDir, 'AppPreferences');
};

const getPreferencesFile = (tmpDir: string) => {
return path.join(getPreferencesFolder(tmpDir), 'General.json');
};

describe('storage', function () {
let tmpDir: string;
describe('UserStorage', function () {
let storedUser: User;
let userStorage: UserStorage;
before(async function () {
tmpDir = mkdtempSync(path.join(os.tmpdir()));
tmpDir = await fs.mkdtemp(path.join(os.tmpdir()));
userStorage = new UserStorage(tmpDir);
storedUser = await userStorage.getOrCreate('');
});
after(function () {
rmdirSync(tmpDir, { recursive: true });
after(async function () {
await fs.rmdir(tmpDir, { recursive: true });
});

it('creates a new user if user does not exist', async function () {
Expand Down Expand Up @@ -47,12 +55,12 @@ describe('storage', function () {
const defaultPreferences = {
showedNetworkOptIn: true,
} as unknown as UserPreferences;
beforeEach(function () {
tmpDir = mkdtempSync(path.join(os.tmpdir()));
beforeEach(async function () {
tmpDir = await fs.mkdtemp(path.join(os.tmpdir()));
});

afterEach(function () {
rmdirSync(tmpDir, { recursive: true });
afterEach(async function () {
await fs.rmdir(tmpDir, { recursive: true });
});

it('sets up the storage', async function () {
Expand All @@ -61,22 +69,19 @@ describe('storage', function () {

const storage = new StoragePreferences(defaultPreferences, tmpDir);

const preferencesDir = path.join(tmpDir, 'AppPreferences');
const preferencesFile = path.join(
tmpDir,
'AppPreferences',
'General.json'
);
const preferencesDir = getPreferencesFolder(tmpDir);
const preferencesFile = getPreferencesFile(tmpDir);

expect(existsSync(preferencesDir)).to.be.false;
expect(existsSync(preferencesFile)).to.be.false;
expect(async () => await fs.access(preferencesDir)).to.throw;
expect(async () => await fs.access(preferencesFile)).to.throw;

await storage.setup();

expect(existsSync(preferencesDir)).to.be.true;
expect(existsSync(preferencesFile)).to.be.true;
expect(async () => await fs.access(preferencesDir)).to.not.throw;
expect(async () => await fs.access(preferencesFile)).to.not.throw;

expect(
JSON.parse(readFileSync(preferencesFile).toString())
JSON.parse((await fs.readFile(preferencesFile)).toString())
).to.deep.equal(defaultPreferences);
});

Expand All @@ -93,5 +98,26 @@ describe('storage', function () {
currentUserId: '123456789',
});
});

it('returns default preference values if its not stored on disk', async function () {
const storage = new StoragePreferences(defaultPreferences, tmpDir);

// manually setup the file with no content
await fs.mkdir(getPreferencesFolder(tmpDir));
await fs.writeFile(
getPreferencesFile(tmpDir),
JSON.stringify({}),
'utf-8'
);

await storage.updatePreferences({
currentUserId: '123456789',
});

expect(storage.getPreferences()).to.deep.equal({
...defaultPreferences,
currentUserId: '123456789',
});
});
});
});
5 changes: 4 additions & 1 deletion packages/compass-preferences-model/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ export class StoragePreferences extends BasePreferencesStorage {
}

getPreferences() {
return this.preferences;
return {
...this.defaultPreferences,
...this.preferences,
};
}

async updatePreferences(attributes: Partial<UserPreferences>) {
Expand Down