diff --git a/.vscode-test.mjs b/.vscode-test.mjs index b62ba25..1507065 100644 --- a/.vscode-test.mjs +++ b/.vscode-test.mjs @@ -1,5 +1,5 @@ import { defineConfig } from '@vscode/test-cli'; export default defineConfig({ - files: 'out/test/**/*.test.js', + files: 'out/*.test.js', }); diff --git a/src/extension.test.ts b/src/extension.test.ts index 4ca0ab4..a02f59d 100644 --- a/src/extension.test.ts +++ b/src/extension.test.ts @@ -1,15 +1,134 @@ -import * as assert from 'assert'; - -// You can import and use all API from the 'vscode' module -// as well as import your extension to test it +import assert = require('assert'); import * as vscode from 'vscode'; -// import * as myExtension from '../../extension'; +import sinon from 'sinon'; +import { SidebarProvider } from './SidebarProvider'; + +const createMockExtensionContext = (): vscode.ExtensionContext => ({ + subscriptions: [], + extensionPath: '/mock/path', + extensionUri: vscode.Uri.parse('file:///mock/path'), + globalState: { + get: sinon.stub(), + update: sinon.stub(), + }, + workspaceState: { + get: sinon.stub(), + update: sinon.stub(), + }, + asAbsolutePath: sinon.stub().returns('/mock/absolute/path'), + storagePath: '/mock/storage/path', + globalStoragePath: '/mock/global/storage/path', + logPath: '/mock/log/path', +} as unknown as vscode.ExtensionContext); + +// Mock classes for FakeWebview and FakeWebviewView to simulate behavior +class FakeWebview implements vscode.Webview { + public html = ''; + public options = {}; + public cspSource = ''; + public onDidReceiveMessage = sinon.spy(); + public asWebviewUri(uri: vscode.Uri): vscode.Uri { + return uri; + } + public postMessage(message: any): Thenable { + this.onDidReceiveMessage(message); // Trigger the spy with the message + return Promise.resolve(true); + } +} + +class FakeWebviewView implements vscode.WebviewView { + constructor(public webview: vscode.Webview) {} + viewType: any; + badge?: vscode.ViewBadge | undefined; + show(preserveFocus?: boolean): void { + throw new Error('Method not implemented.'); + } + public title = ''; + public description = ''; + public onDidDispose = sinon.spy(); + public onDidChangeVisibility = sinon.spy(); + public onDidChangeViewState = sinon.spy(); + public visible = true; + public dispose() {} +} + +suite('Sidebar Test Suite', () => { + + let mockContext: vscode.ExtensionContext; + let extensionUri: vscode.Uri; + + setup(() => { + mockContext = createMockExtensionContext(); + extensionUri = vscode.Uri.parse('http://www.example.com/some/path'); + }); + + test('Sidebar Provider Registration', () => { + const sidebarProvider = new SidebarProvider(extensionUri, mockContext); + + const registerWebviewViewProviderSpy = sinon.spy(vscode.window, 'registerWebviewViewProvider'); + + vscode.window.registerWebviewViewProvider('Test-Sidebar', sidebarProvider); + + assert(registerWebviewViewProviderSpy.calledOnce); + assert( + registerWebviewViewProviderSpy.calledWith( + 'Test-Sidebar', + sinon.match.instanceOf(SidebarProvider) + ) + ); -suite('Extension Test Suite', () => { - vscode.window.showInformationMessage('Start all tests.'); + registerWebviewViewProviderSpy.restore(); + }); + - test('Sample test', () => { - assert.strictEqual(-1, [1, 2, 3].indexOf(5)); - assert.strictEqual(-1, [1, 2, 3].indexOf(0)); - }); + test('Sidebar Content Rendering', async () => { + const sidebarProvider = new SidebarProvider(extensionUri, mockContext); + const webview = new FakeWebview() as vscode.Webview; + const view = new FakeWebviewView(webview) as vscode.WebviewView; + + await sidebarProvider.resolveWebviewView(view); + + assert.strictEqual( + webview.html.includes(''), + true + ); + // Clean up + + }); + + test('Sidebar Comprehensive Functionality', async () => { + const sidebarProvider = new SidebarProvider(extensionUri, mockContext); + const webview = new FakeWebview() as vscode.Webview; + const view = new FakeWebviewView(webview) as unknown as vscode.WebviewView; + + await sidebarProvider.resolveWebviewView(view); + + // Simulate the creation of the webview panel + assert.strictEqual(webview.html.includes(''), true, 'DOCTYPE is missing'); + assert.strictEqual(webview.html.includes(''), true, 'HTML tag is missing'); + assert.strictEqual(webview.html.includes(''), true, 'Head tag is missing'); + assert.strictEqual(webview.html.includes(''), true, 'UTF-8 charset meta tag is missing'); + assert.strictEqual(webview.html.includes('