File size: 3,870 Bytes
26abd99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { expect } from 'chai';
import { SinonFakeTimers } from 'sinon';
import * as sinon from 'sinon';
import { CancellationTokenSource } from 'vscode';
import { newVsCodeStub, VsCodeStub } from '../test/helpers/vscode';
import { CodeManager } from './code-manager';

describe('CodeManager', () => {
  let vsCodeStub: VsCodeStub;
  let clock: SinonFakeTimers;
  let cancellationTokenSource: CancellationTokenSource;
  let manager: CodeManager;

  beforeEach(() => {
    vsCodeStub = newVsCodeStub();
    clock = sinon.useFakeTimers({ toFake: ['setTimeout'] });
    cancellationTokenSource = new vsCodeStub.CancellationTokenSource();
    manager = new CodeManager();
  });

  afterEach(() => {
    manager.dispose();
    clock.restore();
    sinon.restore();
  });

  it('rejects outstanding codes when disposed', async () => {
    const code = manager.waitForCode('1', cancellationTokenSource.token);

    manager.dispose();

    await expect(code).to.be.rejectedWith(/disposed/);
  });

  it('rejects when waiting for the same nonce', async () => {
    const nonce = '1';

    void manager.waitForCode(nonce, cancellationTokenSource.token);

    await expect(
      manager.waitForCode(nonce, cancellationTokenSource.token),
    ).to.be.rejectedWith(/waiting/);
  });

  it('throws when resolving an unknown nonce', async () => {
    const nonce = '1';
    const code = '42';
    const gotCode = manager.waitForCode(nonce, cancellationTokenSource.token);

    expect(() => {
      manager.resolveCode('unknown', code);
    }).to.throw(/Unexpected/);

    // Ensure no code is resolved and ultimately times out.
    clock.tick(60_001);
    await expect(gotCode).to.be.rejectedWith(/timeout/);
  });

  it('rejects when the timeout is exceeded', async () => {
    const gotCode = manager.waitForCode('1', cancellationTokenSource.token);

    clock.tick(60_001);

    await expect(gotCode).to.be.rejectedWith(/timeout/);
  });

  it('rejects when the user cancels', async () => {
    const gotCode = manager.waitForCode('1', cancellationTokenSource.token);

    cancellationTokenSource.cancel();

    await expect(gotCode).to.be.rejectedWith(/cancelled/);
  });

  it('resolves a code', async () => {
    const code = '42';
    const nonce = '123';

    const gotCode = manager.waitForCode(nonce, cancellationTokenSource.token);
    manager.resolveCode(nonce, code);

    await expect(gotCode).to.eventually.equal(code);
  });

  it('resolves the code corresponding to the nonce', async () => {
    const redirects = [
      { nonce: '1', code: '42' },
      { nonce: '2', code: '99' },
    ];

    const gotFirstCode = manager.waitForCode(
      redirects[0].nonce,
      cancellationTokenSource.token,
    );
    const gotSecondCode = manager.waitForCode(
      redirects[1].nonce,
      cancellationTokenSource.token,
    );
    // Redirect the second before the first.
    manager.resolveCode(redirects[1].nonce, redirects[1].code);
    manager.resolveCode(redirects[0].nonce, redirects[0].code);

    await expect(gotFirstCode).to.eventually.equal(redirects[0].code);
    await expect(gotSecondCode).to.eventually.equal(redirects[1].code);
  });

  it('resolves a code while another request times out', async () => {
    const code = '42';
    const nonce = '2';
    const gotFirstCode = manager.waitForCode(
      '1',
      cancellationTokenSource.token,
    );
    // Wait 30s after the first.
    clock.tick(30_000);
    const gotSecondCode = manager.waitForCode(
      nonce,
      cancellationTokenSource.token,
    );
    // Wait just over another 30s to time-out the first.
    clock.tick(30_001);
    await expect(gotFirstCode).to.be.rejectedWith(/timeout/);

    manager.resolveCode(nonce, code);

    await expect(gotSecondCode).to.eventually.equal(code);
  });
});