Skip to content

Commit

Permalink
fix: Unable to switch off the stats collecting
Browse files Browse the repository at this point in the history
  • Loading branch information
tarampampam committed Jul 5, 2024
1 parent 9ade972 commit a0025ca
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
37 changes: 36 additions & 1 deletion src/entrypoints/background/persistent/settings.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, test } from 'vitest'
import type { SettingsState } from '../../../shared/types/settings'
import Settings from './settings'
import StorageArea from './storage-area'

Expand All @@ -12,7 +13,7 @@ describe('settings', () => {
}

async set<T>(v: T): Promise<void> {
this.state = v as Record<string, unknown>
this.state = JSON.parse(JSON.stringify(v)) as Record<string, unknown>
}

async get<T>(): Promise<T | undefined> {
Expand Down Expand Up @@ -52,6 +53,40 @@ describe('settings', () => {
expect((await settings.get()).renew.intervalMillis).toEqual(30000)
})

test('partial from storage', async () => {
const area: StorageArea<SettingsState> = new StorageAreaMock('some-key', 'local')
const settings = new Settings(area)

await settings.update({ enabled: false, remoteUseragentList: { uri: 'foo' } }) // force to save

// read the current state and remove some key
{
const current = await area.get()
if (!current) {
throw new Error('current is undefined')
}

// @ts-expect-error - emulate partial data
delete current.stats

await area.set(current)
}

let executed = false

settings.onChange((s) => {
expect(s.stats).not.toBeUndefined()
expect(s.remoteUseragentList.uri).toEqual('foo')
expect(s.enabled).toBeTruthy()

executed = true
})

await settings.update({ enabled: true }) // force to save

expect(executed).toBeTruthy()
})

test('onChange listener', async () => {
const settings = new Settings(new StorageAreaMock('some-key', 'local'))
let execCount = 0
Expand Down
4 changes: 2 additions & 2 deletions src/entrypoints/background/persistent/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ export default class {
delete updated.blacklist.custom
}

const current = await this.storage.get()
const [merged, changes] = this.merge(structuredClone(current ?? this.defaults), updated)
const [current] = this.merge(this.defaults, (await this.storage.get()) ?? this.defaults)
const [merged, changes] = this.merge(structuredClone(current), updated)

if (changes > 0) {
// to clean in the storage
Expand Down

0 comments on commit a0025ca

Please sign in to comment.