1- import { describe , expect , it } from "vitest" ;
2- import {
3- PAGE_SHARE_MAX_CONTENT_CHARS ,
4- PAGE_SHARE_MAX_NOTE_CHARS ,
5- PAGE_SHARE_MAX_TITLE_CHARS ,
6- PAGE_SHARE_MAX_URL_CHARS ,
7- buildPageSharePayload ,
8- capturePageContent ,
9- googleDocIdFromUrl ,
10- truncateShareText ,
11- } from "./page-share-core.js" ;
1+ import { afterEach , describe , expect , it , vi } from "vitest" ;
2+ import { buildPageSharePayload , capturePageShare } from "./page-share-core.js" ;
3+
4+ const PAGE_SHARE_MAX_CONTENT_CHARS = 120_000 ;
5+ const PAGE_SHARE_MAX_NOTE_CHARS = 2_000 ;
6+ const PAGE_SHARE_MAX_TITLE_CHARS = 500 ;
7+ const PAGE_SHARE_MAX_URL_CHARS = 2_000 ;
128
139describe ( "page share core" , ( ) => {
14- it ( "extracts Google document ids from document URLs only" , ( ) => {
15- expect (
16- googleDocIdFromUrl ( "https://docs.google.com/document/d/document-id_123/edit?tab=t.0" ) ,
17- ) . toBe ( "document-id_123" ) ;
18- expect ( googleDocIdFromUrl ( "https://docs.google.com/spreadsheets/d/sheet-id/edit" ) ) . toBeNull ( ) ;
19- expect ( googleDocIdFromUrl ( "https://example.com/document/d/not-google/edit" ) ) . toBeNull ( ) ;
20- expect ( googleDocIdFromUrl ( "not a URL" ) ) . toBeNull ( ) ;
10+ afterEach ( ( ) => {
11+ vi . unstubAllGlobals ( ) ;
12+ } ) ;
13+
14+ it ( "exports Google documents in the tab using the document id" , async ( ) => {
15+ const executeScript = vi
16+ . fn ( )
17+ . mockResolvedValueOnce ( [ { result : "" } ] )
18+ . mockResolvedValueOnce ( [ { result : { text : "Document body" } } ] ) ;
19+ vi . stubGlobal ( "chrome" , { scripting : { executeScript } } ) ;
20+
21+ await expect (
22+ capturePageShare ( {
23+ id : 17 ,
24+ url : "https://docs.google.com/document/d/document-id_123/edit?tab=t.0" ,
25+ title : "Document" ,
26+ } ) ,
27+ ) . resolves . toEqual ( {
28+ url : "https://docs.google.com/document/d/document-id_123/edit?tab=t.0" ,
29+ title : "Document" ,
30+ selection : "" ,
31+ content : "Document body" ,
32+ } ) ;
33+ expect ( executeScript . mock . calls [ 1 ] ?. [ 0 ] ) . toMatchObject ( {
34+ target : { tabId : 17 } ,
35+ args : [ "document-id_123" ] ,
36+ } ) ;
2137 } ) ;
2238
2339 it ( "keeps text at the boundary and marks truncation beyond it" , ( ) => {
24- expect ( truncateShareText ( "12345" , 5 ) ) . toBe ( "12345" ) ;
25- expect ( truncateShareText ( "123456" , 5 ) ) . toBe ( "12345\n\n[Truncated: original was 6 characters]" ) ;
40+ const atBoundary = buildPageSharePayload ( {
41+ url : "https://example.com" ,
42+ title : "Example" ,
43+ content : "x" . repeat ( PAGE_SHARE_MAX_CONTENT_CHARS ) ,
44+ } ) ;
45+ const beyondBoundary = buildPageSharePayload ( {
46+ url : "https://example.com" ,
47+ title : "Example" ,
48+ content : "x" . repeat ( PAGE_SHARE_MAX_CONTENT_CHARS + 1 ) ,
49+ } ) ;
50+ expect ( atBoundary . content ) . toHaveLength ( PAGE_SHARE_MAX_CONTENT_CHARS ) ;
51+ expect (
52+ beyondBoundary . content . endsWith (
53+ `[Truncated: original was ${ PAGE_SHARE_MAX_CONTENT_CHARS + 1 } characters]` ,
54+ ) ,
55+ ) . toBe ( true ) ;
2656 } ) ;
2757
2858 it ( "trims fields, preserves newlines, applies caps, and drops empty optionals" , ( ) => {
@@ -42,8 +72,21 @@ describe("page share core", () => {
4272 expect ( payload ) . not . toHaveProperty ( "selection" ) ;
4373 } ) ;
4474
45- it ( "keeps the injected capture function self-contained" , ( ) => {
46- const source = String ( capturePageContent ) ;
75+ it ( "keeps the injected capture function self-contained" , async ( ) => {
76+ const executeScript = vi . fn ( ) . mockResolvedValue ( [
77+ {
78+ result : {
79+ url : "https://example.com" ,
80+ title : "Example" ,
81+ selection : "" ,
82+ content : "Body" ,
83+ } ,
84+ } ,
85+ ] ) ;
86+ vi . stubGlobal ( "chrome" , { scripting : { executeScript } } ) ;
87+
88+ await capturePageShare ( { id : 9 , url : "https://example.com" , title : "Example" } ) ;
89+ const source = String ( executeScript . mock . calls [ 0 ] ?. [ 0 ] . func ) ;
4790 expect ( source ) . not . toMatch ( / \b (?: i m p o r t | r e q u i r e ) \b / u) ;
4891 } ) ;
4992} ) ;
0 commit comments