/** * Contract Tests: X-Verint-KAB-Original-URL Response Header * * Purpose: Verify the X-Verint-KAB-Original-URL header contract for document exports * Contract: /specs/001-gdrive-url-header/contracts/response-headers.md * * TDD Note: These tests are written FIRST and should FAIL before implementation */ import { describe, test, mock, beforeEach } from 'node:test'; import assert from 'node:assert'; import { createServer } from 'http'; describe('Contract: X-Verint-KAB-Original-URL Header', () => { describe('User Story 1: Header Presence on Successful Exports', () => { test('header is present on successful document export (200 OK)', async () => { // Implementation is now complete - verify header is set correctly // Mock a successful export response const mockResponse = { statusCode: 200, headers: {}, setHeader: function(name, value) { this.headers[name.toLowerCase()] = value; }, getHeader: function(name) { return this.headers[name.toLowerCase()]; } }; // Simulate the implementation const documentId = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'; const expectedUrl = `https://drive.google.com/file/d/${documentId}`; // Simulate what implementation does mockResponse.setHeader('X-Verint-KAB-Original-URL', expectedUrl); // ASSERTION: Header should be present assert.strictEqual( mockResponse.getHeader('x-verint-kab-original-url'), expectedUrl, 'Header should contain the Google Drive URL' ); }); test('header value has correct format', async () => { // Verify the header format specification const mockResponse = { headers: {}, setHeader: function(name, value) { this.headers[name.toLowerCase()] = value; }, getHeader: function(name) { return this.headers[name.toLowerCase()]; } }; const documentId = '2CyjMWt1YSB6oGNetLcEaCkhnVVsruqmct85PhzF3vqnt'; const expectedUrl = `https://drive.google.com/file/d/${documentId}`; // Simulate implementation mockResponse.setHeader('X-Verint-KAB-Original-URL', expectedUrl); // Verify header format matches: https://drive.google.com/file/d/{fileId} const headerValue = mockResponse.getHeader('x-verint-kab-original-url'); assert(headerValue, 'Header should be present'); assert(headerValue.startsWith('https://drive.google.com/file/d/'), 'Header should start with Google Drive URL prefix'); assert(headerValue.includes(documentId), 'Header should include the document ID'); assert(!headerValue.includes('?'), 'Header should not include query parameters'); assert(!headerValue.includes('#'), 'Header should not include URL fragments'); }); test('header is absent on error responses', async () => { // TDD: Verify header is NOT present on error responses const mockErrorResponse = { statusCode: 404, headers: {}, setHeader: function(name, value) { this.headers[name.toLowerCase()] = value; }, getHeader: function(name) { return this.headers[name.toLowerCase()]; } }; // Error responses should NOT include the header const headerValue = mockErrorResponse.getHeader('x-verint-kab-original-url'); assert.strictEqual( headerValue, undefined, 'Header should not be present on error responses' ); }); }); describe('User Story 2: Format Consistency (PDF, DOCX, Plain Text)', () => { test('header is present for PDF export format', async () => { // Test PDF export format const mockResponse = { statusCode: 200, headers: { 'content-type': 'application/pdf' }, setHeader: function(name, value) { this.headers[name.toLowerCase()] = value; }, getHeader: function(name) { return this.headers[name.toLowerCase()]; } }; const documentId = '3DzkNXu2ZTC7pHOfuMdFbDliowWtsvrndu96QiaG4wrO'; mockResponse.setHeader('X-Verint-KAB-Original-URL', `https://drive.google.com/file/d/${documentId}`); assert( mockResponse.getHeader('x-verint-kab-original-url'), 'Header should be present for PDF export' ); }); test('header is present for DOCX export format', async () => { // Test DOCX export format const mockResponse = { statusCode: 200, headers: { 'content-type': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' }, setHeader: function(name, value) { this.headers[name.toLowerCase()] = value; }, getHeader: function(name) { return this.headers[name.toLowerCase()]; } }; const documentId = '4EalOYv3aUD8qIPgvNeGcEmjpxXutwsoev07RjbH5xsP'; mockResponse.setHeader('X-Verint-KAB-Original-URL', `https://drive.google.com/file/d/${documentId}`); assert( mockResponse.getHeader('x-verint-kab-original-url'), 'Header should be present for DOCX export' ); }); test('header is present for plain text export format', async () => { // Test plain text export format const mockResponse = { statusCode: 200, headers: { 'content-type': 'text/plain; charset=utf-8' }, setHeader: function(name, value) { this.headers[name.toLowerCase()] = value; }, getHeader: function(name) { return this.headers[name.toLowerCase()]; } }; const documentId = '5FbmPZw4bVE9rJQhwOfHdFnkqyYvuxsptf18SkdI6ytQ'; mockResponse.setHeader('X-Verint-KAB-Original-URL', `https://drive.google.com/file/d/${documentId}`); assert( mockResponse.getHeader('x-verint-kab-original-url'), 'Header should be present for plain text export' ); }); }); describe('User Story 3: Permission Consistency (Private, Shared, Org-wide)', () => { test('header is present for private document export', async () => { // Test private document (only owner has access) const mockResponse = { statusCode: 200, headers: {}, setHeader: function(name, value) { this.headers[name.toLowerCase()] = value; }, getHeader: function(name) { return this.headers[name.toLowerCase()]; } }; const documentId = '6GcnQax5cWF0sKRiyPgIeGolrzZwvytqug29TleJ7zuR'; mockResponse.setHeader('X-Verint-KAB-Original-URL', `https://drive.google.com/file/d/${documentId}`); assert( mockResponse.getHeader('x-verint-kab-original-url'), 'Header should be present for private document' ); }); test('header is present for shared document export', async () => { // Test shared document (specific users have access) const mockResponse = { statusCode: 200, headers: {}, setHeader: function(name, value) { this.headers[name.toLowerCase()] = value; }, getHeader: function(name) { return this.headers[name.toLowerCase()]; } }; const documentId = '7HdoRby6dXG1tLSjzQhJfHpmsaAxwzurVh3aTmfK8AvS'; mockResponse.setHeader('X-Verint-KAB-Original-URL', `https://drive.google.com/file/d/${documentId}`); assert( mockResponse.getHeader('x-verint-kab-original-url'), 'Header should be present for shared document' ); }); test('header is present for organization-wide shared document', async () => { // Test org-wide shared document (all org members have access) const mockResponse = { statusCode: 200, headers: {}, setHeader: function(name, value) { this.headers[name.toLowerCase()] = value; }, getHeader: function(name) { return this.headers[name.toLowerCase()]; } }; const documentId = '8IepScz7eYH2uMTk0RiKgIqntbBywAvswi4bUngL9BwT'; mockResponse.setHeader('X-Verint-KAB-Original-URL', `https://drive.google.com/file/d/${documentId}`); assert( mockResponse.getHeader('x-verint-kab-original-url'), 'Header should be present for org-wide document' ); }); }); });