|
5 | 5 |
|
6 | 6 | import assert from 'assert' |
7 | 7 | import { isNewOsSession } from '../../../shared/utilities/osUtils' |
8 | | -import { fs } from '../../../shared' |
9 | | -import { isWin } from '../../../shared/vscode/env' |
10 | | -import { TestFolder } from '../../testUtil' |
| 8 | +import { InstalledClock } from '@sinonjs/fake-timers' |
| 9 | +import { createSandbox, SinonSandbox } from 'sinon' |
11 | 10 |
|
12 | 11 | describe('isNewOsSession', () => { |
13 | | - if (!isWin()) { |
14 | | - it('unix-like: returns true when expected', async () => { |
15 | | - const tmpDir = (await TestFolder.create()).path |
16 | | - |
17 | | - // On a new session the first caller will get true |
18 | | - assert.strictEqual(await isNewOsSession(tmpDir), true) |
19 | | - // Subsequent callers will get false |
20 | | - assert.strictEqual(await isNewOsSession(tmpDir), false) |
21 | | - |
22 | | - // Mimic a fresh restart (/tmp/ folder is deleted) |
23 | | - const files = await fs.readdir(tmpDir) |
24 | | - await Promise.all(files.map(async (file) => await fs.delete(`${tmpDir}/${file[0]}`))) |
25 | | - |
26 | | - // Since the tmp/ folder was cleared it is considered a new session |
27 | | - assert.strictEqual(await isNewOsSession(tmpDir), true) |
28 | | - assert.strictEqual(await isNewOsSession(tmpDir), false) |
29 | | - }) |
30 | | - } |
| 12 | + let clock: InstalledClock | undefined |
| 13 | + let sandbox: SinonSandbox |
| 14 | + |
| 15 | + beforeEach(function () { |
| 16 | + sandbox = createSandbox() |
| 17 | + }) |
| 18 | + |
| 19 | + afterEach(function () { |
| 20 | + clock?.uninstall() |
| 21 | + sandbox.restore() |
| 22 | + }) |
| 23 | + |
| 24 | + it('unix-like: returns true when expected', async () => { |
| 25 | + const uptimeStub = sandbox.stub() |
| 26 | + const now = sandbox.stub() |
| 27 | + // We started our computer at 2 minutes since epoch (time - pc uptime) |
| 28 | + // and the comptuer has been on for 1 minute. So the OS started 1 minute since epoch. |
| 29 | + now.returns(60_000 + 60_000) |
| 30 | + uptimeStub.returns(1) |
| 31 | + |
| 32 | + // On a brand new session the first caller will get true |
| 33 | + assert.strictEqual(await isNewOsSession(now, uptimeStub), true) |
| 34 | + // Subsequent callers will get false |
| 35 | + assert.strictEqual(await isNewOsSession(now, uptimeStub), false) |
| 36 | + |
| 37 | + // Start a computer session 10 minutes from epoch |
| 38 | + uptimeStub.returns(0) |
| 39 | + now.returns(60_000 * 10) |
| 40 | + assert.strictEqual(await isNewOsSession(now, uptimeStub), true) |
| 41 | + // Anything that is within a 5 second threshold of the last session time, is considered the same session |
| 42 | + now.returns(60_000 * 10 + 5000) |
| 43 | + assert.strictEqual(await isNewOsSession(now, uptimeStub), false) |
| 44 | + now.returns(60_000 * 10 + 5000 + 1) |
| 45 | + assert.strictEqual(await isNewOsSession(now, uptimeStub), true) |
| 46 | + |
| 47 | + // A non-zero uptime |
| 48 | + uptimeStub.returns(5) // The computer has been running for 5 minutes already, so the start time is relative to this. |
| 49 | + now.returns(60_000 * 10 + 5000 + 60_000 * 10) // 5 minutes since last session |
| 50 | + // Nothing changes since the diff between uptime and the last start has not changed |
| 51 | + assert.strictEqual(await isNewOsSession(now, uptimeStub), true) |
| 52 | + }) |
31 | 53 | }) |
0 commit comments