|
| 1 | +import { jest, describe, test, expect, beforeEach, afterEach } from '@jest/globals'; |
| 2 | +import { requestAllSpaces } from '../js/background/background.js'; |
| 3 | +import { spacesService } from '../js/background/spacesService.js'; |
| 4 | + |
| 5 | +describe('requestAllSpaces', () => { |
| 6 | + let getAllSessionsSpy; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + // Spy on the method and provide a mock implementation for each test |
| 10 | + getAllSessionsSpy = jest.spyOn(spacesService, 'getAllSessions'); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + // Restore the original implementation after each test |
| 15 | + getAllSessionsSpy.mockRestore(); |
| 16 | + }); |
| 17 | + |
| 18 | + test('should return sorted spaces with open windows first, then by last access time', async () => { |
| 19 | + const mockSessions = [ |
| 20 | + { id: 1, name: 'Saved Space 1', windowId: null, tabs: [{ url: 'http://a.com' }], lastAccess: new Date('2023-01-01T12:00:00Z') }, |
| 21 | + { id: 2, name: 'Open Window 1', windowId: 101, tabs: [{ url: 'http://b.com' }], lastAccess: new Date('2023-01-02T12:00:00Z') }, |
| 22 | + { id: 3, name: 'Saved Space 2', windowId: null, tabs: [{ url: 'http://c.com' }], lastAccess: new Date('2023-01-03T12:00:00Z') }, |
| 23 | + { id: 4, name: 'Open Window 2', windowId: 102, tabs: [{ url: 'http://d.com' }], lastAccess: new Date('2023-01-01T10:00:00Z') }, |
| 24 | + { id: 5, name: 'Empty Space', windowId: null, tabs: [], lastAccess: new Date('2023-01-05T12:00:00Z') }, // Should be filtered out |
| 25 | + ]; |
| 26 | + |
| 27 | + getAllSessionsSpy.mockResolvedValue(mockSessions); |
| 28 | + |
| 29 | + const spaces = await requestAllSpaces(); |
| 30 | + |
| 31 | + // Verify that getAllSessions was called |
| 32 | + expect(getAllSessionsSpy).toHaveBeenCalledTimes(1); |
| 33 | + |
| 34 | + // Check the length of the returned spaces (should exclude the empty one) |
| 35 | + expect(spaces.length).toBe(4); |
| 36 | + |
| 37 | + // Check the sorting order |
| 38 | + // 1. Open Window 1 (id: 2) - open |
| 39 | + // 2. Open Window 2 (id: 4) - open |
| 40 | + // 3. Saved Space 2 (id: 3) - most recent lastAccess |
| 41 | + // 4. Saved Space 1 (id: 1) - older lastAccess |
| 42 | + expect(spaces[0].id).toBe(2); // Open Window 1 |
| 43 | + expect(spaces[1].id).toBe(4); // Open Window 2 |
| 44 | + expect(spaces[2].id).toBe(3); // Saved Space 2 |
| 45 | + expect(spaces[3].id).toBe(1); // Saved Space 1 |
| 46 | + |
| 47 | + // Check that the mapping from session to space is correct |
| 48 | + expect(spaces[0]).toEqual(expect.objectContaining({ sessionId: 2, name: 'Open Window 1' })); |
| 49 | + }); |
| 50 | + |
| 51 | + test('should return an empty array when no sessions are available', async () => { |
| 52 | + getAllSessionsSpy.mockResolvedValue([]); |
| 53 | + |
| 54 | + const spaces = await requestAllSpaces(); |
| 55 | + |
| 56 | + expect(getAllSessionsSpy).toHaveBeenCalledTimes(1); |
| 57 | + expect(spaces).toEqual([]); |
| 58 | + }); |
| 59 | + |
| 60 | + test('should filter out sessions that have no tabs', async () => { |
| 61 | + const mockSessions = [ |
| 62 | + { id: 1, name: 'Valid Space', windowId: null, tabs: [{ url: 'http://a.com' }], lastAccess: new Date() }, |
| 63 | + { id: 2, name: 'Empty Space', windowId: 101, tabs: [], lastAccess: new Date() }, |
| 64 | + { id: 3, name: 'Null Tabs Space', windowId: null, tabs: null, lastAccess: new Date() }, |
| 65 | + ]; |
| 66 | + |
| 67 | + getAllSessionsSpy.mockResolvedValue(mockSessions); |
| 68 | + |
| 69 | + const spaces = await requestAllSpaces(); |
| 70 | + |
| 71 | + expect(spaces.length).toBe(1); |
| 72 | + expect(spaces[0].id).toBe(1); |
| 73 | + }); |
| 74 | + |
| 75 | + test('should handle sessions with only open windows', async () => { |
| 76 | + const mockSessions = [ |
| 77 | + { id: 1, name: 'Open Window 1', windowId: 101, tabs: [{ url: 'http://a.com' }], lastAccess: new Date('2023-01-01T12:00:00Z') }, |
| 78 | + { id: 2, name: 'Open Window 2', windowId: 102, tabs: [{ url: 'http://b.com' }], lastAccess: new Date('2023-01-02T12:00:00Z') }, |
| 79 | + ]; |
| 80 | + |
| 81 | + getAllSessionsSpy.mockResolvedValue(mockSessions); |
| 82 | + |
| 83 | + const spaces = await requestAllSpaces(); |
| 84 | + |
| 85 | + expect(spaces.length).toBe(2); |
| 86 | + // The order between two open windows doesn't have a secondary sort key in the original function, |
| 87 | + // so we just check that both are present. |
| 88 | + expect(spaces.map(s => s.id)).toContain(1); |
| 89 | + expect(spaces.map(s => s.id)).toContain(2); |
| 90 | + }); |
| 91 | + |
| 92 | + test('should correctly sort saved spaces by lastAccess date', async () => { |
| 93 | + const mockSessions = [ |
| 94 | + { id: 1, name: 'Oldest', windowId: null, tabs: [{ url: 'http://a.com' }], lastAccess: new Date('2023-01-01T12:00:00Z') }, |
| 95 | + { id: 2, name: 'Newest', windowId: null, tabs: [{ url: 'http://b.com' }], lastAccess: new Date('2023-01-03T12:00:00Z') }, |
| 96 | + { id: 3, name: 'Middle', windowId: null, tabs: [{ url: 'http://c.com' }], lastAccess: new Date('2023-01-02T12:00:00Z') }, |
| 97 | + ]; |
| 98 | + |
| 99 | + getAllSessionsSpy.mockResolvedValue(mockSessions); |
| 100 | + |
| 101 | + const spaces = await requestAllSpaces(); |
| 102 | + |
| 103 | + expect(spaces.length).toBe(3); |
| 104 | + expect(spaces[0].id).toBe(2); // Newest |
| 105 | + expect(spaces[1].id).toBe(3); // Middle |
| 106 | + expect(spaces[2].id).toBe(1); // Oldest |
| 107 | + }); |
| 108 | +}); |
0 commit comments