Skip to content

Commit

Permalink
Passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pastuxso committed Nov 27, 2024
1 parent d4bed15 commit 6956ac7
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 63 deletions.
17 changes: 0 additions & 17 deletions src/extension/authSessionChangeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export default class AuthSessionChangeHandler {
private subscriptions: Subscription[] = []
private listeners: ((event: AuthenticationSessionsChangeEvent) => void)[] = []
private initialized = false
// private isProcessing = false

private constructor(private debounceTimeMs: number = 500) {
this.eventSubject = new Subject<AuthenticationSessionsChangeEvent>()
Expand Down Expand Up @@ -37,22 +36,6 @@ export default class AuthSessionChangeHandler {
}),
)

// this.subscriptions.push(
// this.eventSubject
// .pipe(
// filter(() => !this.isProcessing),
// take(1),
// )
// .subscribe((event) => {
// this.isProcessing = true
// this.notifyListeners(event)

// setTimeout(() => {
// this.isProcessing = false
// }, 0)
// }),
// )

context.subscriptions.push(
authentication.onDidChangeSessions((e) => {
console.log(`******* authentication.onDidChangeSessions ${e.provider.id}`)
Expand Down
9 changes: 4 additions & 5 deletions src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,10 @@ export class RunmeExtension {
const uriHandler = new RunmeUriHandler(context, kernel, getForceNewWindowConfig())

StatefulAuthProvider.initialize(context, kernel, uriHandler)
const authSessionChangeHandler = AuthSessionChangeHandler.instance
AuthSessionChangeHandler.instance.initialize(context)

authSessionChangeHandler.initialize(context)
context.subscriptions.push({
dispose: () => authSessionChangeHandler.dispose(),
dispose: () => AuthSessionChangeHandler.instance.dispose(),
})

const server = new KernelServer(
Expand Down Expand Up @@ -545,7 +544,7 @@ export class RunmeExtension {

// context.subscriptions.push(authentication.onDidChangeSessions(onChangeSession))

authSessionChangeHandler.addListener((e) => {
AuthSessionChangeHandler.instance.addListener((e) => {
if (
StatefulAuthProvider.instance &&
kernel.isFeatureOn(FeatureName.RequireStatefulAuth) &&
Expand All @@ -567,7 +566,7 @@ export class RunmeExtension {
}
})

authSessionChangeHandler.addListener((e) => {
AuthSessionChangeHandler.instance.addListener((e) => {
if (
kernel.isFeatureOn(FeatureName.Gist) &&
e.provider.id === AuthenticationProviders.GitHub
Expand Down
63 changes: 33 additions & 30 deletions src/extension/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ import { CommandModeEnum } from './grpc/runner/types'
import { GrpcReporter } from './reporter'
import { EnvStoreMonitorWithSession } from './panels/notebook'
import { SignedIn } from './signedIn'
import { StatefulAuthProvider } from './provider/statefulAuth'

enum ConfirmationItems {
Yes = 'Yes',
Expand Down Expand Up @@ -202,42 +203,44 @@ export class Kernel implements Disposable {
this.#onlySignedIn = new SignedIn(this)
this.#disposables.push(this.#onlySignedIn)

const packageJSON = context?.extension?.packageJSON || {}
const featContext: FeatureContext = {
os: os.platform(),
vsCodeVersion: version as string,
extensionVersion: packageJSON?.version,
githubAuth: false,
statefulAuth: false,
extensionId: context?.extension?.id as ExtensionName,
}
StatefulAuthProvider.getSession().then((session) => {
const packageJSON = context?.extension?.packageJSON || {}
const featContext: FeatureContext = {
os: os.platform(),
vsCodeVersion: version as string,
extensionVersion: packageJSON?.version,
githubAuth: false,
statefulAuth: !!session,
extensionId: context?.extension?.id as ExtensionName,
}

const runmeFeatureSettings = workspace.getConfiguration('runme.features')
const featureNames = Object.keys(FeatureName)
const runmeFeatureSettings = workspace.getConfiguration('runme.features')
const featureNames = Object.keys(FeatureName)

featureNames.forEach((feature) => {
if (runmeFeatureSettings.has(feature)) {
const result = runmeFeatureSettings.get<boolean>(feature, false)
this.#featuresSettings.set(feature, result)
}
})
featureNames.forEach((feature) => {
if (runmeFeatureSettings.has(feature)) {
const result = runmeFeatureSettings.get<boolean>(feature, false)
this.#featuresSettings.set(feature, result)
}
})

this.featuresState$ = features.loadState(packageJSON, featContext, this.#featuresSettings)
this.featuresState$ = features.loadState(packageJSON, featContext, this.#featuresSettings)

if (this.featuresState$) {
const subscription = this.featuresState$
.pipe(map((_state) => features.getSnapshot(this.featuresState$)))
.subscribe((snapshot) => {
ContextState.addKey(FEATURES_CONTEXT_STATE_KEY, snapshot)
postClientMessage(this.messaging, ClientMessages.featuresUpdateAction, {
snapshot: snapshot,
if (this.featuresState$) {
const subscription = this.featuresState$
.pipe(map((_state) => features.getSnapshot(this.featuresState$)))
.subscribe((snapshot: string) => {
ContextState.addKey(FEATURES_CONTEXT_STATE_KEY, snapshot)
postClientMessage(this.messaging, ClientMessages.featuresUpdateAction, {
snapshot: snapshot,
})
})
})

this.#disposables.push({
dispose: () => subscription.unsubscribe(),
})
}
this.#disposables.push({
dispose: () => subscription.unsubscribe(),
})
}
})
}

get envProps() {
Expand Down
9 changes: 8 additions & 1 deletion tests/extension/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { test, expect, vi } from 'vitest'
import { notebooks, workspace, commands, window, Uri } from 'vscode'
import { notebooks, workspace, commands, window, Uri, ExtensionContext } from 'vscode'
// eslint-disable-next-line max-len
import { HealthCheckResponse_ServingStatus } from '@buf/grpc_grpc.community_timostamm-protobuf-ts/grpc/health/v1/health_pb'

import { RunmeExtension } from '../../src/extension/extension'
import { bootFile } from '../../src/extension/utils'
import KernelServer from '../../src/extension/server/kernelServer'
import { testCertPEM, testPrivKeyPEM } from '../testTLSCert'
import AuthSessionChangeHandler from '../../src/extension/authSessionChangeHandler'

vi.mock('vscode')
vi.mock('vscode-telemetry')
Expand Down Expand Up @@ -83,6 +84,12 @@ vi.mock('../../src/extension/utils', async () => ({

vi.mock('../../src/extension/grpc/runner/v1', () => ({}))

const contextFake: ExtensionContext = {
subscriptions: [],
} as any

AuthSessionChangeHandler.instance.initialize(contextFake)

test('initializes all providers', async () => {
const configValues = {
binaryPath: 'bin',
Expand Down
8 changes: 8 additions & 0 deletions tests/extension/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {
window,
authentication,
AuthenticationSession,
ExtensionContext,
} from 'vscode'
import { expect, suite, vi, test } from 'vitest'

import * as CellManager from '../../src/extension/cell'
import { github } from '../../src/extension/executors/github'
import { Kernel } from '../../src/extension/kernel'
import { IKernelExecutorOptions } from '../../src/extension/executors'
import AuthSessionChangeHandler from '../../src/extension/authSessionChangeHandler'

vi.mock('vscode', async () => {
const vscode = await import('../../__mocks__/vscode')
Expand All @@ -27,6 +29,12 @@ vi.mock('../../../src/extension/grpc/runner/v1', () => ({
ResolveProgramRequest_Mode: vi.fn(),
}))

const contextFake: ExtensionContext = {
subscriptions: [],
} as any

AuthSessionChangeHandler.instance.initialize(contextFake)

class OctokitMock {
protected rest: any
constructor() {
Expand Down
9 changes: 8 additions & 1 deletion tests/extension/messages/cellOutput.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { NotebookCell } from 'vscode'
import { ExtensionContext, NotebookCell } from 'vscode'
import { suite, vi, test, expect } from 'vitest'

import { handleCellOutputMessage } from '../../../src/extension/messages/cellOutput'
import { ClientMessages, OutputType } from '../../../src/constants'
import { Kernel } from '../../../src/extension/kernel'
import AuthSessionChangeHandler from '../../../src/extension/authSessionChangeHandler'

vi.mock('vscode')
vi.mock('vscode-telemetry')
Expand All @@ -13,6 +14,12 @@ vi.mock('../../../src/extension/grpc/runner/v1', () => ({
ResolveProgramRequest_Mode: vi.fn(),
}))

const contextFake: ExtensionContext = {
subscriptions: [],
} as any

AuthSessionChangeHandler.instance.initialize(contextFake)

suite('Handle CellOutput messages', () => {
const mockOutput = (type: OutputType) => {
const cell = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { authentication, notebooks } from 'vscode'
import { authentication, ExtensionContext, notebooks } from 'vscode'
import { suite, vi, it, beforeAll, afterAll, afterEach, expect } from 'vitest'
import { HttpResponse, graphql } from 'msw'
import { setupServer } from 'msw/node'
Expand All @@ -14,6 +14,7 @@ import {
StatefulAuthProvider,
StatefulAuthSession,
} from '../../../../src/extension/provider/statefulAuth'
import AuthSessionChangeHandler from '../../../../src/extension/authSessionChangeHandler'

vi.mock('vscode-telemetry')
vi.mock('../../../src/extension/runner', () => ({}))
Expand Down Expand Up @@ -44,7 +45,7 @@ vi.mock('vscode', async () => {
}
})

vi.mocked('../../../../src/extension/provider/statefulAuth')
// vi.mocked('../../../../src/extension/provider/statefulAuth')

vi.mock('../../../../src/extension/cell', async () => {
const actual = await import('../../../../src/extension/cell')
Expand All @@ -67,6 +68,12 @@ const graphqlHandlers = [

const server = setupServer(...graphqlHandlers)

const contextFake: ExtensionContext = {
subscriptions: [],
} as any

AuthSessionChangeHandler.instance.initialize(contextFake)

beforeAll(() => {
server.listen({ onUnhandledRequest: 'error' })
})
Expand Down Expand Up @@ -220,6 +227,7 @@ suite('Save cell execution', () => {
},
} as any,
}
vi.spyOn(StatefulAuthProvider, 'getSession').mockResolvedValue(undefined)
vi.mocked(authentication.getSession).mockResolvedValue(undefined)
await saveCellExecution(requestMessage, kernel)

Expand Down Expand Up @@ -256,9 +264,8 @@ suite('Save cell execution', () => {
[
{
"output": {
"data": {
"displayShare": false,
},
"data": "You must authenticate with your Stateful account",
"hasErrors": true,
"id": "cell-id",
},
"type": "common:platformApiResponse",
Expand Down
8 changes: 7 additions & 1 deletion tests/extension/provider/annotations.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { vi, describe, it, expect } from 'vitest'
import { NotebookCellKind } from 'vscode'
import { ExtensionContext, NotebookCellKind } from 'vscode'

import { AnnotationsStatusBarItem } from '../../../src/extension/provider/cellStatusBar/items/annotations'
import { Kernel } from '../../../src/extension/kernel'
import { OutputType } from '../../../src/constants'
import AuthSessionChangeHandler from '../../../src/extension/authSessionChangeHandler'

vi.mock('vscode')
vi.mock('vscode-telemetry')
Expand Down Expand Up @@ -32,6 +33,11 @@ vi.mock('../../../src/extension/utils', () => ({

vi.mock('../../../src/extension/runner', () => ({}))
vi.mock('../../../src/extension/grpc/runner/v1', () => ({}))
const contextFake: ExtensionContext = {
subscriptions: [],
} as any

AuthSessionChangeHandler.instance.initialize(contextFake)

describe('AnnotationsStatusBarItem test suite', () => {
const kernel = new Kernel({} as any)
Expand Down
9 changes: 8 additions & 1 deletion tests/extension/provider/copy.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { vi, test, expect } from 'vitest'
import { NotebookCellStatusBarAlignment } from 'vscode'
import { ExtensionContext, NotebookCellStatusBarAlignment } from 'vscode'

import { CopyStatusBarItem } from '../../../src/extension/provider/cellStatusBar/items/copy'
import { Kernel } from '../../../src/extension/kernel'
import AuthSessionChangeHandler from '../../../src/extension/authSessionChangeHandler'

vi.mock('vscode-telemetry')
vi.mock('vscode')

const contextFake: ExtensionContext = {
subscriptions: [],
} as any

AuthSessionChangeHandler.instance.initialize(contextFake)

test('NotebookCellStatusBarAlignment test suite', () => {
const kernel = new Kernel({} as any)
const p = new CopyStatusBarItem(kernel)
Expand Down
8 changes: 8 additions & 0 deletions tests/extension/provider/named.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ExtensionContext } from 'vscode'
import { vi, suite, test, expect, beforeEach } from 'vitest'

import { getAnnotations } from '../../../src/extension/utils'
import { NamedStatusBarItem } from '../../../src/extension/provider/cellStatusBar/items/named'
import { Kernel } from '../../../src/extension/kernel'
import AuthSessionChangeHandler from '../../../src/extension/authSessionChangeHandler'

vi.mock('vscode-telemetry')
vi.mock('vscode')
Expand All @@ -12,6 +14,12 @@ vi.mock('../../../src/extension/utils', () => ({
isValidEnvVarName: vi.fn().mockReturnValue(true),
}))

const contextFake: ExtensionContext = {
subscriptions: [],
} as any

AuthSessionChangeHandler.instance.initialize(contextFake)

suite('NamedStatusBarItem Test Suite', () => {
const kernel = new Kernel({} as any)

Expand Down
9 changes: 8 additions & 1 deletion tests/extension/provider/notebook.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { vi, describe, it, expect } from 'vitest'
import { commands, NotebookCellKind } from 'vscode'
import { commands, ExtensionContext, NotebookCellKind } from 'vscode'

import { NotebookCellStatusBarProvider } from '../../../src/extension/provider/cellStatusBar/notebook'
import { Kernel } from '../../../src/extension/kernel'
import AuthSessionChangeHandler from '../../../src/extension/authSessionChangeHandler'

vi.mock('vscode')
vi.mock('vscode-telemetry')
Expand All @@ -28,6 +29,12 @@ vi.mock('../../../src/extension/utils', () => ({
vi.mock('../../../src/extension/runner', () => ({}))
vi.mock('../../../src/extension/grpc/runner/v1', () => ({}))

const contextFake: ExtensionContext = {
subscriptions: [],
} as any

AuthSessionChangeHandler.instance.initialize(contextFake)

describe('Notebook Cell Status Bar provider', () => {
const kernel = new Kernel({} as any)
it('should register commands when initializing', () => {
Expand Down
8 changes: 7 additions & 1 deletion tests/extension/provider/sessionOutputs.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { vi, describe, it, expect } from 'vitest'
import { commands, NotebookCellKind } from 'vscode'
import { commands, ExtensionContext, NotebookCellKind } from 'vscode'

import { SessionOutputCellStatusBarProvider } from '../../../src/extension/provider/cellStatusBar/sessionOutput'
import { Kernel } from '../../../src/extension/kernel'
import AuthSessionChangeHandler from '../../../src/extension/authSessionChangeHandler'

vi.mock('vscode')
vi.mock('vscode-telemetry')
Expand All @@ -26,6 +27,11 @@ vi.mock('../../../src/extension/utils', () => ({

vi.mock('../../../src/extension/runner', () => ({}))
vi.mock('../../../src/extension/grpc/runner/v1', () => ({}))
const contextFake: ExtensionContext = {
subscriptions: [],
} as any

AuthSessionChangeHandler.instance.initialize(contextFake)

describe('Session Outputs Cell Status Bar provider', () => {
const kernel = new Kernel({} as any)
Expand Down

0 comments on commit 6956ac7

Please sign in to comment.